Exemple #1
0
def profile(request: HttpRequest) -> HttpResponse:
    team = request.user
    if not hasattr(team, 'teamprofile'):
        messages.add_message(request, messages.ERROR, 'We tried really hard to find it but this team does not exists.')
        return redirect(reverse('team:login'))
    team_profile = team.teamprofile

    ctf_settings = CTFSettings.objects.first()
    ctf_has_been_started = ctf_settings.has_been_started or request.user.is_staff
    if ctf_has_been_started:
        validated_challs = team_profile.validated_challenges.all()
        team_score = sum(map(lambda c: c.get_nb_points(), validated_challs))

        challs = Challenge.objects.all()
        points_to_get = sum(map(lambda c: c.get_nb_points(), challs))
        percentage_valitated_challs = 0
        if points_to_get != 0:
            percentage_valitated_challs = int(100 * (team_score / points_to_get))
        # Deactivate feature as it's not used for now
        ssh = Ssh.objects.filter(id_team_profile=team_profile.pk).first()

    if request.method == "POST":
        team_form = UserForm(request.POST, instance=team)
        team_profile_form = TeamProfileForm(request.POST, request.FILES, instance=team_profile)
        response = create_or_update_team(request, team_form, team_profile_form, False)
        if response is not None:
            return response
    else:
        team_form = UserForm(instance=team)
        team_profile_form = TeamProfileForm(instance=team_profile)

    return render(request, 'user_manager/profile.html', locals())
Exemple #2
0
def register(request: HttpRequest) -> HttpResponse:
    if settings.REGISTER_ENABLED:
        if request.method == "POST":
            team_profile_form = TeamProfileForm(request.POST, request.FILES)
            team_form = UserForm(request.POST)
            response = create_or_update_team(request, team_form, team_profile_form, True)
            if response is not None:
                return response
        else:
            team_profile_form = TeamProfileForm()
            team_form = UserForm()
    else:
        messages.add_message(request, messages.ERROR, "You cannot register anymore, please contact an admin.")
    return render(request, 'user_manager/register.html', locals())
Exemple #3
0
def create_or_update_team(request: HttpRequest, team_form: UserForm,
                          team_profile_form: TeamProfileForm,
                          creating: bool) -> HttpResponse:
    if not team_form.is_valid() or not team_profile_form.is_valid():
        messages.add_message(request, messages.ERROR,
                             team_form.non_field_errors())
    else:
        try:
            if creating:
                team = User.objects.create_user(
                    username=team_form.cleaned_data['username'],
                    email=team_form.cleaned_data['email'],
                    password=team_form.cleaned_data['password'],
                    is_active=True)
            else:
                team = team_form.instance
                team.username = team_form.cleaned_data['username']
                new_pass = team_form.cleaned_data.get('password')
                if new_pass:
                    team.set_password(new_pass)
                else:
                    team.password = team_form.initial['password']
                team.email = team_form.cleaned_data['email']
                team.save()
            team_profile = team_profile_form.save(commit=False)
            team_profile.team = team
            team_profile.save()
            if creating:
                messages.add_message(
                    request, messages.SUCCESS,
                    'Congratulations ! Your team is created.')
                t = authenticate(username=team.username,
                                 password=team_form.cleaned_data['password'])
                if t is not None:
                    login(request, t)
            else:
                messages.add_message(request, messages.SUCCESS,
                                     'Your team has been updated.')
            return redirect(reverse('team:profile'))
        except Exception:
            logger.exception(
                'Error while creating/updating a team. creating=' +
                str(creating) + ', req=' + "\n".join(request.readlines()))
            request.session['messages'] = [
                'Sorry, an error occurred, please alert an admin.'
            ]
    return None
