Exemplo n.º 1
0
def createUser(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        isAdmin = False
        if form.is_valid():
            invite = None
            # Checking if user invite is enabled
            if getAdminOptions().inviteCodeEnabled:
                inviteCode = form.data.get('godFather')
                if InviteCode.objects.filter(code=inviteCode).count() == 1:
                    invite = InviteCode.objects.get(code=inviteCode)
                    if UserPreferences.objects.get(
                            user=invite.user).group.permissions.filter(
                                code="SPON").count() != 1:
                        return render(request, 'signup.html', {'form': form})
                else:
                    return render(request, 'signup.html', {'form': form})

            # FIXME: TOO LONG! Make smaller functions
            form.save()
            # Special condition for the first user to be administrator
            if User.objects.all().count() == 1:
                isAdmin = True
                populateDB()

            # Creating user if tests are ok
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            user.is_superuser = isAdmin
            user.save()

            # Setting the user preferences
            userPref = UserPreferences()
            userPref.user = user
            if isAdmin:
                userPref.group = Groups.objects.get(rank=5)
            else:
                userPref.group = Groups.objects.get(rank=1)
            wallet = Wallet()
            wallet.save()
            userPref.wallet = wallet
            if invite is not None:
                userPref.inviteCode = invite

            # creating user avatar
            userPref.avatar = identicon.create_identicon(username)

            userPref.save()
            createUserInviteCode(user)
            login(request, user)
            return HttpResponseRedirect(reverse('app:index'))
    else:
        form = UserCreationForm()
    return render(request, 'signup.html', {'form': form})
Exemplo n.º 2
0
 def dispatch(self, *args, **kwargs):
     populateDB()
     return super(mainView, self).dispatch(*args, **kwargs)