Le repo des sources pour le site web des JM2L
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

668 linhas
26 KiB

  1. # -*- coding: utf8 -*-
  2. import datetime
  3. import sqlalchemy as sa
  4. import hashlib
  5. from pyramid.security import unauthenticated_userid
  6. from sqlalchemy.orm import relationship, backref
  7. from sqlalchemy import func
  8. from sqlalchemy import or_
  9. from sqlalchemy import (
  10. Column,
  11. Integer,
  12. Text,
  13. Unicode,
  14. UnicodeText,
  15. DateTime,
  16. Enum,
  17. Boolean,
  18. ForeignKey
  19. )
  20. from slugify import slugify
  21. from webhelpers.text import urlify
  22. from webhelpers.paginate import PageURL_WebOb, Page
  23. from webhelpers.date import time_ago_in_words
  24. from collections import namedtuple
  25. from sqlalchemy.ext.declarative import declarative_base
  26. from sqlalchemy.orm import (
  27. scoped_session,
  28. sessionmaker,
  29. relation
  30. )
  31. from zope.sqlalchemy import ZopeTransactionExtension
  32. DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
  33. Base = declarative_base()
  34. CurrentYear = 2015
  35. class TasksArea(Base):
  36. __tablename__ = 'staff_tasks_area'
  37. uid = Column(Integer, primary_key=True)
  38. name = Column(Unicode(80))
  39. description = Column(UnicodeText)
  40. @classmethod
  41. def by_id(cls, id):
  42. return DBSession.query(cls).filter(cls.uid == id).first()
  43. class Tasks(Base):
  44. __tablename__ = 'staff_tasks'
  45. uid = Column(Integer, primary_key=True)
  46. area_uid = Column(Integer, ForeignKey('staff_tasks_area.uid') )
  47. due_date = Column(DateTime, default=None)
  48. closed_by = Column(Integer, ForeignKey('users.uid') )
  49. closed_date = Column(DateTime, default=None)
  50. closed = Column(Integer, default=0)
  51. name = Column(Unicode(80))
  52. description = Column(UnicodeText)
  53. area = relationship(TasksArea, backref=backref("tasks") )
  54. assignee = relationship('User', backref=backref("task_assoc") )
  55. @classmethod
  56. def by_id(cls, id):
  57. return DBSession.query(cls).filter(cls.uid == id).first()
  58. class User_Event(Base):
  59. """ Créer le lien entre la personne et l' évenement en fonction de l'année"""
  60. __tablename__ = 'user_event_link'
  61. uid = Column(Integer, primary_key=True)
  62. event_uid = Column(Integer, ForeignKey('events.uid') )
  63. #, primary_key=True)
  64. #
  65. user_uid = Column(Integer, ForeignKey('users.uid') )
  66. #, primary_key=True)
  67. #
  68. year_uid = Column(Integer, ForeignKey('jm2l_year.year_uid'), default=CurrentYear)
  69. role = Column(Unicode(80))
  70. # Define some relation
  71. #user = relationship('User', backref=backref("events_assoc") )
  72. #event = relationship('events', backref=backref("users_assoc") )
  73. class JM2L_Year(Base):
  74. __tablename__ = 'jm2l_year'
  75. year_uid = Column(Integer, primary_key=True)
  76. description = Column(UnicodeText)
  77. doss_presse = Column(UnicodeText)
  78. state = Column(Enum('Archived', 'Cancelled', 'Ongoing'))
  79. start_time = Column(DateTime, default=datetime.datetime.now)
  80. end_time = Column(DateTime, default=datetime.datetime.now)
  81. created = Column(DateTime, default=datetime.datetime.now)
  82. last_change = Column(DateTime, default=datetime.datetime.now)
  83. @property
  84. def AvailableTimeSlots(self, TimeStep=30):
  85. Available = self.end_time - self.start_time
  86. NbMinutes = Available.total_seconds()/60
  87. NbSteps = NbMinutes/TimeStep
  88. # Create the range of date each 30min
  89. date_list = [self.start_time + datetime.timedelta(minutes=TimeStep*x) for x in range(0, int(NbSteps))]
  90. # Remove out of range datetime
  91. # Remove hours > 19h
  92. date_list = filter(lambda x:x.hour < 19, date_list)
  93. # Remove hours < 10h
  94. date_list = filter(lambda x:x.hour >= 10, date_list)
  95. # Remove 12h < hours < 13h
  96. date_list = filter(lambda x: x.hour<12 or x.hour>=13, date_list)
  97. return date_list
  98. @property
  99. def DocLinks(self):
  100. from .upload import MediaPath
  101. return zip( MediaPath().get_list('presse', self.year_uid, 'Other'),
  102. MediaPath().get_thumb('presse', self.year_uid, 'Other')
  103. )
  104. class User(Base):
  105. __tablename__ = 'users'
  106. uid = Column(Integer, primary_key=True)
  107. user_id = Column(Integer)
  108. nom = Column(Unicode(80))
  109. prenom = Column(Unicode(80))
  110. pseudo = Column(Unicode(80))
  111. slug = Column(Unicode(164))
  112. mail = Column(Unicode(100))
  113. password = Column(Unicode(100), nullable=False)
  114. fonction = Column(Unicode(80))
  115. website = Column(Unicode(100))
  116. phone = Column(Unicode(10))
  117. created = Column(DateTime, default=datetime.datetime.now)
  118. last_logged = Column(DateTime, default=datetime.datetime.now)
  119. last_change = Column(DateTime, default=datetime.datetime.now)
  120. active = Column(Integer, default=1)
  121. bio = Column(UnicodeText)
  122. gpg_key = Column(UnicodeText)
  123. soc_link = Column(UnicodeText)
  124. Staff = Column(Integer, default=0)
  125. vote_logo = Column(Integer, default=0)
  126. # relations
  127. tiers = relationship('Tiers', secondary='user_tiers_link' )
  128. events = relationship('Event', secondary='user_event_link' )
  129. tiersship = relationship('User_Tiers', backref="matching_users")
  130. @classmethod
  131. def by_id(cls, id):
  132. return DBSession.query(cls).filter(cls.uid == id).first()
  133. @classmethod
  134. def by_mail(cls, mail):
  135. return DBSession.query(cls).filter(cls.mail == mail).first()
  136. @classmethod
  137. def by_slug(cls, slug):
  138. return DBSession.query(cls).filter(cls.slug == slug).first()
  139. @classmethod
  140. def by_user_id(cls, user_id):
  141. return DBSession.query(cls).filter(cls.user_id == user_id).first()
  142. @classmethod
  143. def by_name(cls, name):
  144. return DBSession.query(cls).filter(cls.nom == name).first()
  145. @classmethod
  146. def by_hash(cls, tsthash):
  147. for u in DBSession.query(cls):
  148. if u.my_hash==tsthash:
  149. return u
  150. return None
  151. @property
  152. def my_hash(self):
  153. m = hashlib.sha1()
  154. m.update("Nobody inspects ")
  155. if self.nom:
  156. m.update(unicode.encode(self.nom,'utf8'))
  157. if self.pseudo:
  158. m.update(unicode.encode(self.pseudo,'utf8'))
  159. if self.prenom:
  160. m.update(unicode.encode(self.prenom,'utf8'))
  161. m.update(" the spammish repetition")
  162. return m.hexdigest()
  163. @property
  164. def Photos(self):
  165. return DBSession.query(Media.filename) \
  166. .filter(Media.media_table=='users') \
  167. .filter(Media.media_type=='Image') \
  168. .filter(Media.link_id == self.user_id).all()
  169. @property
  170. def PhotosLinks(self):
  171. from .upload import MediaPath
  172. return MediaPath().get_list('users', self.uid)
  173. @property
  174. def PhotosThumb(self):
  175. from .upload import MediaPath
  176. return MediaPath().get_thumb('users', self.uid)
  177. def verify_password(self, password):
  178. return self.password == password
  179. class TiersOpt(Base):
  180. __tablename__ = 'tiers_opt'
  181. uid = Column(Integer, primary_key=True)
  182. entity_type = Column(Unicode(80), nullable=False)
  183. entity_subtype = Column(Unicode(80))
  184. entity_role = Column(Unicode(80))
  185. @property
  186. def slug_entity_type(self):
  187. return slugify(self.entity_type)
  188. @property
  189. def slug_entity_subtype(self):
  190. return slugify(self.entity_subtype)
  191. @classmethod
  192. def get_entity_type(cls):
  193. return DBSession.query(cls, func.count(Tiers.ent_type).label('count'))\
  194. .outerjoin(Tiers)\
  195. .group_by(cls.entity_type).all()
  196. @classmethod
  197. def get_entity_sub_type(cls, entity_type):
  198. return DBSession.query(cls, func.count(Tiers.ent_type).label('count'))\
  199. .outerjoin(Tiers)\
  200. .filter(cls.entity_type == entity_type)\
  201. .group_by(cls.entity_subtype).all()
  202. @classmethod
  203. def by_id(cls, id):
  204. return DBSession.query(cls).filter(cls.uid == id).first()
  205. class Tiers(Base):
  206. __tablename__ = 'tiers'
  207. uid = Column(Integer, primary_key=True)
  208. tiers_id = Column(Integer)
  209. name = Column(Unicode(100), nullable=False)
  210. slug = Column(Unicode(100))
  211. description = Column(UnicodeText)
  212. website = Column(Unicode(100))
  213. tiers_type = Column(Integer, ForeignKey('tiers_opt.uid'), default=1)
  214. created = Column(DateTime, default=datetime.datetime.now)
  215. last_change = Column(DateTime, default=datetime.datetime.now)
  216. # relations
  217. ent_type = relationship('TiersOpt')
  218. #members = relationship('User', secondary='user_tiers_link' )
  219. members = relationship(User,
  220. secondary='user_tiers_link',
  221. backref=backref('associate', uselist=False),
  222. lazy='dynamic')
  223. creator_id = Column(Integer)
  224. membership = relationship('User_Tiers', backref="matching_tiers")
  225. roles = relationship('Role_Tiers', backref="roles_tiers") #secondary='role_tiers_link' )
  226. @classmethod
  227. def by_id(cls, id):
  228. return DBSession.query(cls).filter(cls.uid == id).first()
  229. @classmethod
  230. def by_slug(cls, slug):
  231. return DBSession.query(cls).filter(cls.slug == slug).first()
  232. @property
  233. def get_entity_type(self):
  234. return DBSession.query(TiersOpt)\
  235. .filter(TiersOpt.uid == self.tiers_type).first()
  236. @property
  237. def logo(self):
  238. return DBSession.query(Media) \
  239. .filter(Media.media_table == 'tiers') \
  240. .filter(Media.media_type == 'Image') \
  241. .filter(Media.link_id == self.uid)
  242. @property
  243. def DocLinks(self):
  244. from .upload import MediaPath
  245. return zip( MediaPath().get_list('tiers', self.uid, 'Other'),
  246. MediaPath().get_thumb('tiers', self.uid, 'Other')
  247. )
  248. @property
  249. def PhotosLinks(self):
  250. from .upload import MediaPath
  251. return MediaPath().get_list('tiers', self.uid, 'Image')
  252. @property
  253. def ThumbLinks(self):
  254. from .upload import MediaPath
  255. return MediaPath().get_thumb('tiers', self.uid)
  256. class Role_Tiers(Base):
  257. """ Créer le lien entre le tiers et son rôle dans l'évenement en fonction de l'année"""
  258. __tablename__ = 'role_tiers_link'
  259. uid_role = Column(Integer, primary_key=True)
  260. year_uid = Column(Integer, ForeignKey('jm2l_year.year_uid'), default=CurrentYear)
  261. tiers_uid = Column(Integer, ForeignKey('tiers.uid'))
  262. tiers = relationship(Tiers, backref=backref("roles_assoc") )
  263. tiers_role = Column(Enum('Exposant', 'Sponsor', 'Donateur'))
  264. class User_Tiers(Base):
  265. """ Créer le lien entre la personne et le tiers en fonction de l'année"""
  266. __tablename__ = 'user_tiers_link'
  267. uid_tiers = Column(Integer, primary_key=True)
  268. year_uid = Column(Integer, ForeignKey('jm2l_year.year_uid'), default=CurrentYear)
  269. tiers_uid = Column(Integer, ForeignKey('tiers.uid'))
  270. tiers = relationship(Tiers, backref=backref("users_assoc") )
  271. user_uid = Column(Integer, ForeignKey('users.uid'))
  272. user = relationship(User, backref=backref("tiers_assoc") )
  273. role = Column(Unicode(80))
  274. class Media(Base):
  275. __tablename__ = 'medias'
  276. media_id = Column(Integer, primary_key=True)
  277. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid'))
  278. media_table = Column(Enum('users', 'tiers', 'place', 'salle', 'RIB', 'Justif', 'event' ))
  279. media_type = Column(Enum('Image', 'Video', 'Pres', 'Document'))
  280. link_id = Column(Integer)
  281. mime_type = Column(Unicode(20))
  282. size = Column(Integer)
  283. width = Column(Integer)
  284. height = Column(Integer)
  285. length = Column(Integer)
  286. filename = Column(UnicodeText)
  287. created = Column(DateTime, default=datetime.datetime.now)
  288. @property
  289. def get_path(self):
  290. #return '/upload/%s/%s/%s' % (self.media_type, self.media_table, self.filename)
  291. return '/resources/%s/%s/%s' % (self.for_year, self.media_type, self.filename)
  292. class SallePhy(Base):
  293. """ Représente une salle dans les locaux """
  294. __tablename__ = 'phy_salle'
  295. uid = Column(Integer, primary_key=True)
  296. name = Column(Unicode(40)) # Numéro de salle vu de polytech
  297. slug = Column(Unicode(40))
  298. description = Column(UnicodeText) # Description du matériel disponible
  299. nb_places = Column(Integer, default=0) # Nombre de places assises
  300. @classmethod
  301. def by_id(cls, uid):
  302. return DBSession.query(cls).filter(cls.uid == uid).first()
  303. class Salles(Base):
  304. __tablename__ = 'salle'
  305. salle_id = Column(Integer, primary_key=True)
  306. phy_salle_id = Column(Integer, ForeignKey('phy_salle.uid'))
  307. year_uid = Column(Integer, ForeignKey('jm2l_year.year_uid'), default=CurrentYear)
  308. name = Column(Unicode(40))
  309. place_type = Column(Enum('Conference', 'Stand', 'Ateliers', 'Autres'))
  310. description = Column(UnicodeText) # Description du matériel disponible
  311. created = Column(DateTime, default=datetime.datetime.now)
  312. last_change = Column(DateTime, default=datetime.datetime.now)
  313. phy = relationship(SallePhy)
  314. @classmethod
  315. def by_id(cls, uid):
  316. return DBSession.query(cls).filter(cls.salle_id == uid).first()
  317. class Place(Base):
  318. __tablename__ = 'place'
  319. place_id = Column(Integer, primary_key=True)
  320. usage = Column(Boolean, default=False) # By Default / Extended
  321. place_type = Column(Enum('Aeroport', 'Gare', 'JM2L', \
  322. 'Hotel', 'Habitant', 'Restaurant', 'Autres'))
  323. display_name = Column(Unicode(20))
  324. name = Column(Unicode(80))
  325. slug = Column(Unicode(80))
  326. specific = Column(Unicode(80)) # eg Terminal 2
  327. gps_coord = Column(Unicode(30))
  328. adresse = Column(Unicode(100))
  329. codePostal = Column(Unicode(5))
  330. ville = Column(Unicode(40))
  331. website = Column(Unicode(100))
  332. description = Column(UnicodeText)
  333. created_by = Column(Integer, ForeignKey('users.user_id'))
  334. created = Column(DateTime, default=datetime.datetime.now)
  335. last_change = Column(DateTime, default=datetime.datetime.now)
  336. @classmethod
  337. def by_id(cls, uid):
  338. return DBSession.query(cls).filter(cls.place_id == uid).first()
  339. @classmethod
  340. def get_list(cls, All=False):
  341. if All:
  342. return DBSession.query(cls).all()
  343. else:
  344. return DBSession.query(cls).filter(cls.usage==True).all()
  345. class Itineraire(Base):
  346. __tablename__ = 'itineraire'
  347. itin_id = Column(Integer, primary_key=True)
  348. start_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  349. arrival_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  350. distance = Column(Integer)
  351. duration = Column(Integer)
  352. price = Column(Integer)
  353. tr_pied = Column(Boolean, default=False)
  354. tr_velo = Column(Boolean, default=False)
  355. tr_moto = Column(Boolean, default=False)
  356. tr_voiture = Column(Boolean, default=False)
  357. tr_bus = Column(Boolean, default=False)
  358. tr_taxi = Column(Boolean, default=False)
  359. tr_avion = Column(Boolean, default=False)
  360. description = Column(UnicodeText)
  361. created_by = Column(Integer, ForeignKey('users.user_id')) # User link
  362. created = Column(DateTime, default=datetime.datetime.now)
  363. last_change = Column(DateTime, default=datetime.datetime.now)
  364. # relations
  365. start = relationship(Place, foreign_keys=[start_place])
  366. arrival = relationship(Place, foreign_keys=[arrival_place])
  367. class Exchange_Cat(Base):
  368. __tablename__ = 'exchange_category'
  369. cat_id = Column(Integer, primary_key=True)
  370. exch_type = Column(Enum('H', 'C', 'M')) # Heberg, Co-voit, Materiel
  371. exch_subtype = Column(Unicode(80))
  372. description = Column(UnicodeText)
  373. class Exchange(Base):
  374. __tablename__ = 'exchanges'
  375. exch_id = Column(Integer, primary_key=True)
  376. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid')) # link JM2L_Year
  377. exch_done = Column(Boolean, default=False)
  378. exch_state = Column(Enum('Ask', 'Proposal'))
  379. exch_type = Column(Enum('H', 'C', 'M')) # Heberg, Co-Voit, Materiel
  380. exch_categ = Column(Integer, ForeignKey('exchange_category.cat_id')) # Exchange_Cat link
  381. # Users
  382. asker_id = Column(Integer, ForeignKey('users.uid')) # User link
  383. provider_id = Column(Integer, ForeignKey('users.uid')) # User link
  384. start_time = Column(DateTime, default=datetime.datetime.now)
  385. end_time = Column(DateTime, default=datetime.datetime.now)
  386. # Co-voiturage
  387. itin_id = Column(Integer, ForeignKey('itineraire.itin_id')) # Itineraire link
  388. # Hebergement
  389. place_id = Column(Integer, ForeignKey('place.place_id')) # Place link
  390. # Materiel
  391. duration = Column(Integer)
  392. description = Column(UnicodeText)
  393. pictures = Column(Unicode(80))
  394. created_by = Column(Integer) # User link
  395. created = Column(DateTime, default=datetime.datetime.now)
  396. last_change = Column(DateTime, default=datetime.datetime.now)
  397. # relations
  398. Category = relationship(Exchange_Cat, backref="exchanges")
  399. Itin = relationship(Itineraire, backref="exchanged")
  400. asker = relationship(User, foreign_keys=[asker_id], backref="asked")
  401. provider = relationship(User, foreign_keys=[provider_id], backref="provided")
  402. @classmethod
  403. def by_id(cls, id):
  404. return DBSession.query(cls).filter(cls.exch_id == id).first()
  405. @classmethod
  406. def get_counters(cls):
  407. return DBSession.query(cls.exch_state, cls.exch_type, cls.exch_done, func.count(cls.exch_id))\
  408. .filter(cls.for_year==2015)\
  409. .group_by(cls.exch_state, cls.exch_type, cls.exch_done)
  410. @classmethod
  411. def get_my_counters(cls, uid):
  412. return DBSession.query(cls.exch_state, cls.exch_type, cls.exch_done, func.count(cls.exch_id))\
  413. .filter(cls.for_year==2015)\
  414. .filter( or_(cls.asker_id==uid, cls.provider_id==uid) )\
  415. .group_by(cls.exch_state, cls.exch_type, cls.exch_done)
  416. @classmethod
  417. def get_overview(cls, uid):
  418. # Build a Dic with all exchange to save database access
  419. DicResult= {}
  420. for extype in ['F','C','H','M']:
  421. DicResult[extype] = {}
  422. for exstate in ['Ask','Proposal','Missing','Agree']:
  423. DicResult[extype][exstate]=[]
  424. DicResult[extype]['Counters']={'AllAsk':0, 'AllProp':0, 'AllAgree':0}
  425. Query = DBSession.query(cls)\
  426. .filter(cls.for_year==2015)\
  427. .order_by(cls.start_time).all()
  428. for item in Query:
  429. if item.exch_done:
  430. DicResult[item.exch_type]['Counters']['AllAgree']+=1
  431. if item.exch_state=='Ask':
  432. DicResult[item.exch_type]['Counters']['AllAsk']+=1
  433. if item.exch_state=='Proposal':
  434. DicResult[item.exch_type]['Counters']['AllProp']+=1
  435. if item.asker_id==uid or item.provider_id==uid:
  436. if item.asker_id==uid and item.exch_state=='Ask':
  437. DicResult[item.exch_type]['Ask'].append(item)
  438. if item.provider_id==uid and item.exch_state=='Ask':
  439. DicResult[item.exch_type]['Proposal'].append(item)
  440. if item.asker_id==uid and item.exch_state=='Proposal':
  441. DicResult[item.exch_type]['Ask'].append(item)
  442. if item.provider_id==uid and item.exch_state=='Proposal':
  443. DicResult[item.exch_type]['Proposal'].append(item)
  444. if item.exch_done:
  445. DicResult[item.exch_type]['Agree'].append(item)
  446. else:
  447. DicResult[item.exch_type]['Missing'].append(item)
  448. return DicResult
  449. @classmethod
  450. def get_pub_list(cls, exch_type):
  451. return DBSession.query(cls).filter(cls.for_year==2015 and exch_state in ['Ask','Proposal'])\
  452. .filter(cls.exch_type=='%s' % exch_type)\
  453. .filter(cls.exch_done==False)\
  454. .all()
  455. @classmethod
  456. def get_my_list(cls, uid, exch_type):
  457. DicResult = {}
  458. DicResult['Ask']=DBSession.query(cls)\
  459. .filter(cls.for_year==2015)\
  460. .filter( or_(cls.asker_id==uid, cls.provider_id==uid) )\
  461. .filter(cls.exch_type=='%s' % exch_type)\
  462. .filter(cls.exch_state=='Ask')\
  463. .order_by(cls.start_time).all()
  464. DicResult['Proposal']=DBSession.query(cls)\
  465. .filter(cls.for_year==2015)\
  466. .filter( or_(cls.asker_id==uid, cls.provider_id==uid) )\
  467. .filter(cls.exch_type=='%s' % exch_type)\
  468. .filter(cls.exch_state=='Proposal')\
  469. .order_by(cls.start_time).all()
  470. return DicResult
  471. class Sejour(Base):
  472. __tablename__ = 'sejour'
  473. sej_id = Column(Integer, primary_key=True)
  474. user_id = Column(Integer, ForeignKey('users.user_id')) # User link
  475. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid')) # link JM2L_Year
  476. arrival_time = Column(DateTime)
  477. arrival_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  478. depart_time = Column(DateTime)
  479. depart_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  480. created = Column(DateTime, default=datetime.datetime.now)
  481. last_change = Column(DateTime, default=datetime.datetime.now)
  482. class Event(Base):
  483. __tablename__ = 'events'
  484. uid = Column(Integer, primary_key=True)
  485. salle_uid = Column(Integer, ForeignKey('salle.salle_id'))
  486. event_uid = Column(Integer)
  487. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid')) # link JM2L_Year
  488. name = Column(Unicode(100), nullable=False)
  489. slug = Column(Unicode(100))
  490. event_type = Column(Enum('Stand', 'Table ronde', 'Atelier', 'Concert', 'Conference', 'Repas'))
  491. start_time = Column(DateTime, default=datetime.datetime.now)
  492. end_time = Column(DateTime, default=datetime.datetime.now)
  493. description = Column(UnicodeText)
  494. created = Column(DateTime, default=datetime.datetime.now)
  495. last_change = Column(DateTime, default=datetime.datetime.now)
  496. intervenants = relationship(User,
  497. secondary='user_event_link',
  498. backref=backref('participate', uselist=False),
  499. lazy='dynamic')
  500. interventions = relationship(User_Event, backref="matching_events")
  501. Salle = relationship(Salles, backref='allevents')
  502. @classmethod
  503. def by_id(cls, uid):
  504. return DBSession.query(cls)\
  505. .filter(cls.uid == uid).first()
  506. @classmethod
  507. def by_slug(cls, slug, year=None):
  508. if not year is None:
  509. return DBSession.query(cls)\
  510. .filter(cls.for_year==year)\
  511. .filter(cls.slug == slug).first()
  512. else:
  513. return DBSession.query(cls)\
  514. .filter(cls.slug == slug).first()
  515. @property
  516. def video(self):
  517. return DBSession.query(Media) \
  518. .filter(Media.media_table == 'event') \
  519. .filter(Media.media_type == 'Video') \
  520. .filter(Media.link_id == self.uid)
  521. @property
  522. def presentation(self):
  523. return DBSession.query(Media) \
  524. .filter(Media.media_table == 'event') \
  525. .filter(Media.media_type == 'Pres') \
  526. .filter(Media.link_id == self.uid)
  527. @property
  528. def created_in_words(self):
  529. return time_ago_in_words(self.created)
  530. class Entry(Base):
  531. __tablename__ = 'entries'
  532. id = Column(Integer, primary_key=True)
  533. active = Column(Integer, default=True)
  534. title = Column(Unicode(255), unique=True, nullable=False)
  535. body = Column(UnicodeText, default=u'')
  536. created = Column(DateTime, default=datetime.datetime.now)
  537. edited = Column(DateTime, default=datetime.datetime.now)
  538. @classmethod
  539. def all(cls):
  540. return DBSession.query(Entry).order_by(sa.desc(Entry.created))
  541. @classmethod
  542. def by_id(cls, id):
  543. return DBSession.query(Entry).filter(Entry.id == id).first()
  544. @property
  545. def slug(self):
  546. return urlify(self.title)
  547. @property
  548. def created_in_words(self):
  549. return time_ago_in_words(self.created)
  550. @classmethod
  551. def get_paginator(cls, request, page=1):
  552. page_url = PageURL_WebOb(request)
  553. return Page(Entry.all(), page, url=page_url, items_per_page=5)
  554. #class Seances(Base):
  555. # __tablename__ = 'seances'
  556. def get_user(request):
  557. # the below line is just an example, use your own method of
  558. # accessing a database connection here (this could even be another
  559. # request property such as request.db, implemented using this same
  560. # pattern).
  561. userid = unauthenticated_userid(request)
  562. if userid is not None:
  563. # this should return None if the user doesn't exist
  564. # in the database
  565. return DBSession.query(User).filter(User.uid==userid).first()
  566. def get_sponsors(request, Year):
  567. if Year:
  568. return DBSession.query(Tiers)\
  569. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  570. .filter( Role_Tiers.tiers_role == 'Sponsor')\
  571. .filter( Role_Tiers.year_uid == Year)
  572. else:
  573. return DBSession.query(Tiers)\
  574. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  575. .filter( Role_Tiers.tiers_role == 'Sponsor')
  576. def get_exposants(request, Year):
  577. if Year:
  578. return DBSession.query(Tiers)\
  579. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  580. .filter( Role_Tiers.tiers_role == 'Exposant')\
  581. .filter( Role_Tiers.year_uid == Year)
  582. else:
  583. return DBSession.query(Tiers)\
  584. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  585. .filter( Role_Tiers.tiers_role == 'Exposant')