Le repo des sources pour le site web des JM2L
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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