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.
 
 
 
 
 

176 lines
8.2 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
  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. import logging
  22. def add_renderer_globals(event):
  23. event['mytrip'] = Sejour_helpers(event)
  24. #@sched.scheduled_job('cron', day_of_week='sun', hour=22, minute=07)
  25. def mailer_tasks(config):
  26. # Send the Welcome Mail
  27. mailer = config.registry['mailer']
  28. Contact = DBSession.query(User).filter(User.uid==1).one()
  29. request = Request.blank('/', base_url='http://jm2l.linux-azur.org')
  30. request.registry = config.registry
  31. for StaffUser in DBSession.query(User).filter(User.Staff == True):
  32. # Skip mail to contact
  33. if StaffUser==Contact:
  34. continue
  35. # Skip those that have no task assigned
  36. if len(filter(lambda k:not k.closed, StaffUser.task_assoc))==0:
  37. continue
  38. # Prepare Plain Text Message :
  39. Mail_template = Template(filename='jm2l/templates/mail_plain.mako')
  40. mail_plain = Mail_template.render(request=request, User=StaffUser, Contact=Contact, action="Tasks", output_encoding='ascii',)
  41. # Prepare HTML Message :
  42. Mail_template = Template(filename='jm2l/templates/mail_html.mako')
  43. mail_html = Mail_template.render(request=request, User=StaffUser, Contact=Contact, action="Tasks", output_encoding='ascii',)
  44. # Prepare Message
  45. message = Message(subject="[JM2L] Rappel des Tâches pour les JM2L",
  46. sender="contact@jm2l.linux-azur.org",
  47. recipients=[StaffUser.mail],
  48. body=mail_plain, html=mail_html)
  49. message.add_bcc("spam@style-python.fr")
  50. mailer.send(message)
  51. def main(global_config, **settings):
  52. """ This function returns a Pyramid WSGI application.
  53. """
  54. locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
  55. engine = engine_from_config(settings, 'sqlalchemy.')
  56. DBSession.configure(bind=engine)
  57. # Extract secrets from configuration file if any
  58. CookiesPasswd = settings.get('secret_Cookies', 'itsthefirstseekreet')
  59. AuthTktPasswd = settings.get('secret_AuthTkt', 'itsthesecondseekreet')
  60. my_session_factory = SignedCookieSessionFactory(CookiesPasswd)
  61. authentication_policy = AuthTktAuthenticationPolicy(AuthTktPasswd,
  62. callback=groupfinder, hashalg='sha512', debug=True)
  63. authorization_policy = ACLAuthorizationPolicy()
  64. config = Configurator(settings=settings,
  65. root_factory='.security.RootFactory',
  66. authentication_policy=authentication_policy,
  67. authorization_policy=authorization_policy
  68. )
  69. config.add_subscriber(add_renderer_globals, BeforeRender)
  70. config.registry['mailer'] = mailer_factory_from_settings(settings)
  71. sched = BackgroundScheduler()
  72. #sched.add_job(mailer_tasks, 'interval', minutes=1, args=[ config ])
  73. sched.add_job(mailer_tasks, 'cron', day_of_week='fri', hour=18, args=[ config ])
  74. sched.start() # start the scheduler
  75. config.add_renderer('json', JSON(indent=4))
  76. config.add_renderer('jsonp', JSONP(param_name='callback'))
  77. config.set_session_factory(my_session_factory)
  78. config.add_request_method(get_user, 'user', reify=True)
  79. config.add_request_method(get_sponsors, 'sponsors', reify=False)
  80. config.add_request_method(get_exposants, 'exposants', reify=False)
  81. config.add_static_view('static', 'static', cache_max_age=3600)
  82. config.add_static_view('img', 'static/img', cache_max_age=3600)
  83. config.add_static_view('css', 'static/css', cache_max_age=3600)
  84. config.add_static_view('js', 'static/js', cache_max_age=3600)
  85. config.add_static_view('vendor', 'static/vendor', cache_max_age=3600)
  86. config.add_static_view('upload', 'upload', cache_max_age=3600)
  87. config.add_static_view('resources', 'resources', cache_max_age=3600)
  88. # ICal Routes
  89. config.add_route('progr_iCal', '/{year:\d+}/JM2L.ics')
  90. # JSON Routes
  91. config.add_route('users_json', '/json-users')
  92. config.add_route('tiers_json', '/json-tiers')
  93. config.add_route('progr_json', '/{year:\d+}/le-prog-json')
  94. config.add_route('timeline_json', '/{year:\d+}/timeline-json')
  95. # Session setting Routes
  96. config.add_route('year', '/year/{year:\d+}')
  97. config.add_route('vote_logo', '/vote_logo/{num:\d+}')
  98. # HTML Routes - Staff
  99. config.add_route('list_task', '/Staff')
  100. config.add_route('handle_pole', '/Staff/poles{sep:/*}{pole_id:(\d+)?}')
  101. config.add_route('handle_task', '/Staff/tasks{sep:/*}{task_id:(\d+)?}')
  102. config.add_route('action_task', '/Staff/{action:(\w+)}/{task_id:(\d+)}')
  103. config.add_route('action_task_area', '/Staff/pole/{action:(\w+)}/{pole_id:(\d+)}')
  104. config.add_route('list_salles', '/ListSalles')
  105. config.add_route('handle_salle', '/Salles{sep:/*}{salle_id:(\d+)?}')
  106. config.add_route('handle_salle_phy', '/PhySalles{sep:/*}{salle_id:(\d+)?}')
  107. config.add_route('action_salle', '/Salles/{action:(\w+)}/{salle_id:(\d+)}')
  108. config.add_route('pict_salle', '/salle_picture/{salle_id:(\d+)}')
  109. config.add_route('list_users', '/ListParticipant')
  110. # HTML Routes - Public
  111. config.add_route('home', '/{year:(\d+/)?}')
  112. config.add_route('edit_index', '/{year:\d+}/edit')
  113. config.add_route('presse', '/{year:\d+}/dossier-de-presse')
  114. config.add_route('edit_presse', '/{year:\d+}/dossier-de-presse/edit')
  115. config.add_route('programme', '/{year:\d+}/le-programme')
  116. config.add_route('plan', 'nous-rejoindre')
  117. config.add_route('participer', 'participer-l-evenement')
  118. config.add_route('captcha', '/captcha')
  119. ## Events
  120. config.add_route('event', '/event/{year:\d+}/{event_id:([\w-]+)?}')
  121. config.add_route('link_event_user', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_user')
  122. config.add_route('link_event_tiers', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}/link_tiers')
  123. config.add_route('edit_event', '/MesJM2L/{year:\d+}/{intervention:[\s\w]+}{sep:/*}{event_id:([\w-]+)?}')
  124. ## Entities
  125. config.add_route('entities', '/entities') #{sep:/*}{Nature:\w+?}')
  126. config.add_route('add_entity', '/entity')
  127. config.add_route('delete_entity', '/entity/{entity_id:(\d+)}/delete')
  128. config.add_route('show_entity', '/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)?}')
  129. config.add_route('edit_entity', '/entity/{tiers_type:(\w+)}/{entity_id:([\w-]+)}/edit')
  130. config.add_route('edit_entity_cat', '/categorie/entity')
  131. ## Users
  132. config.add_route('pict_user', '/user_picture')
  133. config.add_route('show_user', '/user/{user_slug:([\w-]+)?}')
  134. # HTML Routes - Logged
  135. #config.add_route('profil', 'MesJM2L')
  136. config.add_route('jm2l', '/MesJM2L')
  137. config.add_route('miam', '/MonMiam')
  138. config.add_route('sejour', '/MonSejour')
  139. config.add_route('modal', '/{year:\d+}/modal/{modtype:\w+}/{id:(\d+)}')
  140. # Handle exchanges
  141. config.add_route('exchange', '/{year:\d+}/exchange/{modtype:\w+}/{id:(\d+)}/{action:\w+}')
  142. # Handle authentication
  143. config.add_route('register', '/register')
  144. config.add_route('auth', '/sign/{action}')
  145. config.add_route('bymail', '/sign/jm2l/{hash}')
  146. # Handle Multimedia and Uploads
  147. config.add_route('media_view', '/image/{media_table:\w+}/{uid:\d+}/{name:.+}')
  148. config.add_route('media_upload', '/uploader/{media_table:\w+}/{uid:\d+}/proceed{sep:/*}{name:.*}')
  149. config.scan()
  150. return config.make_wsgi_app()