summaryrefslogtreecommitdiff
path: root/scipy/views.py
blob: c48dc11051b50a11fa9ec4ae1f8190772d2b900b (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 HttpResponseRedirect("/2013/accounts/login/?status=in")
        else:
            form = UserLoginForm()

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

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

# User Register View
def user_register(request):
    context = {}
    if request.user.is_anonymous():
        if request.method == 'POST':
            form = UserRegisterForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/2013/accounts/upload-document')
            else:
                context.update(csrf(request))
                context['form'] = form
                return render_to_response('register.html', context)
        else:
            form = UserRegisterForm()
        context.update(csrf(request))
        context['form'] = form
        return render_to_response('register.html', context)
    else:
        return HttpResponseRedirect('/2013/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('/2013/accounts/login?next=/2013/accounts/profile')

# Document Upload View
def upload_document(request):
    allowed_files = ['doc', 'docx', 'txt', 'pdf']
    invalid_file_msg = None
    large_file_msg = None
    context = {}
    context['current_user'] = request.user
    if request.user.is_authenticated():
        if request.method == 'POST':
            form = DocumentUploadForm(request.POST, request.FILES)
            attachment = request.FILES['attachments']
            content_type = attachment.content_type.split('/')[1]
            content_size = attachment.size
            if not content_type in allowed_files:
                invalid_file_msg = "Only PDF, DOC, DOCX & TXT files are allowed"
                context['invalid_file'] = invalid_file_msg
                context['form'] = form
                context.update(csrf(request))
                return render_to_response('upload-document.html', context)
            elif content_size > 5242880:
                large_file_msg = "File size exceeds 5MB"
                context['large_file'] = large_file_msg
                context['form'] = form
                context.update(csrf(request))
                return render_to_response('upload-document.html', context)
            elif form.is_valid():
                data = form.save(commit=False)
                data.user = request.user
                data.verified = False
                data.save()
                return HttpResponseRedirect("/2013/call-for-proposals/?status=up")
            else:
                context = {}
                context.update(csrf(request))
                context['form'] = form
                context['current_user'] = request.user
                return render_to_response('upload-document.html', context)
        else:
            form = DocumentUploadForm()
        context.update(csrf(request))
        context['form'] = DocumentUploadForm()
        return render_to_response('upload-document.html', context)
    else:
        return HttpResponseRedirect('/2013/accounts/login')