views.py 1.64 KB
Newer Older
Skia's avatar
Skia committed
1
from django.shortcuts import render, redirect, get_object_or_404
Skia's avatar
Skia committed
2
from django.http import HttpResponse
Skia's avatar
Skia committed
3
from django.contrib.auth import logout as auth_logout
Skia's avatar
Skia committed
4
5

from .models import User
Skia's avatar
Skia committed
6
from .forms import RegisteringForm, LoginForm
Skia's avatar
Skia committed
7
8
9
10
11
12

import logging

logging.basicConfig(level=logging.DEBUG)

def index(request):
Skia's avatar
Skia committed
13
    return render(request, "core/index.html", {'title': 'Bienvenue!'})
Skia's avatar
Skia committed
14
15

def register(request):
Skia's avatar
Skia committed
16
    context = {'title': 'Register a user'}
Skia's avatar
Skia committed
17
18
19
    if request.method == 'POST':
        form = RegisteringForm(request.POST)
        if form.is_valid():
Skia's avatar
Skia committed
20
21
            logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name'])
            u = form.save()
Skia's avatar
Skia committed
22
            context['user_registered'] = u
Skia's avatar
Skia committed
23
24
25
            form = RegisteringForm()
    else:
        form = RegisteringForm()
Skia's avatar
Skia committed
26
27
    context['form'] = form.as_p()
    return render(request, "core/register.html", context)
Skia's avatar
Skia committed
28
29
30
31
32
33
34
35
36
37
38
39
40

def login(request):
    if request.method == 'POST':
        try:
            form = LoginForm(request)
            form.login()
            # TODO redirect to profile when done
            return redirect('index')
        except Exception as e:
            logging.debug(e)
    else:
        form = LoginForm()
    return render(request, "core/login.html", {'title': 'Login', 'form': form.as_p()})
Skia's avatar
Skia committed
41

Skia's avatar
Skia committed
42
43
44
def logout(request):
    auth_logout(request)
    return redirect('core:index')
Skia's avatar
Skia committed
45
46
47
48
49
50
51
52
53
54

def user(request, user_id=None):
    if user_id == None:
        return render(request, "core/user.html", {'user_list': User.objects.all})
    user = get_object_or_404(User, pk=user_id)
    return render(request, "core/user.html", {'profile': user})

def user_edit(request, user_id):
    pass