Le repo des sources pour le site web des JM2L
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

198 righe
9.4 KiB

  1. # -*- coding: utf8 -*-
  2. from pyramid.authentication import AuthTktAuthenticationPolicy
  3. from pyramid.authorization import ACLAuthorizationPolicy
  4. from pyramid.config import Configurator
  5. from pyramid.renderers import JSON, JSONP
  6. from pyramid.session import SignedCookieSessionFactory
  7. from pyramid.events import BeforeRender
  8. from sqlalchemy import engine_from_config
  9. from pyramid.renderers import render_to_response
  10. from .models import DBSession, get_user, get_sponsors, get_exposants
  11. from .security import EntryFactory, groupfinder
  12. from pyramid_mailer import mailer_factory_from_settings
  13. from pyramid_mailer.message import Attachment, Message
  14. import locale
  15. from .helpers import Sejour_helpers, Orga_helpers
  16. from apscheduler.schedulers.background import BackgroundScheduler
  17. # Database access imports
  18. from pyramid.request import Request
  19. from mako.template import Template
  20. from .models import User
  21. from jm2l.const import CurrentYear
  22. from .models import JM2L_Year
  23. import logging
  24. def add_renderer_globals(event):
  25. event['mytrip'] = Sejour_helpers(event)
  26. event['myorga'] = Orga_helpers(event)
  27. event['SelectedYear'] = CurrentYear
  28. event['CurrentYear'] = CurrentYear
  29. # @sched.scheduled_job('cron', day_of_week='sun', hour=22, minute=7)
  30. def mailer_tasks(config):
  31. # Send the Welcome Mail
  32. contact = DBSession.query(User).filter(User.uid == 1).one()
  33. request = Request.blank('/', base_url='http://jm2l.linux-azur.org')
  34. request.registry = config.registry
  35. mailer = request.mailer
  36. for staff_user in DBSession.query(User).filter(User.Staff is True):
  37. # Skip mail to contact
  38. if staff_user == contact:
  39. continue
  40. # Skip those that have no task assigned
  41. if len(filter(lambda k: not k.closed, staff_user.task_assoc)) == 0:
  42. continue
  43. # Prepare Plain Text Message :
  44. mail_template = Template(filename='jm2l/templates/mail_plain.mako')
  45. mail_plain = mail_template.render(request=request, User=staff_user, Contact=contact, action="Tasks")
  46. # Prepare HTML Message :
  47. mail_template = Template(filename='jm2l/templates/mail_html.mako')
  48. mail_html = mail_template.render(request=request, User=staff_user, Contact=contact, action="Tasks")
  49. # Prepare Message
  50. message = Message(subject="[JM2L] Le mail de rappel pour les JM2L !",
  51. sender="contact@jm2l.linux-azur.org",
  52. recipients=[staff_user.mail],
  53. body=mail_plain, html=mail_html)
  54. message.add_bcc("spam@style-python.fr")
  55. mailer.send_immediately(message)
  56. def main(global_config, **settings):
  57. """ This function returns a Pyramid WSGI application.
  58. """
  59. # locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
  60. locale.setlocale(locale.LC_ALL, "fr_FR.utf8")
  61. engine = engine_from_config(settings, 'sqlalchemy.')
  62. DBSession.configure(bind=engine)
  63. # Extract secrets from configuration file if any
  64. CookiesPasswd = settings.get('secret_Cookies', 'itsthefirstseekreet')
  65. AuthTktPasswd = settings.get('secret_AuthTkt', 'itsthesecondseekreet')
  66. my_session_factory = SignedCookieSessionFactory(CookiesPasswd)
  67. authentication_policy = AuthTktAuthenticationPolicy(AuthTktPasswd,
  68. callback=groupfinder, hashalg='sha512', debug=True)
  69. authorization_policy = ACLAuthorizationPolicy()
  70. config = Configurator(settings=settings,
  71. root_factory='.security.RootFactory',
  72. authentication_policy=authentication_policy,
  73. authorization_policy=authorization_policy
  74. )
  75. config.include('pyramid_mailer')
  76. config.add_subscriber(add_renderer_globals, BeforeRender)
  77. print(settings)
  78. # config.registry['mailer'] = mailer_factory_from_settings(settings)
  79. config.registry['event_date'] = JM2L_Year.get_latest_jm2l_startdate()
  80. sched = BackgroundScheduler()
  81. sched.add_job(mailer_tasks, 'cron', day_of_week='fri', hour=18, args=[config])
  82. sched.start() # start the scheduler
  83. config.add_renderer('json', JSON(indent=4))
  84. config.add_renderer('jsonp', JSONP(param_name='callback'))
  85. config.set_session_factory(my_session_factory)
  86. config.add_request_method(get_user, 'user', reify=True)
  87. config.add_request_method(get_sponsors, 'sponsors', reify=False)
  88. config.add_request_method(get_exposants, 'exposants', reify=False)
  89. config.add_static_view('static', 'static', cache_max_age=3600)
  90. config.add_static_view('img', 'static/img', cache_max_age=3600)
  91. config.add_static_view('css', 'static/css', cache_max_age=3600)
  92. config.add_static_view('js', 'static/js', cache_max_age=3600)
  93. config.add_static_view('vendor', 'static/vendor', cache_max_age=3600)
  94. config.add_static_view('upload', 'upload', cache_max_age=3600)
  95. config.add_static_view('resources', 'resources', cache_max_age=3600)
  96. # ICal Routes
  97. config.add_route('progr_iCal', r'/{year:\d+}/JM2L.ics')
  98. config.add_route('progr_dyn_iCal', r'/{year:\d+}/JM2L_dyn.ics')
  99. # JSON Routes
  100. config.add_route('users_json', '/json-users')
  101. config.add_route('tiers_json', '/json-tiers')
  102. config.add_route('progr_json', r'/{year:\d+}/le-prog-json')
  103. config.add_route('timeline_json', r'/{year:\d+}/timeline-json')
  104. # Session setting Routes
  105. config.add_route('year', r'/year/{year:\d+}')
  106. config.add_route('vote_logo', r'/vote_logo/{num:\d+}')
  107. # HTML Routes - Staff
  108. config.add_route('Live', '/Live')
  109. config.add_route('list_expenses', r'/{year:\d+}/Staff/compta')
  110. config.add_route('list_task', r'/{year:\d+}/Staff')
  111. config.add_route('handle_pole', r'/{year:\d+}/Staff/poles{sep:/*}{pole_id:(\d+)?}')
  112. config.add_route('handle_task', r'/{year:\d+}/Staff/tasks{sep:/*}{task_id:(\d+)?}')
  113. config.add_route('action_task', r'/{year:\d+}/Staff/{action:(\w+)}/{task_id:(\d+)}')
  114. config.add_route('action_task_area', r'/{year:\d+}/Staff/pole/{action:(\w+)}/{pole_id:(\d+)}')
  115. config.add_route('list_salles', '/ListSalles')
  116. config.add_route('list_salles_phy', '/ListSallesPhy')
  117. config.add_route('handle_salle', r'/Salles{sep:/*}{salle_id:(\d+)?}')
  118. config.add_route('handle_salle_phy', r'/PhySalles{sep:/*}{salle_id:(\d+)?}')
  119. config.add_route('action_salle', r'/Salles/{action:(\w+)}/{salle_id:(\d+)}')
  120. config.add_route('pict_salle', r'/salle_picture/{salle_id:(\d+)}')
  121. config.add_route('list_users', r'/{year:\d+}/ListParticipant')
  122. config.add_route('list_users_csv', r'/{year:\d+}/ListParticipant.csv')
  123. config.add_route('list_orga', r'/{year:\d+}/ListOrga')
  124. # HTML Routes - Public
  125. config.add_route('home', r'/{year:(\d+/)?}')
  126. config.add_route('edit_index', r'/{year:\d+}/edit')
  127. config.add_route('presse', r'/{year:\d+}/dossier-de-presse')
  128. config.add_route('edit_presse', r'/{year:\d+}/dossier-de-presse/edit')
  129. config.add_route('programme', r'/{year:\d+}/le-programme')
  130. config.add_route('plan', 'nous-rejoindre')
  131. config.add_route('participer', 'participer-l-evenement')
  132. config.add_route('captcha', '/captcha')
  133. ## Events
  134. config.add_route('event', r'/event/{year:\d+}/{event_id:([\w-]+)?}')
  135. config.add_route('link_event_user', r'/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_user')
  136. config.add_route('delete_link_u', r'/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/delete_link_user')
  137. config.add_route('link_event_tiers', r'/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_tiers')
  138. config.add_route('delete_link_t', r'/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/delete_link_tiers')
  139. config.add_route('edit_event', r'/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}')
  140. config.add_route('delete_event', r'/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}/delete')
  141. # Entities
  142. config.add_route('entities', '/entities') # {sep:/*}{Nature:\w+?}')
  143. config.add_route('add_entity', '/entity')
  144. config.add_route('delete_entity', r'/entity/{entity_id:(\d+)}/delete')
  145. config.add_route('show_entity', r'/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)?}')
  146. config.add_route('edit_entity', r'/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)}/edit')
  147. config.add_route('edit_entity_cat', '/categorie/entity')
  148. # Users
  149. config.add_route('pict_user', '/user_picture')
  150. config.add_route('show_user', r'/user/{user_slug:([\w-]+)?}')
  151. config.add_route('badge_user', r'/user/{user_slug:([\w-]+)?}/badge')
  152. config.add_route('all_badges', '/badges')
  153. config.add_route('place_print', '/place_print')
  154. config.add_route('stand_print', '/stand_print')
  155. # HTML Routes - Logged
  156. # config.add_route('profil', 'MesJM2L')
  157. config.add_route('jm2l', '/MesJM2L')
  158. config.add_route('drop_sejour', '/DropSejour')
  159. config.add_route('miam', '/MonMiam')
  160. config.add_route('sejour', '/MonSejour')
  161. config.add_route('orga', '/MonOrga')
  162. config.add_route('modal', r'/{year:\d+}/modal/{modtype:\w+}/{id:(\d+)}')
  163. # Handle exchanges
  164. config.add_route('exchange', r'/{year:\d+}/exchange/{modtype:\w+}/{id:(\d+)}/{action:\w+}')
  165. # Handle authentication
  166. config.add_route('register', '/register')
  167. config.add_route('auth', '/sign/{action}')
  168. config.add_route('bymail', '/sign/jm2l/{hash}')
  169. # Handle Multimedia and Uploads
  170. config.add_route('media_view', r'/image/{media_table:\w+}/{uid:\d+}/{name:.+}')
  171. config.add_route('media_upload', r'/uploader/{media_table:\w+}/{uid:\d+}/proceed{sep:/*}{name:.*}')
  172. config.scan()
  173. return config.make_wsgi_app()