Question: Best way to determine a new site user - Django

Question

Best way to determine a new site user - Django

Answers 2
Added at 2016-12-31 18:12
Tags
Question

I want a way of checking if someone has filled out their profile information (new user) with django. If they havn't I want to show a modal that won't go away until all information has been filled out. No matter what page they go to, it should show this modal until filled out.

Should I do the check with javascript(ajax) to a route that would make the check and return a json request with the answer? If the json object says they are new, I would append the modal to the screen dynamically.

Update: I use django's authentication system. Here is an example of logging in. The check would be similar but I would be using model I made in another app that extends Django's base user class. I call it user_profile. I would probably check to see if the user's first name is set. If it isn't I would wanna perform the check.

    def auth_login(request):
    if request.POST:

        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user:
        # the password verified for the user
            if user.is_active:
                print("User is valid, active and authenticated")
                request.session['id'] = user.id
                request.session['email'] = user.email
                login(request, user)
                data = {}
                data['status'] = "login"
                return HttpResponse(json.dumps(data), content_type="application/json")
                #redirect
            else:
                print("The password is valid, but the account has been disabled!")
        else:
            # the authentication system was unable to verify the username and password
            print("The username and password were incorrect.")
            data = {}
            data['status'] = "The username and password are incorrect"
            return HttpResponse(json.dumps(data), content_type="application/json")

    return HttpResponse("hello")
Answers
nr: #1 dodano: 2016-12-31 18:12

One option is to put a model method on your user_profile's model:

class UserProfile(models.Model):
    name = CharField...
    ...other fields...

    def get_is_new(self):
        if self.name is None:  # You could include other checks as well
            return True
        return False

Then you could check in your view like so:

def auth_login(request):
    if request.POST:

        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user:
        # the password verified for the user
            if user.is_active:
                print("User is valid, active and authenticated")
                if user.get_is_new() is True:
                    # Return the modal
                request.session['id'] = user.id
                .......rest of your code..........
nr: #2 dodano: 2017-01-01 18:01

The best way would be to create a custom context processor that would check the current user's registered data and set a boolean value in the context that would be accessible in every single template and view.

It would avoid having to call the code over and over again on all views.

You can read about context processors here: https://docs.djangoproject.com/en/1.10/ref/templates/api/

Source Show
◀ Wstecz