reg/views.py
author nishanth
Mon, 12 Apr 2010 04:23:13 +0530
changeset 10 c52d170969f0
parent 9 e29ecb7819e7
child 11 334550460bd7
permissions -rw-r--r--
quite a few changes. modified models and feedback views .
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     1
from django.contrib.auth.models import User
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     2
from django.contrib.auth import authenticate, login, logout
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     3
from django.contrib.auth.decorators import login_required
2
c11afa8623f7 incorporated gen_key .
nishanth
parents: 0
diff changeset
     4
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     5
from django.shortcuts import render_to_response, redirect
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
     6
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
     7
from workshop.reg.models import Event
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
     8
from workshop.reg import forms as reg_forms
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
     9
from workshop.reg import events as reg_events
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    10
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    11
from django.http import HttpResponse
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    12
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    13
def homepage(request):
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    14
    """ see if the user is active.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    15
    If not, only show the re send activation email link.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    16
    else show all the options in homepage.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    17
    """
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    18
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    19
    user = request.user
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    20
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    21
    events = Event.objects.all()[:10]
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    22
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    23
def user_login(request):
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    24
    """ get the user object from e-mail and then check for password.
2
c11afa8623f7 incorporated gen_key .
nishanth
parents: 0
diff changeset
    25
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents: 0
diff changeset
    26
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    27
    user = request.user
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    28
    if user.is_authenticated():
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    29
        return redirect('/reg')
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    30
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    31
    if request.method == "POST":
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    32
        form = reg_forms.LoginForm(request.POST)
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    33
        if form.is_valid():
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    34
            email = form.cleaned_data['email']
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    35
            password = form.cleaned_data['password']
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    36
            username = User.objects.get(email__iexact=email).username
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    37
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    38
            user = authenticate(username=username, password=password)
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    39
            login(request, user)
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    40
            return redirect('/reg')
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    41
        else:
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    42
            return render_to_response('login.html', {'form':form})
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    43
    else:
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    44
        form = reg_forms.LoginForm()
3
182f216da4a8 made the login view. have to write templates and check it.
nishanth
parents: 2
diff changeset
    45
        return render_to_response('login.html', {'form':form})
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    46
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    47
def user_logout(request):
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    48
    """ simply logout the user and redirect to homepage.
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    49
    """
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    50
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    51
    logout(request)
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    52
    return redirect('/reg')
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    53
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    54
def user_register(request):
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    55
    """ take the credentials like name, college and gender here itself.
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    56
    """
4
ededea9ad08b login and logout works .
nishanth
parents: 3
diff changeset
    57
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    58
    if request.method == "POST":
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    59
        form = reg_forms.RegisterForm(request.POST)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    60
        if form.is_valid():
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    61
            data = form.cleaned_data
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    62
            reg_events.create_user(email=data['email'], 
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    63
                                   password=data['password'],
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    64
                                   firstname=data['first_name'],
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    65
                                   lastname=data['last_name'], 
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    66
                                   gender=data['gender'], 
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    67
                                   profession=data['profession'], 
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    68
                                   affiliated_to=data['affiliated_to'], 
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    69
                                   interests=data['interests']
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    70
                                  )
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    71
            return render_to_response('account_created.html')
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    72
        else:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    73
            return render_to_response('register.html', {'form':form})
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    74
    else:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    75
        form = reg_forms.RegisterForm()
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 5
diff changeset
    76
        return render_to_response('register.html', {'form':form})
8
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    77
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    78
def create_event(request):
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    79
    """ see if the user is a staff and only then let him do it.
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    80
    """
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    81
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    82
    user = request.user
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    83
    if user.is_authenticated() and user.is_staff:
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    84
        if request.method ==  "POST":
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    85
            form = reg_forms.EventCreateForm(request.POST)
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    86
            if form.is_valid():
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    87
                data = form.cleaned_data
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    88
                new_event = reg_events.create_event(title=data['title'],
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    89
                                                    description=data['description'],
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    90
                                                    start_date=data['start_date'],
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    91
                                                    stop_date=data['stop_date'],
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    92
                                                    created_by=user,
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    93
                                                   )
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    94
                event_url = "/reg/event/view/%s"%(new_event.key)
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    95
                return redirect(event_url)
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    96
            else:
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    97
                return render_to_response('event_create.html', {'form':form})
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    98
        else:
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
    99
            form = reg_forms.EventCreateForm()
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   100
            return render_to_response('event_create.html', {'form':form})
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   101
    else:
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   102
        return redirect('/reg')
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   103
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   104
def view_event(request, key):
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   105
    """ get the event by its key and display it.
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   106
    """
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   107
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   108
    user = request.user
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   109
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   110
    try:
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   111
        event = Event.objects.get(key__iexact=key)
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   112
    except Event.DoesNotExist:
10
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   113
        return redirect("/reg")
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   114
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   115
    is_guest = False if user.is_authenticated() else True
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   116
    is_attendee = True if user in event.attendees.all() else False
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   117
    is_org = True if user in event.organizers.all() else False
8
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   118
10
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   119
    context = {'is_guest': is_guest,
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   120
               'is_attendee': is_attendee,
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   121
               'is_org': is_org,
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   122
              }
c52d170969f0 quite a few changes. modified models and feedback views .
nishanth
parents: 9
diff changeset
   123
    return render_to_response('view_event.html', context)
8
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 6
diff changeset
   124
9
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   125
def reset_password(request):
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   126
    """ check for the existance of e-mail.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   127
    Then call the event.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   128
    """
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   129
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   130
    user = request.user
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   131
    if user.is_authenticated():
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   132
        return redirect('/reg')
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   133
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   134
    if request.method == "POST":
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   135
        form = reg_forms.PasswordResetForm(request.POST)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   136
        if form.is_valid():
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   137
            email = form.cleaned_data['email']
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   138
            user = User.objects.get(email__iexact=email)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   139
            new_password = reg_events.reset_password(user)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   140
            return render_to_response('password_reset.html', {'new_password':new_password})
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   141
        else:
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   142
            return render_to_response('password_reset.html', {'form':form})
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   143
    else:
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   144
        form = reg_forms.PasswordResetForm()
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   145
        return render_to_response('password_reset.html', {'form':form})
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   146
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   147
def change_password(request):
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   148
    """ check for the password and then set the new password.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   149
    """
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   150
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   151
    user = request.user
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   152
    if not user.is_authenticated():
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   153
        return redirect('/reg')
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   154
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   155
    if request.method == "POST":
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   156
        data = request.POST.copy()
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   157
        data.__setitem__('username', user.username)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   158
        form = reg_forms.PasswordChangeForm(data)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   159
        if form.is_valid():
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   160
            new_password = form.cleaned_data['new_password']
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   161
            reg_events.change_password(user, new_password)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   162
            return render_to_response('password_change.html', {'password_changed': True})
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   163
        else:
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   164
            return render_to_response('password_change.html', {'form':form})
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   165
    else:
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   166
        form = reg_forms.PasswordChangeForm()
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   167
        return render_to_response('password_change.html', {'form':form})
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   168
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   169
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   170
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   171
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   172
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   173
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   174