Пример #1
0
def create_account(request):
    context_dict = base_context()
    JSON_ELC = str(
        EDUCATION_LEVEL_CHOICES
    )  # JSON_ELC is a version of EDUCATION_LEVEL_CHOICES converted into a format useable by the Facebook registration plugin (converts from tuple to dictionary).
    JSON_ELC_LIST = JSON_ELC[:-1].split(',')
    JSON_ELC = '{'
    for index in range(0, len(JSON_ELC_LIST), 2):
        key = JSON_ELC_LIST[index][2:]
        value = JSON_ELC_LIST[index + 1][:-1]
        JSON_ELC += key + ':' + value + ','

    JSON_ELC += '}'
    context_dict['EDUCATION_LEVEL_CHOICES'] = mark_safe(
        JSON_ELC
    )  # Mark_safe is used to stop django auto-escaping quotation marks.

    if request.is_ajax():
        response = {'username': True, 'email': False}
        try:
            User.objects.get(username=request.GET['username'])
        except:
            response['username'] = False

        return HttpResponse(json.dumps(response), mimetype="application/json")

    template = loader.get_template('userprofiles/create_account.html')
    context = RequestContext(request, context_dict)

    return HttpResponse(template.render(context))
Пример #2
0
def create_account(request):
    context_dict = base_context()
    JSON_ELC = str(
        EDUCATION_LEVEL_CHOICES
    )  # JSON_ELC is a version of EDUCATION_LEVEL_CHOICES converted into a format useable by the Facebook registration plugin (converts from tuple to dictionary).
    JSON_ELC_LIST = JSON_ELC[:-1].split(",")
    JSON_ELC = "{"
    for index in range(0, len(JSON_ELC_LIST), 2):
        key = JSON_ELC_LIST[index][2:]
        value = JSON_ELC_LIST[index + 1][:-1]
        JSON_ELC += key + ":" + value + ","

    JSON_ELC += "}"
    context_dict["EDUCATION_LEVEL_CHOICES"] = mark_safe(
        JSON_ELC
    )  # Mark_safe is used to stop django auto-escaping quotation marks.

    if request.is_ajax():
        response = {"username": True, "email": False}
        try:
            User.objects.get(username=request.GET["username"])
        except:
            response["username"] = False

        return HttpResponse(json.dumps(response), mimetype="application/json")

    template = loader.get_template("userprofiles/create_account.html")
    context = RequestContext(request, context_dict)

    return HttpResponse(template.render(context))
