from django.shortcuts import render from .forms import CustomUserCreationForm, LoginForm from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from .models import Base_User,OwnerUser from django.views.generic import ( View, CreateView, FormView ) from django.contrib.auth import authenticate,login,logout from django.conf import settings from django.http import HttpResponseRedirect from django.contrib import messages from camper.models import Offer from reservation.models import Reservation from django.contrib.auth import views as auth_views # 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 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 @method_decorator(login_required) def get(self,request): self.Base_User = request.user return render(request,'users/profile.html', {'Base_User':Base_User}) 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}) class EditProfile(): #edycja danych w profilu - zmiana hasłą + zmiana danych pass 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) class Login_View(auth_views.LoginView): template_name = 'users/login.html' success_url = '/profile' def logout_view(request): logout(request) messages.success(request,'Zostałeś poprawnie wylogowany') return HttpResponseRedirect('/')