Commit bf94578f authored by MariaV's avatar MariaV
Browse files

Merge branch 'double_acc_rec' into 'master'

Added logic for gitlab account request creation to deal with multiple requests, pending vs rejected, etc.

See merge request !66
parents 1760f728 a740ac11
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
# Generated by Django 3.1.5 on 2021-01-27 15:51

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('anonticket', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='gitlabaccountrequest',
            name='linked_user',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='anonticket.useridentifier'),
        ),
    ]
+1 −1
Original line number Diff line number Diff line
@@ -287,7 +287,7 @@ class GitlabAccountRequest(models.Model):
    )
    approved_to_GitLab = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True, blank=False)
    linked_user = models.OneToOneField(
    linked_user = models.ForeignKey(
        UserIdentifier, 
        null=True, 
        blank=True, 
+2 −2
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ urlpatterns = [
        'user/login/', 
        views.login_view, name='login'),
    path(
        'user/create-gitlab-account/', 
        'user/gitlab-account/create/', 
        views.GitlabAccountRequestCreateView.as_view(), 
        name='create-gitlab-no-user'),
    path(
@@ -41,7 +41,7 @@ urlpatterns = [
        views.CannotCreateObjectView.as_view(), 
        name='cannot-create-with-user'),
    path(
        'user/<str:user_identifier>/create-gitlab-account/', 
        'user/<str:user_identifier>/gitlab-account/create/', 
        views.GitlabAccountRequestCreateView.as_view(), 
        name='create-gitlab-with-user'),
    path(
+23 −11
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ from .forms import (
    )
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse, reverse_lazy
from django.core.exceptions import ObjectDoesNotExist
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import user_passes_test
from django.views.generic import (
@@ -261,20 +262,31 @@ class GitlabAccountRequestCreateView(
    template_name_suffix = '_user_create'

    def form_valid(self, form):
        """Populate user_identifier FK from URL if present."""
        """Populate user_identifier FK from URL if present, without allowing duplicate
        pending requests."""
        # If user_identifier in the URL path, set variable user_identifier.
        if 'user_identifier' in self.kwargs:
            user_identifier = self.kwargs['user_identifier']
            # make sure it's a valid user_identifier
            if check_user(user_identifier) == False:
                return redirect('user-login-error', user_identifier=user_identifier)
            else:
                # Then try to grab the linked_user from the database.
             # Then try to grab the user_identifier from URL from database.
            try: 
                    linked_user = UserIdentifier.objects.get(user_identifier=user_identifier)
                    form.instance.linked_user = linked_user
                # Unless this user has already created a GL account request
                except:
                url_user = UserIdentifier.objects.get(user_identifier=user_identifier)
                # if the User Identifier exists, see if has made any account requests
                gl_requests = GitlabAccountRequest.objects.filter(linked_user=url_user)
                if len(gl_requests) != 0:
                    for request in gl_requests:
                        if request.reviewer_status == 'P':
                            return redirect('cannot-create-with-user', user_identifier=user_identifier)
                        # If you get this far, save the form with the new account request.
                        form.instance.linked_user = url_user
                 # if the User Identifier exists, but does not have a pending GL request, make one.
                else:
                    form.instance.linked_user = url_user
                    return redirect ('issue-created', user_identifier=user_identifier)
            # If the user_identifier doesn't exist, create it and save the request.
            except ObjectDoesNotExist:
                new_user = UserIdentifier(user_identifier=user_identifier)
                new_user.save()
                form.instance.linked_user = new_user