Beispiel #1
0
def squads_dashboard():
    search = request.args.get('search')

    # handle search bar requests
    if search is not None:
        squads = SquadsFinder.search_by_name(search)
    else:
        search = None
        squads = SquadsFinder.get_all()

    if squads is None or len(squads) == 0:
        error = 'No squads found'
        return render_template(
            'admin/students_app/squads/squads_dashboard.html',
            squads=None,
            error=error,
            search=search,
            current_user=current_user)

    for squad in squads:
        squad.members_id = [member.user.username for member in squad.members]
        squad.members_id.remove(squad.captain_ist_id)
        squad.members_id = " ".join(squad.members_id)

    return render_template('admin/students_app/squads/squads_dashboard.html',
                           squads=squads,
                           error=None,
                           search=search,
                           current_user=current_user)
Beispiel #2
0
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
Beispiel #3
0
def reject_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)

    SquadsHandler.delete_squad_invitation(invitation)

    return jsonify('Success'), 200
Beispiel #4
0
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'))
Beispiel #5
0
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)
Beispiel #6
0
    def invite_squad_members(cls, student, members_ist_id):
        invitations_sent = SquadsFinder.get_invitations_from_parameters(
            {"sender_id": student.id})
        if (student.squad is None
                or (len(members_ist_id) + len(student.squad.members.all()) +
                    len(invitations_sent) > 4)):
            return None

        for member_ist_id in members_ist_id:
            member = StudentsFinder.get_from_ist_id(member_ist_id)
            if (member is None or member in student.squad.members):
                continue

            if (SquadsHandler.create_squad_invitation(
                    sender_id=student.id, receiver_id=member.id) is None):
                return False

        return True
Beispiel #7
0
def select_winners():
    top_squads = SquadsFinder.get_first()
    if top_squads is None:
        return APIErrorValue("No squad found").json(404)

    winner = choice(top_squads)
    now = datetime.utcnow()
    date = now.strftime('%d %b %Y, %a')

    squad_reward = RewardsFinder.get_squad_reward_from_date(date)
    if squad_reward is None:
        return APIErrorValue("No reward found").json(404)

    squad_reward = RewardsHandler.update_squad_reward(squad_reward,
                                                      winner_id=winner.id)
    if squad_reward is None:
        return APIErrorValue("Error selecting winner").json(500)

    return jsonify("Success"), 200
Beispiel #8
0
def get_notifications(student):
    notifications = {}

    if (student.squad):
        notifications['squad_xp'] = student.squad.total_points

    notifications['invites'] = []
    invitations = SquadsFinder.get_invitations_from_parameters(
        {"receiver_id": student.id})
    for invitation in invitations:
        sender = StudentsFinder.get_from_id(invitation.sender_id)
        notifications['invites'].append(sender.user.name)

    notifications['activities'] = []
    activities = ActivitiesFinder.get_next_activity()
    for activity in activities:
        notifications['activities'].append(activity.name)

    return jsonify(notifications), 200
Beispiel #9
0
def cancel_invite(student):
    try:
        receiver_id = request.get_json()["id"]
    except KeyError:
        return APIErrorValue('Invalid members').json(500)

    invitations = SquadsFinder.get_invitations_from_parameters({
        "sender_id":
        student.id,
        "receiver_id":
        receiver_id
    })
    if (invitations is None or len(invitations) == 0):
        return APIErrorValue("No invites found").json(404)

    for invitation in invitations:
        SquadsHandler.delete_squad_invitation(invitation)

    return jsonify('Success'), 200
Beispiel #10
0
    def leave_squad(cls, student):
        if (student.squad is None):
            return student

        if (len(student.squad.members.all()) == 1):
            SquadsHandler.delete_squad(student.squad)

        elif (student.is_captain()):
            SquadsHandler.update_squad(
                student.squad,
                captain_ist_id=list(
                    filter(lambda member: (not member.is_captain()),
                           student.squad.members.all()))[0].user.username)

        invitations = SquadsFinder.get_invitations_from_parameters(
            {'sender_id': student.id})
        for invitation in invitations:
            SquadsHandler.delete_squad_invitation(invitation)

        return cls.update_student(student, squad_id=None, squad_points=0)
    def __init__(self, invitations):
        super(SquadInvitationsValue, self).initialize({})
        invitations_array = []
        for invitation in invitations:
            squad = invitation.sender.squad

            invitation_value = {
                "external_id":
                invitation.external_id,
                "squad_name":
                squad.name,
                "squad_cry":
                squad.cry,
                "squad_rank":
                SquadsFinder.get_rank(squad.id),
                "squad_image":
                SquadsHandler.find_squad_image(str(squad.external_id)),
                "sender_name":
                invitation.sender.user.name
            }
            invitations_array.append(invitation_value)
        self.serialize_with(data=invitations_array)
Beispiel #12
0
	def __init__(self, squads):
		super(SquadsValue, self).initialize({})

		if (not isinstance(squads, list)):
			squads = [squads]

		squads_array = []
		for squad in squads:
			squad_value = {
				"name": squad.name,
				"cry": squad.cry,
				"daily_points": squad.daily_points,
				"total_points": squad.total_points,
				"image": SquadsHandler.find_squad_image(str(squad.external_id)),
                "members": SquadMembersValue(squad.members).to_dict(),
				"captain_ist_id": squad.captain_ist_id,
				"rank": SquadsFinder.get_rank(squad.id)
			}
			squads_array.append(squad_value)
		
		if(len(squads_array) == 1):
			self.serialize_with(data=squads_array[0])
		else:
			self.serialize_with(data=squads_array)
Beispiel #13
0
def get_daily_squads_ranking(student):
    squads = SquadsFinder.get_daily_top()

    return SquadsValue(squads).json(200)
Beispiel #14
0
def get_squad_invitations_sent(student):
    invitations = SquadsFinder.get_invitations_from_parameters(
        {"sender_id": student.id})

    return SquadInvitationsSentValue(
        [invitation.receiver for invitation in invitations]).json(200)
Beispiel #15
0
def get_squad_invitations_received(student):
    invitations = SquadsFinder.get_invitations_from_parameters(
        {"receiver_id": student.id})

    return SquadInvitationsValue(invitations).json(200)