views.py 2.16 KB
Newer Older
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
1 2 3 4 5 6 7
#!/usr/bin/env python

# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.translation import gettext_lazy as _
8
from django.views.generic import CreateView, ListView, DetailView
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
9
from django.http import HttpResponseRedirect
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
10 11
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
pa's avatar
pa committed
12
from django.db.models import Q
13 14 15

from .models import Profile, Club
from .forms import ProfileForm, ClubForm
pa's avatar
pa committed
16
from note.models.transactions import Transaction
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
17

Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
18
class UserCreateView(CreateView):
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    """
    Une vue pour inscrire un utilisateur et lui créer un profile

    """
    form_class = ProfileForm
    success_url = reverse_lazy('login')
    template_name ='member/signup.html'
    second_form = UserCreationForm

    def get_context_data(self,**kwargs):
        context = super(SignUp,self).get_context_data(**kwargs)
        context["user_form"] = self.second_form

        return context
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
33 34 35 36 37 38 39 40 41

    def form_valid(self, form):
        user_form = UserCreationForm(self.request.POST)
        if user_form.is_valid():
            user = user_form.save()
            user_profile = form.save(commit=False) # do not save to db
            user_profile.user = user
            user_profile.save()
        return super().form_valid(form)
42 43


Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
44 45
class UserDetailView(LoginRequiredMixin,DetailView):
    model = Profile
pa's avatar
pa committed
46 47 48 49 50 51 52 53
    
    def get_context_data(slef,**kwargs):
        context = super().get_context_data(**kwargs)
        user = context['object'].user.note
        user_transactions = \
                Transaction.objects.all().filter(Q(source=user) | Q(destination=user))
        context['history_list'] = user_transactions
        return context
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
54 55


56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
class ClubCreateView(LoginRequiredMixin,CreateView):
    """
    Create Club
    """
    model = Club
    form_class = ClubForm

    def form_valid(self,form):
        return super().form_valid(form)
   
class ClubListView(LoginRequiredMixin,ListView):
    """
    List TransactionsTemplates
    """
    model = Club
    form_class = ClubForm
Pierre-antoine Comby's avatar
Pierre-antoine Comby committed
72

73 74 75 76
class ClubDetailView(LoginRequiredMixin,DetailView):
    """
    """
    model = Club