You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
6.3 KiB
206 lines
6.3 KiB
# -*- coding: utf-8 -*-
|
|
from django.views.generic import CreateView, UpdateView, DeleteView, ListView, FormView, DetailView
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
from django.utils.translation import ugettext as _
|
|
from .forms import *
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from django.conf import settings
|
|
from django.shortcuts import get_object_or_404
|
|
from service.order_forms import TranslationForm
|
|
from django.http import HttpResponseRedirect, Http404
|
|
from .models import _city, _country
|
|
|
|
|
|
# =========== ADMIN VIEWS ===========
|
|
|
|
# Specialist views
|
|
|
|
|
|
class SpecialistCreateView(CreateView):
|
|
form_class = SpecialistForm
|
|
model = Specialist
|
|
template_name = 'admin/specialist/specialist_new.html'
|
|
success_url = reverse_lazy("specialist_all")
|
|
|
|
|
|
class SpecialistListView(ListView):
|
|
model = Specialist
|
|
template_name = 'admin/specialist/specialist_all.html'
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
|
|
|
|
class SpecialistUpdateView(UpdateView):
|
|
form_class = SpecialistForm
|
|
model = Specialist
|
|
template_name = 'admin/specialist/specialist_new.html'
|
|
success_url = reverse_lazy("specialist_all")
|
|
|
|
def get_form(self, form_class):
|
|
form = super(SpecialistUpdateView, self).get_form(form_class)
|
|
form.fields['city'].widget.attrs['data-init-text'] = self.object.city.name
|
|
return form
|
|
|
|
|
|
class SpecialistDeleteView(DeleteView):
|
|
model = Specialist
|
|
template_name = 'admin/specialist/specialist_confirm_delete.html'
|
|
success_url = reverse_lazy("specialist_all")
|
|
|
|
|
|
# Catalog views
|
|
|
|
|
|
class CatalogCreateView(CreateView):
|
|
form_class = SpecialistCatalogForm
|
|
model = SpecialistCatalog
|
|
template_name = 'admin/specialist/catalog_new.html'
|
|
success_url = reverse_lazy("catalog_all")
|
|
|
|
|
|
class CatalogListView(ListView):
|
|
model = SpecialistCatalog
|
|
template_name = 'admin/specialist/catalog_all.html'
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
|
|
|
|
class CatalogCityView(ListView):
|
|
model = SpecialistCatalog
|
|
template_name = 'admin/specialist/catalog_all.html'
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
|
|
def get_queryset(self):
|
|
qs = super(CatalogCityView, self).get_queryset()
|
|
return qs.filter(type=2)
|
|
|
|
|
|
class CatalogCountryView(ListView):
|
|
model = SpecialistCatalog
|
|
template_name = 'admin/specialist/catalog_all.html'
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
|
|
def get_queryset(self):
|
|
qs = super(CatalogCountryView, self).get_queryset()
|
|
return qs.filter(type=1)
|
|
|
|
|
|
class CatalogUpdateView(UpdateView):
|
|
form_class = SpecialistCatalogForm
|
|
model = SpecialistCatalog
|
|
template_name = 'admin/specialist/catalog_new.html'
|
|
success_url = reverse_lazy("catalog_all")
|
|
|
|
def get_form(self, form_class):
|
|
form = super(CatalogUpdateView, self).get_form(form_class)
|
|
if self.object.type is 2: # city
|
|
form.fields['city'].widget.attrs['data-init-text'] = self.object.city.name
|
|
return form
|
|
|
|
|
|
class CatalogDeleteView(DeleteView):
|
|
model = SpecialistCatalog
|
|
template_name = 'admin/specialist/catalog_confirm_delete.html'
|
|
success_url = reverse_lazy("catalog_all")
|
|
|
|
|
|
# Feedback views
|
|
|
|
|
|
class FeedbackCreateView(CreateView):
|
|
form_class = FeedbackForm
|
|
model = Feedback
|
|
template_name = 'admin/specialist/feedback_new.html'
|
|
success_url = reverse_lazy("catalog_all")
|
|
|
|
def get_initial(self):
|
|
catalog = get_object_or_404(SpecialistCatalog, pk=self.kwargs.get('catalog_pk'))
|
|
return {'catalog': catalog}
|
|
|
|
|
|
class FeedbackUpdateView(UpdateView):
|
|
form_class = FeedbackForm
|
|
model = Feedback
|
|
template_name = 'admin/specialist/feedback_new.html'
|
|
success_url = reverse_lazy("catalog_all")
|
|
|
|
def get_initial(self):
|
|
catalog = get_object_or_404(SpecialistCatalog, pk=self.kwargs.get('catalog_pk'))
|
|
return {'catalog': catalog}
|
|
|
|
|
|
class FeedbackDeleteView(DeleteView):
|
|
model = Feedback
|
|
template_name = 'admin/specialist/feedback_confirm_delete.html'
|
|
success_url = reverse_lazy("catalog_all")
|
|
|
|
|
|
# ========= CLIENT VIEWS ============
|
|
|
|
|
|
class CatalogDetailedView(SingleObjectMixin, FormView):
|
|
model = SpecialistCatalog
|
|
form_class = TranslationForm
|
|
template_name = "client/specialist_catalog/catalog_detailed.html"
|
|
success_url = reverse_lazy("service_thanks")
|
|
|
|
def get_catalog_obj(self):
|
|
if self.kwargs.get('type') is "country":
|
|
try:
|
|
return self.model.objects.language().get(type=1, country__url=self.kwargs.get('slug'))
|
|
except self.model.DoesNotExist:
|
|
raise Http404
|
|
else:
|
|
try:
|
|
return self.model.objects.language().get(type=2, city__url=self.kwargs.get('slug'))
|
|
except self.model.DoesNotExist:
|
|
raise Http404
|
|
|
|
def get_object(self, queryset=None):
|
|
obj = self.get_catalog_obj()
|
|
self.object = obj
|
|
return obj
|
|
|
|
def get_context_data(self, **kwargs):
|
|
self.get_object()
|
|
context = super(CatalogDetailedView, self).get_context_data(**kwargs)
|
|
context['object'] = self.object
|
|
return context
|
|
|
|
def form_valid(self, form):
|
|
order = form.save(commit=False)
|
|
order.save()
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
def get_initial(self):
|
|
obj = self.get_object()
|
|
init = {}
|
|
init['country'] = obj.country.name
|
|
if self.kwargs.get('type') is "city":
|
|
init['city'] = obj.city.name
|
|
return init
|
|
|
|
class SpecCatalog(ListView):
|
|
model = SpecialistCatalog
|
|
template_name = 'client/specialist_catalog/catalog.html'
|
|
|
|
def get_queryset(self):
|
|
|
|
if self.kwargs.get('type') == "country":
|
|
ctype = _country
|
|
else:
|
|
ctype = _city
|
|
|
|
qs = list(self.model.objects.language().select_related('country', 'city').filter(type=ctype))
|
|
if ctype == _country:
|
|
result = sorted(qs, key=lambda x: x.country.name)
|
|
else:
|
|
result = sorted(qs, key=lambda x: x.city.name)
|
|
|
|
return result
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(SpecCatalog, self).get_context_data(**kwargs)
|
|
if self.kwargs.get('type') == "country":
|
|
context['title'] = _(u'Переводчики по странам')
|
|
else:
|
|
context['title'] = _(u'Переводчики по городам')
|
|
return context |