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.
 
 
 
 
 

196 lines
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=07)
  30. def mailer_tasks(config):
  31. # Send the Welcome Mail
  32. mailer = config.registry['mailer']
  33. Contact = DBSession.query(User).filter(User.uid==1).one()
  34. request = Request.blank('/', base_url='http://jm2l.linux-azur.org')
  35. request.registry = config.registry
  36. for StaffUser in DBSession.query(User).filter(User.Staff == True):
  37. # Skip mail to contact
  38. if StaffUser==Contact:
  39. continue
  40. # Skip those that have no task assigned
  41. if len(filter(lambda k:not k.closed, StaffUser.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=StaffUser, 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=StaffUser, 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=[StaffUser.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.add_subscriber(add_renderer_globals, BeforeRender)
  76. # config.registry['mailer'] = mailer_factory_from_settings(settings)
  77. config.registry['event_date'] = JM2L_Year.get_latest_jm2l_startdate()
  78. sched = BackgroundScheduler()
  79. sched.add_job(mailer_tasks, 'cron', day_of_week='fri', hour=18, args=[ config ])
  80. sched.start() # start the scheduler
  81. config.add_renderer('json', JSON(indent=4))
  82. config.add_renderer('jsonp', JSONP(param_name='callback'))
  83. config.set_session_factory(my_session_factory)
  84. config.add_request_method(get_user, 'user', reify=True)
  85. config.add_request_method(get_sponsors, 'sponsors', reify=False)
  86. config.add_request_method(get_exposants, 'exposants', reify=False)
  87. config.add_static_view('static', 'static', cache_max_age=3600)
  88. config.add_static_view('img', 'static/img', cache_max_age=3600)
  89. config.add_static_view('css', 'static/css', cache_max_age=3600)
  90. config.add_static_view('js', 'static/js', cache_max_age=3600)
  91. config.add_static_view('vendor', 'static/vendor', cache_max_age=3600)
  92. config.add_static_view('upload', 'upload', cache_max_age=3600)
  93. config.add_static_view('resources', 'resources', cache_max_age=3600)
  94. # ICal Routes
  95. config.add_route('progr_iCal', '/{year:\d+}/JM2L.ics')
  96. config.add_route('progr_dyn_iCal', '/{year:\d+}/JM2L_dyn.ics')
  97. # JSON Routes
  98. config.add_route('users_json', '/json-users')
  99. config.add_route('tiers_json', '/json-tiers')
  100. config.add_route('progr_json', '/{year:\d+}/le-prog-json')
  101. config.add_route('timeline_json', '/{year:\d+}/timeline-json')
  102. # Session setting Routes
  103. config.add_route('year', '/year/{year:\d+}')
  104. config.add_route('vote_logo', '/vote_logo/{num:\d+}')
  105. # HTML Routes - Staff
  106. config.add_route('Live', '/Live')
  107. config.add_route('list_expenses', '/{year:\d+}/Staff/compta')
  108. config.add_route('list_task', '/{year:\d+}/Staff')
  109. config.add_route('handle_pole', '/{year:\d+}/Staff/poles{sep:/*}{pole_id:(\d+)?}')
  110. config.add_route('handle_task', '/{year:\d+}/Staff/tasks{sep:/*}{task_id:(\d+)?}')
  111. config.add_route('action_task', '/{year:\d+}/Staff/{action:(\w+)}/{task_id:(\d+)}')
  112. config.add_route('action_task_area', '/{year:\d+}/Staff/pole/{action:(\w+)}/{pole_id:(\d+)}')
  113. config.add_route('list_salles', '/ListSalles')
  114. config.add_route('list_salles_phy', '/ListSallesPhy')
  115. config.add_route('handle_salle', '/Salles{sep:/*}{salle_id:(\d+)?}')
  116. config.add_route('handle_salle_phy', '/PhySalles{sep:/*}{salle_id:(\d+)?}')
  117. config.add_route('action_salle', '/Salles/{action:(\w+)}/{salle_id:(\d+)}')
  118. config.add_route('pict_salle', '/salle_picture/{salle_id:(\d+)}')
  119. config.add_route('list_users', '/{year:\d+}/ListParticipant')
  120. config.add_route('list_users_csv', '/{year:\d+}/ListParticipant.csv')
  121. config.add_route('list_orga', '/{year:\d+}/ListOrga')
  122. # HTML Routes - Public
  123. config.add_route('home', '/{year:(\d+/)?}')
  124. config.add_route('edit_index', '/{year:\d+}/edit')
  125. config.add_route('presse', '/{year:\d+}/dossier-de-presse')
  126. config.add_route('edit_presse', '/{year:\d+}/dossier-de-presse/edit')
  127. config.add_route('programme', '/{year:\d+}/le-programme')
  128. config.add_route('plan', 'nous-rejoindre')
  129. config.add_route('participer', 'participer-l-evenement')
  130. config.add_route('captcha', '/captcha')
  131. ## Events
  132. config.add_route('event', '/event/{year:\d+}/{event_id:([\w-]+)?}')
  133. config.add_route('link_event_user', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_user')
  134. config.add_route('delete_link_u', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/delete_link_user')
  135. config.add_route('link_event_tiers', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_tiers')
  136. config.add_route('delete_link_t', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/delete_link_tiers')
  137. config.add_route('edit_event', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}')
  138. config.add_route('delete_event', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}/delete')
  139. ## Entities
  140. config.add_route('entities', '/entities') #{sep:/*}{Nature:\w+?}')
  141. config.add_route('add_entity', '/entity')
  142. config.add_route('delete_entity', '/entity/{entity_id:(\d+)}/delete')
  143. config.add_route('show_entity', '/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)?}')
  144. config.add_route('edit_entity', '/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)}/edit')
  145. config.add_route('edit_entity_cat', '/categorie/entity')
  146. ## Users
  147. config.add_route('pict_user', '/user_picture')
  148. config.add_route('show_user', '/user/{user_slug:([\w-]+)?}')
  149. config.add_route('badge_user', '/user/{user_slug:([\w-]+)?}/badge')
  150. config.add_route('all_badges', '/badges')
  151. config.add_route('place_print', '/place_print')
  152. config.add_route('stand_print', '/stand_print')
  153. # HTML Routes - Logged
  154. #config.add_route('profil', 'MesJM2L')
  155. config.add_route('jm2l', '/MesJM2L')
  156. config.add_route('drop_sejour', '/DropSejour')
  157. config.add_route('miam', '/MonMiam')
  158. config.add_route('sejour', '/MonSejour')
  159. config.add_route('orga', '/MonOrga')
  160. config.add_route('modal', '/{year:\d+}/modal/{modtype:\w+}/{id:(\d+)}')
  161. # Handle exchanges
  162. config.add_route('exchange', '/{year:\d+}/exchange/{modtype:\w+}/{id:(\d+)}/{action:\w+}')
  163. # Handle authentication
  164. config.add_route('register', '/register')
  165. config.add_route('auth', '/sign/{action}')
  166. config.add_route('bymail', '/sign/jm2l/{hash}')
  167. # Handle Multimedia and Uploads
  168. config.add_route('media_view', '/image/{media_table:\w+}/{uid:\d+}/{name:.+}')
  169. config.add_route('media_upload', '/uploader/{media_table:\w+}/{uid:\d+}/proceed{sep:/*}{name:.*}')
  170. config.scan()
  171. return config.make_wsgi_app()