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.
 
 
 
 
 

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