90 lines
3.6 KiB
Python
90 lines
3.6 KiB
Python
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')
|
|
|
|
#def check_offer_belong_to_user(request):
|
|
# if request.check_user_is_owner() == True:
|
|
# if offer.author == request.users.id:
|
|
# return True
|
|
# else:
|
|
# raise ValueError("To chyba nie twoje :)")
|
|
|
|
# if check_users_is_owner == True:
|
|
|
|
# Create your views here.
|
|
|
|
class CreateOffer(CreateView):
|
|
|
|
@method_decorator(login_required)
|
|
def get(self, request,*args,**kwargs):
|
|
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)
|
|
|
|
@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.success(request,'Dodaj zdjęcia aby przesłać ofertę do weryfikacji')
|
|
return redirect('/') #Redirect to offer 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.warning(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 |