2020-12-07 21:41:55 +01:00
|
|
|
from django.shortcuts import render
|
2021-02-05 18:05:47 +01:00
|
|
|
from .forms import CustomUserCreationForm, LoginForm
|
2020-12-07 21:41:55 +01:00
|
|
|
from django.shortcuts import render, redirect
|
2021-01-27 20:09:55 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.decorators import method_decorator
|
2021-02-05 18:05:47 +01:00
|
|
|
from .models import Base_User,OwnerUser
|
2021-01-27 20:09:55 +01:00
|
|
|
from django.views.generic import (
|
|
|
|
View,
|
2021-02-05 18:05:47 +01:00
|
|
|
CreateView,
|
|
|
|
FormView
|
2021-01-27 20:09:55 +01:00
|
|
|
)
|
2021-02-05 18:05:47 +01:00
|
|
|
from django.contrib.auth import authenticate,login,logout
|
2021-01-27 20:09:55 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.contrib import messages
|
|
|
|
from camper.models import Offer
|
2021-02-05 18:05:47 +01:00
|
|
|
from reservation.models import Reservation
|
|
|
|
from django.contrib.auth import views as auth_views
|
2020-12-07 21:41:55 +01:00
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
#REJESTRACJA BASE USERA NASTĘPNIE WYBOR JAKI RODZAJ KONTA UZYTKOWNIK CCHE ZALOZYC
|
|
|
|
# * W PRZYPADKU Klikniecia 'Wynajmij swoj kamper' autoatmtycznie wybierz rodzaj konta na OwnerUser
|
|
|
|
|
|
|
|
|
2021-01-27 20:09:55 +01:00
|
|
|
|
|
|
|
class ProfileView(View):
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["offers"] = Offer.objects.filter(created_by = self.Base_User)
|
|
|
|
return context
|
|
|
|
|
2021-02-05 18:05:47 +01:00
|
|
|
@method_decorator(login_required)
|
2021-01-27 20:09:55 +01:00
|
|
|
def get(self,request):
|
|
|
|
self.Base_User = request.user
|
|
|
|
return render(request,'users/profile.html', {'Base_User':Base_User})
|
|
|
|
|
2021-02-05 18:05:47 +01:00
|
|
|
def user_profile(request):
|
|
|
|
user = request.user
|
|
|
|
template_name = 'users/profile.html'
|
|
|
|
if user.status == ' Company_Account' or user.status =='Moderator':
|
|
|
|
user_offers = Offer.objects.filter(created_by = request.user).order_by('-date_added')
|
|
|
|
user_reservations = Reservation.objects.filter(user = request.user).order_by('-reservation_start_date')
|
|
|
|
return render(request,template_name, {'user':user, 'user_offers':user_offers, 'user_reservations':user_reservations})
|
|
|
|
if user.status == 'BaseAcc':
|
|
|
|
user_reservations = Reservation.objects.filter(user = request.user).order_by('-reservation_start_date')
|
|
|
|
return render(request,template_name, {'user':user,'user_reservation':user_reservations})
|
2021-01-27 20:09:55 +01:00
|
|
|
|
|
|
|
class EditProfile(): #edycja danych w profilu - zmiana hasłą + zmiana danych
|
|
|
|
pass
|
2021-02-05 18:05:47 +01:00
|
|
|
class Register_View(FormView):
|
|
|
|
|
|
|
|
def get(self,request,*args,**kwargs):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
return redirect('/profile')
|
|
|
|
else:
|
|
|
|
context = {}
|
|
|
|
form = CustomUserCreationForm()
|
|
|
|
context['form'] = form
|
|
|
|
return render(request,'users/register.html',context)
|
|
|
|
|
|
|
|
def post(self,request,*args,**kwargs):
|
|
|
|
context = {}
|
|
|
|
if request.POST:
|
|
|
|
form = CustomUserCreationForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
email = form.cleaned_data.get('email')
|
|
|
|
raw_password = form.cleaned_data.get('password1')
|
|
|
|
account = authenticate(email = email, password = raw_password)
|
|
|
|
login(request,account)
|
|
|
|
return redirect('/')
|
|
|
|
else:
|
|
|
|
context['form'] = form
|
|
|
|
else:
|
|
|
|
form = CustomUserCreationForm()
|
|
|
|
context['form'] = form
|
|
|
|
return render(request,'users/register.html', context)
|
2021-01-27 20:09:55 +01:00
|
|
|
|
2021-02-05 18:05:47 +01:00
|
|
|
class Login_View(auth_views.LoginView):
|
|
|
|
template_name = 'users/login.html'
|
|
|
|
success_url = '/profile'
|
2021-01-27 20:09:55 +01:00
|
|
|
def logout_view(request):
|
|
|
|
logout(request)
|
|
|
|
messages.success(request,'Zostałeś poprawnie wylogowany')
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
|