def create_squad(student): if (student.squad is not None): return APIErrorValue('Student already has squad').json(401) name = request.form.get('name', None) cry = request.form.get('cry', None) if name is None or cry is None: return APIErrorValue('Invalid squad info').json(500) if 'file' not in request.files: return APIErrorValue('No image detected').json(500) file = request.files['file'] if file and file.filename != '': squad = SquadsHandler.create_squad( name=name, cry=cry, captain_ist_id=student.user.username) if squad is None: return APIErrorValue('Error creating squad').json(500) result, msg = SquadsHandler.upload_squad_image(file, str(squad.external_id)) if not result: SquadsHandler.delete_squad(squad) return APIErrorValue(msg).json(500) StudentsHandler.add_squad_member(student, squad) else: return APIErrorValue('No image found').json(500) return SquadsValue(squad).json(200)
def unban_student(student_external_id): banned_student = StudentsFinder.get_banned_student_from_external_id( student_external_id) if banned_student is None: return APIErrorValue('Couldnt find student').json(500) StudentsHandler.delete_banned_student(banned_student) return redirect(url_for('admin_api.banned_students_dashboard'))
def add_linkedin(student): try: url = request.get_json()["url"] except KeyError: return APIErrorValue('Invalid url').json(500) if not student.linkedin_url: StudentsHandler.add_points(student, int(Config.REWARD_LINKEDIN)) StudentsHandler.update_student(student, linkedin_url=url) return StudentsValue(student, details=True).json(200)
def add_companies(student): try: companies = request.get_json()["companies"] except KeyError: return APIErrorValue('Invalid company').json(500) for company in companies: company = CompaniesFinder.get_from_name(company) if (company is None or company in student.companies): continue StudentsHandler.add_student_company(student, company) return StudentsValue(student, details=True).json(200)
def create_level(): value = request.form.get('value', None) points = request.form.get('points', None) reward_id = request.form.get('reward', None) if (reward_id == ""): reward_id = None if (value is None or points is None): return APIErrorValue('Invalid value or points').json(500) if (reward_id is not None): reward = RewardsFinder.get_reward_from_external_id(reward_id) if (reward is None): return APIErrorValue('Invalid reward Id') reward_id = reward.id levels = LevelsFinder.get_all_levels() if ((len(levels) > 0 and int(levels[-1].value + 1) != int(value)) or (len(levels) == 0 and int(value) != 1)): return APIErrorValue('Invalid level value').json(500) level = LevelsHandler.create_level(value=value, points=points, reward_id=reward_id) if (level is None): return APIErrorValue('Error creating level').json(500) if (len(levels) == 0 and level.value == 1): students = StudentsFinder.get_from_parameters({'level_id': None}) for student in students: StudentsHandler.update_student(student, level_id=level.id) levels = LevelsFinder.get_all_levels() rewards = RewardsFinder.get_all_rewards() if (levels is None): return render_template( 'admin/students_app/levels/levels_dashboard.html', levels=None, rewards=rewards, error='No levels found', current_user=current_user) return render_template('admin/students_app/levels/levels_dashboard.html', levels=levels, rewards=rewards, error=None, current_user=current_user)
def ban_squad(squad_external_id): squad = SquadsFinder.get_from_external_id(squad_external_id) if squad is None: return APIErrorValue('Couldnt find squad').json(500) for member in squad.members: StudentsHandler.leave_squad(member) banned_student = StudentsHandler.create_banned_student(member) if banned_student is None: return APIErrorValue('Error banning student').json(500) UsersHandler.delete_user(member.user) return redirect(url_for('admin_api.squads_dashboard'))
def kick_member(student): if (not student.is_captain()): return APIErrorValue('Student is not captain').json(401) try: member_ist_id = request.get_json()["ist_id"] except KeyError: return APIErrorValue('Invalid IST id').json(500) member = StudentsFinder.get_from_ist_id(member_ist_id) if (member is None): return APIErrorValue('Member not found').json(404) StudentsHandler.leave_squad(member) return SquadsValue(student.squad).json(200)
def delete_company(student): try: company = request.get_json()["company"] except KeyError: return APIErrorValue('Invalid company').json(500) company = CompaniesFinder.get_from_name(company) if (company is None): return APIErrorValue('Company not found').json(404) student_company = StudentsFinder.get_student_company(student, company) if (student_company is None): return APIErrorValue('Student company not found').json(404) StudentsHandler.delete_student_company(student_company) return StudentsValue(student, details=True).json(200)
def today_login(student): now = datetime.utcnow() date = now.strftime('%d %b %Y, %a') event = EventsFinder.get_default_event() dates = EventsHandler.get_event_dates(event) if date in dates: student_login = StudentsFinder.get_student_login(student, date) if student_login is None: StudentsHandler.add_student_login(student, date) StudentsHandler.add_points(student, int(Config.REWARD_LOGIN)) else: return APIErrorValue("Already loggedin today").json(409) else: return APIErrorValue("Date out of event").json(409) return StudentsValue(student, details=True).json(200)
def login_student(fenix_auth_code): if fenix_auth_code is not None: user = TecnicoClientHandler.get_user(fenix_client, fenix_auth_code) person = TecnicoClientHandler.get_person(fenix_client, user) banned_ids = StudentsFinder.get_banned_students_ist_id() if (person['username'] in banned_ids): return None, None student = StudentsFinder.get_from_ist_id(person['username']) if student is None: course = None for role in person['roles']: if role['type'] == "STUDENT": for registration in role['registrations']: if registration['acronym']: course = registration['acronym'] entry_year = get_year( registration['academicTerms']) break if role['type'] == 'ALUMNI': for registration in role['concludedRegistrations']: if registration['acronym']: course = registration['acronym'] entry_year = get_year( registration['academicTerms']) break if not course: return None, None if person['email']: email = person['email'] elif person['institutionalEmail']: email = person['institutionalEmail'] else: return None, None student = StudentsHandler.create_student( person['name'], person['username'], email, course, entry_year, person['photo']['data'], person['photo']['type']) if student is None: return None, None _jwt = jwt.encode({"user_id": person['username']}, Config.JWT_SECRET, algorithm="HS256") encrypted_jwt = EncryptTokenService(_jwt).call() return student, encrypted_jwt else: return None, None
def reset_daily_points(): squads = SquadsFinder.get_all() for squad in squads: if not SquadsHandler.reset_daily_points(squad): return APIErrorValue("Reset failed").json(500) students = StudentsFinder.get_all() for student in students: if not StudentsHandler.reset_daily_points(student): return APIErrorValue("Reset failed").json(500) return jsonify("Success"), 200
def delete_level(level_external_id): level = LevelsFinder.get_level_from_external_id(level_external_id) if level is None: return APIErrorValue('Couldnt find level').json(500) levels = LevelsFinder.get_all_levels() if (len(levels) == 0 or (len(levels) > 0 and levels[-1] == level)): students = StudentsFinder.get_from_level_or_higher(level) previous_level = LevelsFinder.get_level_by_value(level.value - 1) if (previous_level is None): for student in students: StudentsHandler.update_student(student, level_id=None, total_points=0) else: for student in students: StudentsHandler.update_student( student, level_id=previous_level.id, total_points=previous_level.points) LevelsHandler.delete_level(level) levels = LevelsFinder.get_all_levels() rewards = RewardsFinder.get_all_rewards() if (levels is None): return render_template( 'admin/students_app/levels/levels_dashboard.html', levels=None, rewards=rewards, error='No levels found', current_user=current_user) return render_template('admin/students_app/levels/levels_dashboard.html', levels=levels, rewards=rewards, error=None, current_user=current_user)
def add_cv(student): if 'cv' not in request.files: return APIErrorValue('No cv found').json(500) file = request.files['cv'] if file.filename == '': return APIErrorValue('No cv found').json(500) if file and FileHandler.allowed_file(file.filename): filename = 'cv-' + student.user.username + '.pdf' if not FileHandler.upload_file(file, filename): return APIErrorValue('Error uploading file').json(500) if not student.uploaded_cv: StudentsHandler.update_student(student, uploaded_cv=True) StudentsHandler.add_points(student, int(Config.REWARD_CV)) else: return APIErrorValue('Wrong file extension').json(500) return StudentsValue(student, details=True).json(200)
def invite_squad(student): if (student.squad is None): return APIErrorValue('No squad found').json(401) try: members = request.get_json()["members"] except KeyError: return APIErrorValue('Invalid members').json(500) if (StudentsHandler.invite_squad_members(student, members)): return jsonify('Success'), 200 else: return APIErrorValue('Failed to invite').json(500)
def ban_student(student_external_id): student = StudentsFinder.get_from_external_id(student_external_id) if student is None: return APIErrorValue('Couldnt find student').json(500) if student.squad: SquadsHandler.delete_squad(student.squad) banned_student = StudentsHandler.create_banned_student(student) if banned_student is None: return APIErrorValue('Error banning student').json(500) UsersHandler.delete_user(student.user) return redirect(url_for('admin_api.students_dashboard'))
def accept_invitation(student): try: invitation_id = request.get_json()["invitation_id"] except KeyError: return APIErrorValue('Invalid invitation').json(500) invitation = SquadsFinder.get_invitation_from_external_id(invitation_id) if (invitation is None): return APIErrorValue('Invitation not found').json(404) student = StudentsHandler.accept_invitation(student, invitation) if (not student): return APIErrorValue("Failed to join squad").json(500) return StudentsValue(student, details=True).json(200)
def redeem_activity_code(cls, student, code): activity_code = ActivityCodesFinder.get_from_code(code) if (activity_code is None): return "Code not found", student if (activity_code.activity in student.activities): return "Already participated", student ActivitiesHandler.add_student_activity(student, activity_code.activity) now = datetime.utcnow() today = now.strftime('%d %b %Y, %a') if activity_code.activity.day != today: return "Code expired", student points = activity_code.activity.points if activity_code.activity.activity_type.name not in [ "Speaker", "Discussion Panel", "Ceremony" ]: cls.delete_activity_code(activity_code) return None, StudentsHandler.add_points(student, points)
def leave_squad(student): student = StudentsHandler.leave_squad(student) return StudentsValue(student, details=True).json(200)