summaryrefslogtreecommitdiff
path: root/scipy/views.py
blob: cccbdd24c5f6387e92ebdbc47d9276a7d8730b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from django.http import HttpResponse, HttpResponseRedirect
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

from scipy.forms import UserLoginForm, UserRegisterForm, UserProfileForm, DocumentUploadForm

# User Login View
def user_login(request):
    if request.user.is_anonymous():
        if request.method == '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)
                    return HttpResponseRedirect(request.POST['next'])
                else:
                    return HttpResponse("Not active")
            else:
                return HttpResponse("Invalid username or password")
        else:
            form = UserLoginForm()

        next = '/accounts/upload-document/'
        if 'next' in request.GET:
            next = request.GET['next']

        context = {}
        context.update(csrf(request))
        context['form'] = form
        context['next'] = next
        return render_to_response('login.html', context)
    else:
        return HttpResponseRedirect('/accounts/upload-document')

# User Logout View
def user_logout(request):
    logout(request)
    return HttpResponseRedirect('/2013')

# User Register View
def user_register(request):
    if request.user.is_anonymous():
        if request.method == 'POST':
            form = UserRegisterForm(request.POST)
            if form.is_valid:
                form.save()
            return HttpResponseRedirect('/accounts/upload-document')
        else:
            form = UserRegisterForm()
        context = {}
        context.update(csrf(request))
        context['form'] = form
        return render_to_response('register.html', context)
    else:
        return HttpResponseRedirect('/accounts/upload-document')

# User Profile View
def user_profile(request):
    if request.user.is_authenticated():
        context = {}
        context.update(csrf(request))
        context['form'] = UserProfileForm(instance=request.user)
        return render_to_response('profile.html', context)
    else:
        return HttpResponseRedirect('/accounts/login?next=/accounts/profile')

# Document Upload View
@login_required
def upload_document(request):
    if request.method == 'POST':
        form = DocumentUploadForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.save(commit=False)
            data.user = request.user
            data.verified = False
            data.save()
            return HttpResponseRedirect("/2013/call-for-papers/?status=up")
        else:
            context = {}
            context.update(csrf(request))
            context['form'] = form
            return render_to_response('upload-document.html', context)
    else:
        form = DocumentUploadForm()
        
    context = {}
    context.update(csrf(request))
    context['form'] = DocumentUploadForm()
    return render_to_response('upload-document.html', context)