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.
 
 
 
 
 

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