Пример #3
0
def account(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(HOME_URL + 'login/?next=' + HOME_URL +
                                    'account/')

    user = request.user
    user_profile = user.profile
    context_dict = base_context()

    if request.POST:
        uform = UserModelForm(request.POST)
        pform = ProfileModelForm(request.POST)
        print uform.errors
        print pform.errors
        if uform.is_valid() and pform.is_valid():

            user.first_name = uform.cleaned_data['first_name']
            user.last_name = uform.cleaned_data['last_name']
            user_profile.location = pform.cleaned_data['location']
            user_profile.dob = pform.cleaned_data['dob']
            user_profile.institute_enrolled_in = pform.cleaned_data[
                'institute_enrolled_in']
            user_profile.course_enrolled_in = pform.cleaned_data[
                'course_enrolled_in']
            user_profile.academic_level = pform.cleaned_data['academic_level']
            user.save()
            user_profile.save()

    uform = UserModelForm(
        initial={
            'username': user.username,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'email': user.email,
        })  # Form for editing basic account details.
    pform = ProfileModelForm(
        initial={
            'dob': user.profile.dob,
            'location': user.profile.location,
            'institute_enrolled_in': user.profile.institute_enrolled_in,
            'course_enrolled_in': user.profile.course_enrolled_in,
            'academic_level': user.profile.academic_level
        })

    context_dict['uform'] = uform
    context_dict['pform'] = pform
    context_dict['article_set'] = user.articles.all().order_by('-upload_date')
    template = loader.get_template('userprofiles/account.html')
    context = RequestContext(request, context_dict)

    if request.is_ajax():
        template = loader.get_template(
            'userprofiles/account_articles_page.html')
    return HttpResponse(template.render(context))
Пример #4
0
def account(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(HOME_URL + "login/?next=" + HOME_URL + "account/")

    user = request.user
    user_profile = user.profile
    context_dict = base_context()

    if request.POST:
        uform = UserModelForm(request.POST)
        pform = ProfileModelForm(request.POST)
        print uform.errors
        print pform.errors
        if uform.is_valid() and pform.is_valid():

            user.first_name = uform.cleaned_data["first_name"]
            user.last_name = uform.cleaned_data["last_name"]
            user_profile.location = pform.cleaned_data["location"]
            user_profile.dob = pform.cleaned_data["dob"]
            user_profile.institute_enrolled_in = pform.cleaned_data["institute_enrolled_in"]
            user_profile.course_enrolled_in = pform.cleaned_data["course_enrolled_in"]
            user_profile.academic_level = pform.cleaned_data["academic_level"]
            user.save()
            user_profile.save()

    uform = UserModelForm(
        initial={
            "username": user.username,
            "first_name": user.first_name,
            "last_name": user.last_name,
            "email": user.email,
        }
    )  # Form for editing basic account details.
    pform = ProfileModelForm(
        initial={
            "dob": user.profile.dob,
            "location": user.profile.location,
            "institute_enrolled_in": user.profile.institute_enrolled_in,
            "course_enrolled_in": user.profile.course_enrolled_in,
            "academic_level": user.profile.academic_level,
        }
    )

    context_dict["uform"] = uform
    context_dict["pform"] = pform
    context_dict["article_set"] = user.articles.all().order_by("-upload_date")
    template = loader.get_template("userprofiles/account.html")
    context = RequestContext(request, context_dict)

    if request.is_ajax():
        template = loader.get_template("userprofiles/account_articles_page.html")
    return HttpResponse(template.render(context))
Пример #5
0
def login_view(request):

    if request.user.is_authenticated():
        print "USER ALREADY LOGGED IN"
        return HttpResponseRedirect(HOME_URL)

    context_dict = base_context()

    user = None
    redirect_to = None

    if request.GET and request.GET["next"] != "":
        redirect_to = request.GET["next"]
        context_dict["redirect_to"] = redirect_to

    if request.POST:
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                if redirect_to is None:
                    return HttpResponseRedirect("/")
                else:
                    return HttpResponseRedirect(redirect_to)
            else:
                pass  # Return a 'disabled account' error message
        else:
            error_message = "Invalid login: Please enter a correct username and password combination. Note that both fields are case-sensitive."
            context_dict["error_message"] = error_message
            pass  # Return an 'invalid login' error message.

    if (
        user != None
    ):  # Doesn't do anything currently. User is already passed to template, checked for using user.is_anonymous.
        if request.user.is_authenticated():
            print "--------------------------------->>>>>>>>>>>>>User:"******"userprofiles/login.html")
    context = RequestContext(request, context_dict)
    return HttpResponse(template.render(context))
Пример #6
0
def login_view(request):

    if request.user.is_authenticated():
        print 'USER ALREADY LOGGED IN'
        return HttpResponseRedirect(HOME_URL)

    context_dict = base_context()

    user = None
    redirect_to = None

    if request.GET and request.GET['next'] != '':
        redirect_to = request.GET['next']
        context_dict['redirect_to'] = redirect_to

    if request.POST:
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                if redirect_to is None:
                    return HttpResponseRedirect('/')
                else:
                    return HttpResponseRedirect(redirect_to)
            else:
                pass  # Return a 'disabled account' error message
        else:
            error_message = "Invalid login: Please enter a correct username and password combination. Note that both fields are case-sensitive."
            context_dict["error_message"] = error_message
            pass  # Return an 'invalid login' error message.

    if user != None:  # Doesn't do anything currently. User is already passed to template, checked for using user.is_anonymous.
        if request.user.is_authenticated():
            print '--------------------------------->>>>>>>>>>>>>User:'******'authenticated_user'] = user
        else:
            pass  # Do something for anonymous users.

    template = loader.get_template('userprofiles/login.html')
    context = RequestContext(request, context_dict)
    return HttpResponse(template.render(context))