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.
 
 
 
 
 

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