Le repo des sources pour le site web des JM2L
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

673 行
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, uid):
  228. return DBSession.query(cls).filter(cls.uid == uid).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. @property
  304. def PhotosLinks(self):
  305. from .upload import MediaPath
  306. return MediaPath().get_list('salle', self.uid, 'Image')
  307. class Salles(Base):
  308. __tablename__ = 'salle'
  309. salle_id = Column(Integer, primary_key=True)
  310. phy_salle_id = Column(Integer, ForeignKey('phy_salle.uid'))
  311. year_uid = Column(Integer, ForeignKey('jm2l_year.year_uid'), default=CurrentYear)
  312. name = Column(Unicode(40))
  313. place_type = Column(Enum('Conference', 'Stand', 'Ateliers', 'Autres'))
  314. description = Column(UnicodeText) # Description du matériel disponible
  315. created = Column(DateTime, default=datetime.datetime.now)
  316. last_change = Column(DateTime, default=datetime.datetime.now)
  317. phy = relationship(SallePhy)
  318. @classmethod
  319. def by_id(cls, uid):
  320. return DBSession.query(cls).filter(cls.salle_id == uid).first()
  321. class Place(Base):
  322. __tablename__ = 'place'
  323. place_id = Column(Integer, primary_key=True)
  324. usage = Column(Boolean, default=False) # By Default / Extended
  325. place_type = Column(Enum('Aeroport', 'Gare', 'JM2L', \
  326. 'Hotel', 'Habitant', 'Restaurant', 'Autres'))
  327. display_name = Column(Unicode(20))
  328. name = Column(Unicode(80))
  329. slug = Column(Unicode(80))
  330. specific = Column(Unicode(80)) # eg Terminal 2
  331. gps_coord = Column(Unicode(30))
  332. adresse = Column(Unicode(100))
  333. codePostal = Column(Unicode(5))
  334. ville = Column(Unicode(40))
  335. website = Column(Unicode(100))
  336. description = Column(UnicodeText)
  337. created_by = Column(Integer, ForeignKey('users.user_id'))
  338. created = Column(DateTime, default=datetime.datetime.now)
  339. last_change = Column(DateTime, default=datetime.datetime.now)
  340. @classmethod
  341. def by_id(cls, uid):
  342. return DBSession.query(cls).filter(cls.place_id == uid).first()
  343. @classmethod
  344. def get_list(cls, All=False):
  345. if All:
  346. return DBSession.query(cls).all()
  347. else:
  348. return DBSession.query(cls).filter(cls.usage==True).all()
  349. class Itineraire(Base):
  350. __tablename__ = 'itineraire'
  351. itin_id = Column(Integer, primary_key=True)
  352. start_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  353. arrival_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  354. distance = Column(Integer)
  355. duration = Column(Integer)
  356. price = Column(Integer)
  357. tr_pied = Column(Boolean, default=False)
  358. tr_velo = Column(Boolean, default=False)
  359. tr_moto = Column(Boolean, default=False)
  360. tr_voiture = Column(Boolean, default=False)
  361. tr_bus = Column(Boolean, default=False)
  362. tr_taxi = Column(Boolean, default=False)
  363. tr_avion = Column(Boolean, default=False)
  364. description = Column(UnicodeText)
  365. created_by = Column(Integer, ForeignKey('users.user_id')) # User link
  366. created = Column(DateTime, default=datetime.datetime.now)
  367. last_change = Column(DateTime, default=datetime.datetime.now)
  368. # relations
  369. start = relationship(Place, foreign_keys=[start_place])
  370. arrival = relationship(Place, foreign_keys=[arrival_place])
  371. class Exchange_Cat(Base):
  372. __tablename__ = 'exchange_category'
  373. cat_id = Column(Integer, primary_key=True)
  374. exch_type = Column(Enum('H', 'C', 'M')) # Heberg, Co-voit, Materiel
  375. exch_subtype = Column(Unicode(80))
  376. description = Column(UnicodeText)
  377. class Exchange(Base):
  378. __tablename__ = 'exchanges'
  379. exch_id = Column(Integer, primary_key=True)
  380. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid')) # link JM2L_Year
  381. exch_done = Column(Boolean, default=False)
  382. exch_state = Column(Enum('Ask', 'Proposal'))
  383. exch_type = Column(Enum('H', 'C', 'M')) # Heberg, Co-Voit, Materiel
  384. exch_categ = Column(Integer, ForeignKey('exchange_category.cat_id')) # Exchange_Cat link
  385. # Users
  386. asker_id = Column(Integer, ForeignKey('users.uid')) # User link
  387. provider_id = Column(Integer, ForeignKey('users.uid')) # User link
  388. start_time = Column(DateTime, default=datetime.datetime.now)
  389. end_time = Column(DateTime, default=datetime.datetime.now)
  390. # Co-voiturage
  391. itin_id = Column(Integer, ForeignKey('itineraire.itin_id')) # Itineraire link
  392. # Hebergement
  393. place_id = Column(Integer, ForeignKey('place.place_id')) # Place link
  394. # Materiel
  395. duration = Column(Integer)
  396. description = Column(UnicodeText)
  397. pictures = Column(Unicode(80))
  398. created_by = Column(Integer) # User link
  399. created = Column(DateTime, default=datetime.datetime.now)
  400. last_change = Column(DateTime, default=datetime.datetime.now)
  401. # relations
  402. Category = relationship(Exchange_Cat, backref="exchanges")
  403. Itin = relationship(Itineraire, backref="exchanged")
  404. asker = relationship(User, foreign_keys=[asker_id], backref="asked")
  405. provider = relationship(User, foreign_keys=[provider_id], backref="provided")
  406. @classmethod
  407. def by_id(cls, id):
  408. return DBSession.query(cls).filter(cls.exch_id == id).first()
  409. @classmethod
  410. def get_counters(cls):
  411. return DBSession.query(cls.exch_state, cls.exch_type, cls.exch_done, func.count(cls.exch_id))\
  412. .filter(cls.for_year==2015)\
  413. .group_by(cls.exch_state, cls.exch_type, cls.exch_done)
  414. @classmethod
  415. def get_my_counters(cls, uid):
  416. return DBSession.query(cls.exch_state, cls.exch_type, cls.exch_done, func.count(cls.exch_id))\
  417. .filter(cls.for_year==2015)\
  418. .filter( or_(cls.asker_id==uid, cls.provider_id==uid) )\
  419. .group_by(cls.exch_state, cls.exch_type, cls.exch_done)
  420. @classmethod
  421. def get_overview(cls, uid):
  422. # Build a Dic with all exchange to save database access
  423. DicResult= {}
  424. for extype in ['F','C','H','M']:
  425. DicResult[extype] = {}
  426. for exstate in ['Ask','Proposal','Missing','Agree']:
  427. DicResult[extype][exstate]=[]
  428. DicResult[extype]['Counters']={'AllAsk':0, 'AllProp':0, 'AllAgree':0}
  429. Query = DBSession.query(cls)\
  430. .filter(cls.for_year==2015)\
  431. .order_by(cls.start_time).all()
  432. for item in Query:
  433. if item.exch_done:
  434. DicResult[item.exch_type]['Counters']['AllAgree']+=1
  435. if item.exch_state=='Ask':
  436. DicResult[item.exch_type]['Counters']['AllAsk']+=1
  437. if item.exch_state=='Proposal':
  438. DicResult[item.exch_type]['Counters']['AllProp']+=1
  439. if item.asker_id==uid or item.provider_id==uid:
  440. if item.asker_id==uid and item.exch_state=='Ask':
  441. DicResult[item.exch_type]['Ask'].append(item)
  442. if item.provider_id==uid and item.exch_state=='Ask':
  443. DicResult[item.exch_type]['Proposal'].append(item)
  444. if item.asker_id==uid and item.exch_state=='Proposal':
  445. DicResult[item.exch_type]['Ask'].append(item)
  446. if item.provider_id==uid and item.exch_state=='Proposal':
  447. DicResult[item.exch_type]['Proposal'].append(item)
  448. if item.exch_done:
  449. DicResult[item.exch_type]['Agree'].append(item)
  450. else:
  451. DicResult[item.exch_type]['Missing'].append(item)
  452. return DicResult
  453. @classmethod
  454. def get_pub_list(cls, exch_type):
  455. return DBSession.query(cls).filter(cls.for_year==2015 and exch_state in ['Ask','Proposal'])\
  456. .filter(cls.exch_type=='%s' % exch_type)\
  457. .filter(cls.exch_done==False)\
  458. .all()
  459. @classmethod
  460. def get_my_list(cls, uid, exch_type):
  461. DicResult = {}
  462. DicResult['Ask']=DBSession.query(cls)\
  463. .filter(cls.for_year==2015)\
  464. .filter( or_(cls.asker_id==uid, cls.provider_id==uid) )\
  465. .filter(cls.exch_type=='%s' % exch_type)\
  466. .filter(cls.exch_state=='Ask')\
  467. .order_by(cls.start_time).all()
  468. DicResult['Proposal']=DBSession.query(cls)\
  469. .filter(cls.for_year==2015)\
  470. .filter( or_(cls.asker_id==uid, cls.provider_id==uid) )\
  471. .filter(cls.exch_type=='%s' % exch_type)\
  472. .filter(cls.exch_state=='Proposal')\
  473. .order_by(cls.start_time).all()
  474. return DicResult
  475. class Sejour(Base):
  476. __tablename__ = 'sejour'
  477. sej_id = Column(Integer, primary_key=True)
  478. user_id = Column(Integer, ForeignKey('users.user_id')) # User link
  479. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid')) # link JM2L_Year
  480. arrival_time = Column(DateTime)
  481. arrival_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  482. depart_time = Column(DateTime)
  483. depart_place = Column(Integer, ForeignKey('place.place_id')) # Place link
  484. created = Column(DateTime, default=datetime.datetime.now)
  485. last_change = Column(DateTime, default=datetime.datetime.now)
  486. class Event(Base):
  487. __tablename__ = 'events'
  488. uid = Column(Integer, primary_key=True)
  489. salle_uid = Column(Integer, ForeignKey('salle.salle_id'))
  490. event_uid = Column(Integer)
  491. for_year = Column(Integer, ForeignKey('jm2l_year.year_uid')) # link JM2L_Year
  492. name = Column(Unicode(100), nullable=False)
  493. slug = Column(Unicode(100))
  494. event_type = Column(Enum('Stand', 'Table ronde', 'Atelier', 'Concert', 'Conference', 'Repas'))
  495. start_time = Column(DateTime, default=datetime.datetime.now)
  496. end_time = Column(DateTime, default=datetime.datetime.now)
  497. description = Column(UnicodeText)
  498. created = Column(DateTime, default=datetime.datetime.now)
  499. last_change = Column(DateTime, default=datetime.datetime.now)
  500. intervenants = relationship(User,
  501. secondary='user_event_link',
  502. backref=backref('participate', uselist=False),
  503. lazy='dynamic')
  504. interventions = relationship(User_Event, backref="matching_events")
  505. Salle = relationship(Salles, backref='allevents')
  506. @classmethod
  507. def by_id(cls, uid):
  508. return DBSession.query(cls)\
  509. .filter(cls.uid == uid).first()
  510. @classmethod
  511. def by_slug(cls, slug, year=None):
  512. if not year is None:
  513. return DBSession.query(cls)\
  514. .filter(cls.for_year==year)\
  515. .filter(cls.slug == slug).first()
  516. else:
  517. return DBSession.query(cls)\
  518. .filter(cls.slug == slug).first()
  519. @property
  520. def video(self):
  521. return DBSession.query(Media) \
  522. .filter(Media.media_table == 'event') \
  523. .filter(Media.media_type == 'Video') \
  524. .filter(Media.link_id == self.uid)
  525. @property
  526. def presentation(self):
  527. return DBSession.query(Media) \
  528. .filter(Media.media_table == 'event') \
  529. .filter(Media.media_type == 'Pres') \
  530. .filter(Media.link_id == self.uid)
  531. @property
  532. def created_in_words(self):
  533. return time_ago_in_words(self.created)
  534. class Entry(Base):
  535. __tablename__ = 'entries'
  536. id = Column(Integer, primary_key=True)
  537. active = Column(Integer, default=True)
  538. title = Column(Unicode(255), unique=True, nullable=False)
  539. body = Column(UnicodeText, default=u'')
  540. created = Column(DateTime, default=datetime.datetime.now)
  541. edited = Column(DateTime, default=datetime.datetime.now)
  542. @classmethod
  543. def all(cls):
  544. return DBSession.query(Entry).order_by(sa.desc(Entry.created))
  545. @classmethod
  546. def by_id(cls, id):
  547. return DBSession.query(Entry).filter(Entry.id == id).first()
  548. @property
  549. def slug(self):
  550. return urlify(self.title)
  551. @property
  552. def created_in_words(self):
  553. return time_ago_in_words(self.created)
  554. @classmethod
  555. def get_paginator(cls, request, page=1):
  556. page_url = PageURL_WebOb(request)
  557. return Page(Entry.all(), page, url=page_url, items_per_page=5)
  558. #class Seances(Base):
  559. # __tablename__ = 'seances'
  560. def get_user(request):
  561. # the below line is just an example, use your own method of
  562. # accessing a database connection here (this could even be another
  563. # request property such as request.db, implemented using this same
  564. # pattern).
  565. userid = unauthenticated_userid(request)
  566. if userid is not None:
  567. # this should return None if the user doesn't exist
  568. # in the database
  569. return DBSession.query(User).filter(User.uid==userid).first()
  570. def get_sponsors(request, Year):
  571. if Year:
  572. return DBSession.query(Tiers)\
  573. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  574. .filter( Role_Tiers.tiers_role == 'Sponsor')\
  575. .filter( Role_Tiers.year_uid == Year)
  576. else:
  577. return DBSession.query(Tiers)\
  578. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  579. .filter( Role_Tiers.tiers_role == 'Sponsor')
  580. def get_exposants(request, Year):
  581. if Year:
  582. return DBSession.query(Tiers)\
  583. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  584. .filter( Role_Tiers.tiers_role == 'Exposant')\
  585. .filter( Role_Tiers.year_uid == Year)
  586. else:
  587. return DBSession.query(Tiers)\
  588. .join(Role_Tiers, Role_Tiers.tiers_uid == Tiers.uid )\
  589. .filter( Role_Tiers.tiers_role == 'Exposant')