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.
 
 
 
 
 

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