Exemple #4
0
def register(request):
    if settings.REGISTER_ENABLED:
        if request.method == "POST":
            team_profile_form = TeamProfileForm(request.POST, request.FILES)
            team_form = UserForm(request.POST)
            response = create_or_update_team(request, team_form,
                                             team_profile_form, True)
            if response is not None:
                return response
        else:
            team_profile_form = TeamProfileForm()
            team_form = UserForm()
    else:
        request.session["messages"] = [
            "You cannot register anymore, please contact an admin."
        ]
    return delete_messages_before(
        render(request, 'user_manager/register.html', locals()), request)
Exemple #5
0
def register(request):
    """docstring"""

    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        dev_form = DevSignUpForm(data=request.POST)

        if user_form.is_valid() and dev_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            dev = dev_form.save(commit=False)
            dev.user = user

            if 'profile_picture' in request.FILES:
                dev.profile_picture = request.FILES['profile_picture']

            dev.save()

            new_resume = Resume()
            new_resume.owner = dev
            new_resume.last_update = datetime.today()
            new_resume.save()

            # registered = True
            return redirect('user_manager:login')
            
        else:
            print(user_form.errors, dev_form.errors)
    else:
        user_form = UserForm()
        dev_form = DevSignUpForm()

    return render(
        request,
        'register.html',
        {
            'user_form': user_form,
            'dev_form': dev_form,
            'registered': registered
        }
    )
Exemple #6
0
def profile(request, user_to_show):
    team = get_object_or_404(User, username=user_to_show)
    if not hasattr(team, 'teamprofile'):
        request.session['messages'] = [
            'We tried really hard to find it but this team does not exists.'
        ]
        return redirect(reverse('challenges:list'))
    team_profile = team.teamprofile

    ctf_settings = CTFSettings.objects.first()
    if ctf_settings.is_running or request.user.is_staff:
        if request.user.is_staff:
            number_of_contenders = User.objects.filter(is_active=True).count()
            rank = TeamProfile.objects.filter(
                score__gt=team_profile.score).count() + 1
            nb_challs = Challenge.all_objects.all().count()
        else:
            number_of_contenders = User.objects.filter(is_active=True,
                                                       is_staff=False).count()
            rank = TeamProfile.objects.filter(score__gt=team_profile.score,
                                              team__is_staff=False).count() + 1
            nb_challs = Challenge.objects.count()
        percentage_valitated_challs = 0
        if nb_challs != 0:
            percentage_valitated_challs = int(
                100 * (team_profile.validated_challenges.count() / nb_challs))

    rank_by_challenge_val = {}
    for index, chall_val in enumerate(team_profile.validated_challenges.all()):
        if request.user.is_staff:
            team_flag = TeamFlagChall.objects.filter(flagger=team_profile,
                                                     chall=chall_val)
            rank = TeamFlagChall.objects.filter(
                chall=chall_val,
                date_flagged__lte=team_flag[0].date_flagged).count()
        else:
            team_flag = TeamFlagChall.objects.filter(
                flagger=team_profile,
                chall=chall_val,
                flagger__team__is_staff=False)
            rank = TeamFlagChall.objects.filter(
                chall=chall_val,
                date_flagged__lte=team_flag[0].date_flagged,
                flagger__team__is_staff=False).count()
        rank_by_challenge_val[index] = rank

    # form to update personnal infos
    if user_to_show == request.user.username or request.user.is_staff:
        personnal_space = True
        if request.method == "POST":
            team_form = UserForm(request.POST, instance=team)
            team_profile_form = TeamProfileForm(request.POST,
                                                request.FILES,
                                                instance=team_profile)
            response = create_or_update_team(request, team_form,
                                             team_profile_form, False)
            if response is not None:
                return response
        else:
            team_form = UserForm(instance=team)
            team_profile_form = TeamProfileForm(instance=team_profile)
    if user_to_show == request.user.username:
        name = 'You'
        pronoum = 'Your'
    else:
        name = user_to_show
        pronoum = user_to_show

    return delete_messages_before(
        render(request, 'user_manager/profile.html', locals()), request)