TG-60 : AJout de l'insertion de paires etudiant/tuteur + envoi de mails + templates de mails
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,2 +1 @@ | ||||
| .idea | ||||
|  | ||||
|   | ||||
| @@ -95,4 +95,16 @@ PUT -> Modify an existing group | ||||
| GET -> Getting specified group infos | ||||
|     In: (Suffixes = /bygid/<GROUP_ID> | /byname/<GROUP_NAME> ) | ||||
|     Out: | ||||
|         200 -> GROUP = <GROUP_OBJECT>|null : Dictionary containing group infos or null | ||||
|         200 -> GROUP = <GROUP_OBJECT>|null : Dictionary containing group infos or null | ||||
|  | ||||
| OPTIONS -> Add pairs of users (student/tutor) to the group | ||||
|     In: | ||||
|         pairs -> Table of pairs student's uid/tutor's uid (ex: [[1,2],[3,2]]) | ||||
|     Out: | ||||
|         200 -> RESULT = "Pairs added successfully" | ||||
|         400 -> ERROR = "One or more parameters are missing !" : Bad request | ||||
|         400 -> ERROR = "The user with id <USER_ID> doesn't exists !" : The given USER_ID for student or tutor is not found | ||||
|         400 -> ERROR = "A student must have the 'student' role !" : The given USER_ID for student doesn't have the "student" role (4) | ||||
|         400 -> ERROR = "A student can't be a tutor !" : The given USER_ID for tutor have the "student" role (4) and so can't be a tutor | ||||
|         405 -> ERROR = "This group doesn't exists !" : Bad GROUP_ID provided | ||||
|         409 -> ERROR = "Pairs are incorrectly formed !" : Bad syntax in pairs table | ||||
|   | ||||
							
								
								
									
										1
									
								
								backend/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								backend/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -31,3 +31,4 @@ coverage.xml | ||||
