88 lines
3.9 KiB
Python
Executable File
88 lines
3.9 KiB
Python
Executable File
from django.shortcuts import render,redirect,get_object_or_404
|
|
from camper.models import Offer,Offer_Category,Image_offer
|
|
from users.models import Base_User
|
|
from django.views.generic import (
|
|
CreateView,
|
|
UpdateView,
|
|
DeleteView,
|
|
)
|
|
|
|
from camper.forms import OfferCreateForm,OfferImageCreateForm,OfferImageFormSet
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.utils.decorators import method_decorator
|
|
from django.contrib import messages
|
|
from django.http import HttpResponseRedirect
|
|
import users
|
|
|
|
|
|
def check_user_is_auth(request):
|
|
if request.users.Base_user.is_authenticated():
|
|
return True
|
|
else:
|
|
render (request,'signup.html')
|
|
|
|
def check_user_is_owner(request):
|
|
if check_user_is_auth == True:
|
|
if request.users.Base_user.status =="Company_Account": # check if user has status 'owner':
|
|
pass
|
|
else:
|
|
render (request,'upgrade-to-onwer.html')
|
|
|
|
|
|
class CreateOffer(CreateView):
|
|
|
|
@method_decorator(login_required)
|
|
def get(self, request,*args,**kwargs):
|
|
if check_user_is_owner(request) == True or request.user.is_staff == True:
|
|
form = OfferCreateForm()
|
|
formset = OfferImageFormSet()
|
|
context = {
|
|
'form': form,
|
|
'formset': formset,
|
|
}
|
|
#if check_user_is_owner(request) == True or request.Base_user.is_staff == True:
|
|
return render(request,'offer/add-offer.html', context)
|
|
else:
|
|
messages.error(request, 'Nie masz uprawnień!')
|
|
return redirect('/') #Redirect to login / register page for owner
|
|
|
|
@method_decorator(login_required)
|
|
def post(self,request,*args,**kwargs):
|
|
if check_user_is_owner(request) == True or request.user.is_staff == True:
|
|
if request.method == 'POST':
|
|
form = OfferCreateForm(request.POST)
|
|
formset = OfferImageFormSet(request.POST or None, request.FILES or None)
|
|
if form.is_valid() and formset.is_valid():
|
|
offer = form.save(commit = False)
|
|
offer.created_by = request.user
|
|
offer.status = 'Verification'
|
|
offer.save()
|
|
for photo in formset:
|
|
try:
|
|
imageoffer = Image_offer(offer = offer,main_image = photo.cleaned_data['main_image'],image = photo.cleaned_data['image'])
|
|
imageoffer.save()
|
|
except Exception:
|
|
offer.status = 'Inactive'
|
|
offer.save()
|
|
messages.error(request,'Dodaj zdjęcia aby przesłać ofertę do weryfikacji')
|
|
return redirect('/') #Redirect to offe-update-page or user's offers list
|
|
messages.success(request,'Oferta utworzona i przesłana do weryfikacji')
|
|
return redirect('/') #Redirect to offer page or user's offers list
|
|
else:
|
|
messages.error(request, 'Wprowadzone dane są nieprawidłowe.')
|
|
return render(request,'offer/add-offer.html', {'form': form, 'formset': formset} )
|
|
else:
|
|
form = OfferCreateForm()
|
|
formset = OfferImageFormSet()
|
|
return render(request,'offer/add-offer.html', {'form': form, 'formset': formset} )
|
|
|
|
else:
|
|
messages.error(request, 'Nie masz uprawnień!')
|
|
return redirect('/') #Redirect to login / register page for owner
|
|
|
|
|
|
class EditOffer(UpdateView):
|
|
model = Offer
|
|
template_name = 'offer/update-offer.html'
|
|
fields = ('title','category','price','description','DMC','Places_to_sleep','cooker','fridge', 'microwave', 'Webasto', 'auto_temp_control', 'water_level_indicator', 'battery_level', 'photovoltaic_panels', 'gearbox', 'tempomat')
|