# -*- coding: utf-8 -*- from django.views.generic import CreateView, UpdateView, DeleteView, FormView, DetailView from functions.custom_views import ListView from django.views.generic.detail import SingleObjectMixin from django.utils.translation import ugettext as _ from meta.views import MetadataMixin 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, HttpResponsePermanentRedirect 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") from hvad.utils import get_translation_aware_manager class SpecialistListView(ListView): model = Specialist template_name = 'admin/specialist/specialist_all.html' paginate_by = settings.ADMIN_PAGINATION def get_queryset(self): name = self.request.GET.get('name', None) city = self.request.GET.get('city', None) qs = get_translation_aware_manager(Specialist).all() if name: qs = qs.filter(name__icontains=name) if city: qs = qs.filter(city__name__icontains=city) return qs 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 paginator_class = settings.DEFAULT_PAGINATOR def get_queryset(self): _GET = self.request.GET query, city, country, only_cntry = _GET.get('query'), _GET.get('city'), _GET.get('country'), _GET.get('only_countries') qs = self.model.objects.language() if only_cntry: qs = qs.filter(type=1) if query: qs = qs.filter(title__icontains=query) if city: qs = qs.filter(city__translations__name__icontains=city) if country: qs = qs.filter(country__translations__name__icontains=country) return qs 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(MetadataMixin, 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: spec_catalog = self.model.objects.language().get(type=1, country__url=self.kwargs.get('slug')) self.kwargs['country'] = spec_catalog.country return spec_catalog except self.model.DoesNotExist: raise Http404 else: try: spec_catalog = self.model.objects.language().get(type=2, city__url=self.kwargs.get('slug')) self.kwargs['city'] = spec_catalog.city return spec_catalog 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(MetadataMixin, 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 def redirect_old(request, *args, **kwargs): if not kwargs.get('type'): return HttpResponsePermanentRedirect('/translators/country/') else: return HttpResponsePermanentRedirect('/translators/city/')