1+ import collections
12import json
23import os
3- import botocore
4- import requests
5- import boto3
64
7- from conditional .models .models import MajorProject , MajorProjectSkill
8- from conditional .util .user_dict import user_dict_is_eval_director
95from flask import Blueprint
106from flask import request
117from flask import jsonify
128from flask import redirect
139
14- from sqlalchemy import func , desc
15-
10+ import botocore
11+ import requests
12+ import boto3
1613import structlog
14+
15+ from sqlalchemy import func , desc
1716from werkzeug .utils import secure_filename
1817
19- from conditional .util .context_processors import get_member_name
18+ from conditional import db , start_of_year , get_user , auth , app
19+ from conditional .models .models import MajorProject
20+ from conditional .models .models import MajorProjectSkill
2021
21- from conditional .util .ldap import ldap_is_eval_director
22+ from conditional .util .context_processors import get_member_name
2223from conditional .util .ldap import ldap_get_member
2324from conditional .util .flask import render_template
25+ from conditional .util .user_dict import user_dict_is_eval_director
2426
25- from conditional import db , start_of_year , get_user , auth , app
26-
27- import collections
2827collections .Callable = collections .abc .Callable
2928
3029logger = structlog .get_logger ()
3332
3433def list_files_in_folder (bucket_name , folder_prefix ):
3534
36- s3 = boto3 .client (
35+ s3 = boto3 .client (
3736 service_name = "s3" ,
38- aws_access_key_id = app .config ['AWS_ACCESS_KEY_ID' ],
37+ aws_access_key_id = app .config ['AWS_ACCESS_KEY_ID' ],
3938 aws_secret_access_key = app .config ['AWS_SECRET_ACCESS_KEY' ],
4039 endpoint_url = app .config ['S3_URI' ]
4140 )
42-
41+
4342 try :
4443 response = s3 .list_objects (Bucket = bucket_name , Prefix = folder_prefix )
4544 if 'Contents' in response :
4645 return [obj ['Key' ] for obj in response ['Contents' ]]
47- else :
48- return []
49-
46+
47+ return []
48+
5049 except botocore .exceptions .ClientError as e :
5150 print (f"Error listing files in the folder: { e } " )
5251 return []
@@ -77,13 +76,6 @@ def display_major_project(user_dict=None):
7776 ).where (MajorProject .date >= start_of_year ()
7877 ).order_by (desc (MajorProject .date ), desc (MajorProject .id ))
7978
80- s3 = boto3 .client (
81- service_name = "s3" ,
82- aws_access_key_id = app .config ['AWS_ACCESS_KEY_ID' ],
83- aws_secret_access_key = app .config ['AWS_SECRET_ACCESS_KEY' ],
84- endpoint_url = app .config ['S3_URI' ]
85- )
86-
8779 bucket = app .config ['S3_BUCKET_ID' ]
8880
8981 major_projects = [
@@ -112,7 +104,6 @@ def display_major_project(user_dict=None):
112104 major_projects = major_projects ,
113105 major_projects_len = major_projects_len ,
114106 username = user_dict ["username" ])
115-
116107@major_project_bp .route ("/major_project/upload" , methods = ["POST" ])
117108@auth .oidc_auth ("default" )
118109@get_user
@@ -122,7 +113,7 @@ def upload_major_project_files(user_dict=None):
122113
123114 if len (list (request .files .keys ())) < 1 :
124115 return "No file" , 400
125-
116+
126117 # Temporarily save files to a place, to be uploaded on submit
127118
128119 for _ , file in request .files .lists ():
@@ -132,7 +123,7 @@ def upload_major_project_files(user_dict=None):
132123
133124 os .makedirs (os .path .dirname (filename ), exist_ok = True )
134125 file .save (filename )
135-
126+
136127 return jsonify ({"success" : True }), 200
137128
138129
@@ -161,7 +152,7 @@ def submit_major_project(user_dict=None):
161152 # TODO: Do we want any of the fields to have enforced min or max lengths?
162153 if not name or not tldr or not time_spent or not description :
163154 return jsonify ({"success" : False }), 400
164-
155+
165156 project : MajorProject = MajorProject (user_id , name , tldr , time_spent , description , links )
166157
167158 # Save the info to the database
@@ -174,41 +165,41 @@ def submit_major_project(user_dict=None):
174165 MajorProject .name == name ,
175166 MajorProject .uid == user_id
176167 ).first ()
177-
168+
178169 skills_list : list = list (filter (lambda x : x != 'None' , skills ))
179170
180171 for skill in skills_list :
181172 skill = skill .strip ()
182-
183- if skill != "" and skill != 'None' :
173+
174+ if skill not in ( "" , 'None' ) :
184175 mp_skill = MajorProjectSkill (project .id , skill )
185176 db .session .add (mp_skill )
186177
187178 db .session .commit ()
188-
179+
189180 # Fail if attempting to retreive non-existent project
190181 if project is None :
191182 return jsonify ({"success" : False }), 500
192-
183+
193184 # Sanitize input so that the Slackbot cannot ping @channel
194185 name = name .replace ("<!" , "<! " )
195186
196187 # Connect to S3 bucket
197- s3 = boto3 .client ("s3" ,
188+ s3 = boto3 .client ("s3" ,
198189 aws_access_key_id = app .config ['AWS_ACCESS_KEY_ID' ],
199190 aws_secret_access_key = app .config ['AWS_SECRET_ACCESS_KEY' ],
200191 endpoint_url = app .config ['S3_URI' ])
201-
192+
202193 # Collect all the locally cached files and put them in the bucket
203194 temp_dir = f"/tmp/{ user_id } "
204195 if os .path .exists (temp_dir ):
205196 for file in os .listdir (temp_dir ):
206197 filepath = f"{ temp_dir } /{ file } "
207198
208199 s3 .upload_file (filepath , 'major-project-media' , f"{ project .id } /{ file } " )
209-
200+
210201 os .remove (filepath )
211-
202+
212203 # Delete the temp directory once all the files have been stored in S3
213204 os .rmdir (temp_dir )
214205
@@ -245,7 +236,7 @@ def major_project_review(user_dict=None):
245236
246237 db .session .flush ()
247238 db .session .commit ()
248-
239+
249240 return jsonify ({"success" : True }), 200
250241
251242
@@ -261,14 +252,14 @@ def major_project_delete(pid, user_dict=None):
261252
262253 if creator == user_dict ["username" ] or user_dict_is_eval_director (user_dict ["account" ]):
263254 MajorProject .query .filter (MajorProject .id == pid ).delete ()
264-
255+
265256 db .session .flush ()
266257 db .session .commit ()
267-
258+
268259 return jsonify ({"success" : True }), 200
269260
270261 return "Must be project owner to delete!" , 401
271262
272263
273264def send_slack_ping (payload ):
274- requests .post (app .config ["WEBHOOK_URL" ], json .dumps (payload ), timeout = 120 )
265+ requests .post (app .config ["WEBHOOK_URL" ], json .dumps (payload ), timeout = 120 )
0 commit comments