class ClientView(CreateView):
activate('es')
model = Client
form_class = ClientForm
template_name = 'clientes/clientes.html'
def get(self, request, *args, **kwargs):
self.object = None
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
intr = _('Fill field before continuing')
mail = _('Fill a valid email address')
chk = _('Fill one of the checkbox to a valid state')
dat = _('Please Choose a date')
company = ''
if self.kwargs['company']:
company = self.kwargs['company']
#if company no exist, do not continue
get_object_or_404(GrupoViviendas, nombre__icontains=company)
if Department.objects.filter(grupo_o_administrador__nombre__icontains=company).exists():
company = Department.objects.filter(grupo_o_administrador__nombre__icontains=company)[0].grupo_o_administrador.nombre
grupo = GrupoViviendas.objects.get(nombre__icontains=company)
departments = Department.objects.filter(
grupo_o_administrador__nombre=company).order_by('nombre')
self.extra_context = {"department_list": departments,
'company': company, "introduce": intr,
"mail": mail, "check": chk, "dat": dat }
else:
departments = [{'nombre': 'Vacios'}]
self.extra_context = {"department_list": departments,
'company': company, "introduce": intr,
"mail": mail, "check": chk, "dat": dat }
if 'form' not in kwargs:
kwargs['form'] = self.get_form()
return super().get_context_data(**kwargs)
def form_valid(self, form):
"""If the form is valid, save the associated model."""
# print('casas', form.data['client_house'])
self.object = form.save()
return super().form_valid(form)
# cambiamos algo de info antes de serializar el formulario
def post(self, request, *args, **kwargs):
departement = ''
request.POST._mutable = True
company = self.kwargs['company']
company = GrupoViviendas.objects.get(nombre__icontains=company)
depa = company.company_related.all()
if request.POST['client_house']:
for dep in depa:
print('introducido',str(request.POST['client_house']).lower())
print('iterador',str(dep.nombre).lower())
if str(request.POST['client_house']).lower() in str(dep.nombre).lower() :
departement = dep.nombre
print("departamento", departement)
dp = get_object_or_404(Department, nombre=departement)
request.POST[
'client_house'] =
dp.id
self.object = None
return super().post(request, *args, **kwargs)
def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
if not form.is_valid():
messages.error(self.request, 'Form invalid.')
return self.render_to_response(self.get_context_data(form=form))
def get_success_url(self):
self.object.client_code_language = self.request.LANGUAGE_CODE
self.object.save()
handler = HandlerTicket(self.object)
sn = handler.get_sn()
company = self.kwargs['company']
return reverse_lazy('clientes:review_reserva', args=[pk, company])
def review_ticket_by_client(request, pk, company):
""" Render review form with data of rate """
client = Client.objects.filter(pk=pk)[0]
result = serialize(
'json', TService.objects.filter(
client=
client.pk))
# result = TService.objects.filter(client=client.pk) debug = settings.DEBUG
key = settings.STRIPE_PUBLISHABLE_KEY
tickets = client.get_tkclient.all()
trip_ida = 0
trip_vuelta = 0
tarifas_depto = client.client_house.tarifa
for tk in tickets:
if tk.car_type == 'Turismo':
if tk.tstate == 'ida':
trip_ida += tarifas_depto.precio_turismo
else:
trip_vuelta += tarifas_depto.precio_turismo
if tk.car_type == 'Turismo XL':
if tk.tstate == 'ida':
trip_ida += tarifas_depto.precio_turismo
else:
trip_vuelta += tarifas_depto.precio_turismo
if tk.car_type == 'Minivan':
if tk.tstate == 'ida':
trip_ida += tarifas_depto.precio_minivan
else:
trip_vuelta += tarifas_depto.precio_minivan
total_trip = trip_ida + trip_vuelta
return render(request, 'clientes/data_confir.html', {'total_trip': total_trip,
'trip_ida': trip_ida,
'trip_vuelta': trip_vuelta,
'client': client,
'company': company,
'debug': debug,
'result': result,
'key': key })