コード例 #1
0
def applications_view_bystatus(request, pk, term):
    """
    Filter applicants by application status.

    pk: Session user's id.
    term:
        'a': accepted
        'r': rejected
        'u': undecided
    """
    show_messages(request)
    user = User.objects.get(id=pk)
    if identify(request, user):
        return HttpResponseRedirect(reverse('home'))
    position_list = []
    project_list = user.projects.all()
    for project in project_list:
        for position in project.positions.all():
            position_list.append(position)
    applicants = Applicant.objects.filter(
        position__in=position_list
    ).filter(status=term)
    context = {
        'user': user,
        'applicants': applicants,
        'positions': position_list,
        'term': term
    }
    return render(request, 'accounts/applications.html', context)
コード例 #2
0
def applications_view(request, pk):
    """
    Display all applicants for session user's open projects.

    pk: Session user's id.
    """
    show_messages(request)
    user = User.objects.get(id=pk)
    if identify(request, user):
        return HttpResponseRedirect(reverse('home'))
    project_list = user.projects.all()
    position_list = []
    for project in project_list:
        for position in project.positions.all():
            position_list.append(position)

    applicants = Applicant.objects.filter(position__in=position_list)

    context = {
        'user': user,
        'applicants': applicants,
        'positions': position_list,
        'term': ''
    }

    return render(request, 'accounts/applications.html', context)
コード例 #3
0
def applications_view_byposition(request, pk, term):
    """
    Filter applicants by position they've applied for.

    pk: Session user's id.
    term: Name of position or positions.
    """
    show_messages(request)
    user = User.objects.get(id=pk)
    if identify(request, user):
        return HttpResponseRedirect(reverse('home'))
    position_list = []
    project_list = user.projects.all()
    for project in project_list:
        for position in project.positions.all():
            if position.name == term:
                position_list.append(position)
    applicants = Applicant.objects.filter(position__in=position_list)
    context = {
        'user': user,
        'applicants': applicants,
        'positions': position_list,
        'term': term
    }
    return render(request, 'accounts/applications.html', context)
コード例 #4
0
def avatar_view(request, pk):
    """
    View target user's avatar.

    pk: Target user's id.
    """
    show_messages(request)
    target_user = User.objects.get(id=pk)
    if not target_user.avatar:
        messages.info(request, "This user has no photo.")
        return HttpResponseRedirect(reverse('home'))
    context = {'user': request.user, 'target_user': target_user}
    return render(request, 'accounts/user_photo.html', context)
コード例 #5
0
def user_deactivate_view(request, pk):
    """
    If session user is target user,
    get confirmation for account deactivation.
    Else, redirect to homepage and do nothing.

    pk: User's id.
    """
    show_messages(request)
    target_user = User.objects.get(id=pk)
    if identify(request, target_user):
        return HttpResponseRedirect(reverse('home'))
    return render(
        request,
        'projects/delete.html',
        {'target_user': target_user}
    )
コード例 #6
0
def avatar_update_view(request, pk):
    """
    Upload or replace a user's avatar.
    If session user is not target user, redirect to home page.

    pk: Target user's id.
    """
    show_messages(request)
    user = User.objects.get(id=pk)
    if identify(request, user):
        return HttpResponseRedirect(reverse('home'))
    if request.method == 'POST':
        form = forms.PhotoForm(request.POST, request.FILES, instance=user)
        if form.is_valid():
            form.save()
            messages.success(request, 'Your user photo has been updated.')
            return redirect('accounts:details', pk=pk)
    else:
        form = forms.PhotoForm()
    context = {'form': form, 'user': user}
    return render(request, 'accounts/photo_form.html', context)
コード例 #7
0
def user_detail_view(request, pk):
    """
    View target user's account details.

    pk: Target user's id.
    """
    show_messages(request)
    user = request.user
    target_user = User.objects.get(id=pk)
    if not request.user.is_authenticated:
        if not target_user.is_active:
            return redirect('accounts:reactivate', pk=pk)

    user_skills = user.skills.order_by('name')
    skill_data = [{'name': skill}
                  for skill in user_skills]

    context = {
        'user': user,
        'target_user': target_user,
        'skills': skill_data
    }

    return render(request, 'accounts/user_detail.html', context)
コード例 #8
0
def user_update_view(request, pk):
    """
    If session user is target user, update account details.
    Else, redirect to home page.

    pk: Target user's id.
    """
    show_messages(request)
    user = User.objects.get(id=pk)
    if identify(request, user):
        return HttpResponseRedirect(reverse('home'))
    userdata = {
        'display_name': user.display_name,
        'bio': user.bio
    }
    SkillFormSet = formset_factory(pforms.SkillForm,
                                   formset=pforms.BaseSkillFormSet)
    user_skills = user.skills.all().order_by('name')
    skill_data = [{'name': skill.name}
                  for skill in user_skills]
    if request.method == 'GET':
        form = forms.UserForm(initial=userdata)
        formset = SkillFormSet(initial=skill_data)

    elif request.method == 'POST':
        form = forms.UserForm(request.POST, request.FILES, instance=user)
        formset = SkillFormSet(request.POST, request.FILES)
        old_skills = []
        for skill in user.skills.all():
            old_skills.append(skill.name)

        if form.is_valid() and formset.is_valid():
            user = form.save()
            new_skills = []

            for skill_form in formset:
                skill_name = skill_form.cleaned_data.get('name')

                if skill_name:
                    new_skills.append(skill_name)

            try:
                with transaction.atomic():
                    for skill in new_skills:
                        add_skill, _ = Skill.objects.get_or_create(name=skill)
                        add_skill.save()
                        add_skill.users.add(user)
                        if skill not in old_skills:
                            messages.success(
                                request,
                                '{} added to skills!'.format(skill)
                            )
                    for skill in old_skills:
                        if skill not in new_skills:
                            old_skill = Skill.objects.get(name=skill)
                            old_skill.save()
                            old_skill.users.remove(user)
                            messages.success(
                                request,
                                '{} removed from skills!'.format(skill)
                            )
                    user.save()

                    messages.success(request,
                                     'You have updated your profile.')

            except IntegrityError:
                messages.error(request,
                               'There was an error saving your profile.')
                return redirect('accounts:details', pk=pk)

    return render(
        request,
        'accounts/user_form.html',
        {'form': form, 'formset': formset}
    )
コード例 #9
0
 def form_valid(self, form):
     login(self.request, form.get_user())
     show_messages(self.request)
     # messages.info(self.request, form.get_user().notifications)
     return super().form_valid(form)