Le repo des sources pour le site web des JM2L
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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