|  | ||||
| #Config files | ||||
| config.py | ||||
| /app/OLA_RESSOURCES/ | ||||
|   | ||||
| @@ -2,9 +2,9 @@ import os | ||||
|  | ||||
| from flask_restful import Resource, request | ||||
|  | ||||
| from app.core import app | ||||
| from app.api import mailsModels | ||||
| from app.model import * | ||||
| from app.utils import checkParams | ||||
| from app.utils import * | ||||
|  | ||||
|  | ||||
| class GroupAPI(Resource): | ||||
| @@ -97,7 +97,7 @@ class GroupAPI(Resource): | ||||
|         query = GROUP.update().values(name=name, year=year, class_short=class_short, class_long=class_long, | ||||
|                                       department=department, resp_id=resp_id, sec_id=sec_id, ressources_dir=res_dir) \ | ||||
|             .where(GROUP.c.id == gid) | ||||
|         res = query.execute() | ||||
|         query.execute() | ||||
|  | ||||
|         if group["ressources_dir"] != res_dir: | ||||
|             os.rename(group["ressources_dir"], res_dir) | ||||
| @@ -109,3 +109,43 @@ class GroupAPI(Resource): | ||||
|             return {'GROUP': getGroup(gid=gid)}, 200 | ||||
|         elif name != "": | ||||
|             return {'GROUP': getGroup(name=name)}, 200 | ||||
|  | ||||
|     def options(self, gid): | ||||
|         args = request.get_json(cache=False, force=True) | ||||
|         if not checkParams(['pairs'], args): | ||||
|             return {"ERROR": "One or more parameters are missing !"}, 400 | ||||
|  | ||||
|         pairs = args["pairs"] | ||||
|  | ||||
|         group = getGroup(gid=gid) | ||||
|         if group is None: | ||||
|             return {"ERROR": "This group does not exists !"}, 405 | ||||
|  | ||||
|         for p in pairs: | ||||
|             try: | ||||
|                 stud = getUser(uid=p[0]) | ||||
|                 if stud is None: | ||||
|                     return {"ERROR": "The user with id " + str(p[0]) + " does not exists !"}, 400 | ||||
|                 elif stud['role'] != "4": | ||||
|                     return {"ERROR": "A student must have the 'student' role !"}, 400 | ||||
|  | ||||
|                 tutor = getUser(uid=p[1]) | ||||
|                 if tutor is None: | ||||
|                     return {"ERROR": "The user with id " + str(p[1]) + " does not exists !"}, 400 | ||||
|                 elif tutor['role'] == "4": | ||||
|                     return {"ERROR": "A student can't be a tutor !"}, 400 | ||||
|                 elif "3" not in tutor['role'].split('-'): | ||||
|                     role = tutor['role'] + "-3" | ||||
|                     query = USER.update().values(role=role).where(USER.c.id == p[1]) | ||||
|                     query.execute() | ||||
|             except IndexError: | ||||
|                 return {"ERROR": "Pairs are incorrectly formed !"}, 409 | ||||
|  | ||||
|             query = TUTORSHIP.insert().values(group_id=gid, student_id=p[0], ptutor_id=p[1]) | ||||
|             query.execute() | ||||
|             mail = mailsModels.getMailContent("NEW_TO_GROUP", {"GROUP": group["name"], | ||||
|                                                                "URL": "ola.univ-tlse2.fr/registration/" | ||||
|                                                                       + get_random_string()}) | ||||
|             send_mail(mail[0], stud["email"], mail[1]) | ||||
|  | ||||
|         return {"RESULT": "Pairs added successfully"}, 200 | ||||
|   | ||||
							
								
								
									
										13
									
								
								backend/app/api/mailsModels.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								backend/app/api/mailsModels.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| _NEW_USER = ("Votre compte OLA a été créé !", "Bonjour,<br/><p>Votre compte vient d'être créé dans l'Outil du " | ||||
|                                               "Livret de l'Alternant dans le groupe #GROUPE. Vous pouvez dès " | ||||
|                                               "maintenant créer un livret en vous rendant à l'adresse : <br/>" | ||||
|                                               "<a href='#URL'>#URL</a></p><p>Bonne journée !</p>") | ||||
|  | ||||
|  | ||||
| def getMailContent(mail_type, args): | ||||
|     mail = None | ||||
|     if mail_type == "NEW_USER": | ||||
|         mail = _NEW_USER | ||||
|         for key, value in args: | ||||
|             mail[1].replace("#" + key, value) | ||||
|     return mail | ||||
| @@ -31,6 +31,8 @@ class Config: | ||||
|     CAS_LOGIN_ROUTE = "/login" | ||||
|     CAS_LOGOUT_ROUTE = "/logout" | ||||
|     CAS_VALIDATE_ROUTE = "/serviceValidate" | ||||
|     BASE_RESSOURCES_DIR = "/OLA_RESSOURCES/" | ||||
|     MAILER = True | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -42,6 +44,7 @@ class Debug(Config): | ||||
|     DEBUG = True | ||||
|     SESSION_COOKIE_SECURE = False | ||||
|     SQLALCHEMY_DATABASE_URI = 'mysql://ola:XXX@localhost/OLA' | ||||
|     BASE_RESSOURCES_DIR = os.path.abspath(os.path.dirname(__file__))+"/OLA_RESSOURCES/" | ||||
|  | ||||
|  | ||||
| class Test(Config): | ||||
| @@ -49,3 +52,5 @@ class Test(Config): | ||||
|     BASE_DIR = os.path.abspath(os.path.dirname(__file__)) | ||||
|     SQLALCHEMY_DATABASE_URI = \ | ||||
|         'sqlite:///' + os.path.join(BASE_DIR, '../test.db') | ||||
|     BASE_RESSOURCES_DIR = os.path.abspath(os.path.dirname(__file__))+"/OLA_RESSOURCES/" | ||||
|     MAILER = False | ||||
|   | ||||
| @@ -3,8 +3,12 @@ import string | ||||
| from hashlib import sha512 | ||||
|  | ||||
| from flask import json | ||||
| from mailer import Mailer | ||||
| from mailer import Message | ||||
| from sqlalchemy.ext.declarative import DeclarativeMeta | ||||
|  | ||||
| from app.core import app | ||||
|  | ||||
| SIMPLE_CHARS = string.ascii_letters + string.digits | ||||
|  | ||||
|  | ||||
| @@ -57,3 +61,12 @@ def new_alchemy_encoder(revisit_self=False, fields_to_expand=[]): | ||||
| def checkParams(wanted, args): | ||||
|     inter = [elt for elt in wanted if elt in args] | ||||
|     return len(inter) == len(wanted) | ||||
|  | ||||
|  | ||||
| def send_mail(subject, to, html): | ||||
|     if app.config['MAILER']: | ||||
|         message = Message(From="ola.noreply@univ-tlse2.fr", To=to, charset="utf-8") | ||||
|         message.Subject = subject | ||||
|         message.Html = html | ||||
|         sender = Mailer('localhost')  # TODO: Mettre le SMTP de la fac ici | ||||
|         sender.send(message) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user