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.
134 lines
5.2 KiB
134 lines
5.2 KiB
# -*- coding: utf-8 -*-
|
|
from django.views.generic import DetailView
|
|
from functions.custom_views import ListView
|
|
from django.shortcuts import get_object_or_404
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
from django.utils.translation import ugettext as _
|
|
from django.conf import settings
|
|
from django.http import Http404
|
|
from functions.cache_mixin import JitterCacheMixin, CacheMixin
|
|
from meta.views import MetadataMixin
|
|
from accounts.models import User
|
|
from country.models import Country
|
|
from city.models import City
|
|
from translator.models import Translator
|
|
|
|
class TranslatorList(JitterCacheMixin, MetadataMixin, ListView):
|
|
model = User
|
|
template_name = 'client/accounts/translators/translators_list.html'
|
|
paginate_by = settings.CLIENT_PAGINATION
|
|
def get_queryset(self):
|
|
# all users with translator profile
|
|
qs = self.model.objects.select_related('profile', 'country', 'city').filter(translator__isnull=False)
|
|
|
|
if self.kwargs.get('city'):
|
|
city_slug = self.kwargs.get('city')
|
|
city = get_object_or_404(City, url=city_slug)
|
|
self.kwargs['city'] = city
|
|
qs = qs.filter(profile__city=city)
|
|
if self.kwargs.get('country'):
|
|
country_slug = self.kwargs.get('country')
|
|
country = get_object_or_404(Country, url=country_slug)
|
|
self.kwargs['country'] = country
|
|
qs = qs.filter(profile__country=country)
|
|
|
|
return qs
|
|
|
|
class TranslatorsAbstract(SingleObjectMixin, ListView):
|
|
slug_field = 'url'
|
|
|
|
def get_object(self):
|
|
return get_object_or_404(self.model, url=self.kwargs['slug'])
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
return super(TranslatorsAbstract, self).get(request, *args, **kwargs)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(TranslatorsAbstract, self).get_context_data(**kwargs)
|
|
context['object'] = self.object
|
|
return context
|
|
|
|
class TranslatorsCity(TranslatorsAbstract):
|
|
model = City
|
|
template_name = 'client/accounts/translators/translator_city.html'
|
|
|
|
def get_queryset(self):
|
|
city = self.object
|
|
self.kwargs['city'] = city
|
|
qs = User.objects.select_related('profile', 'country', 'city').filter(translator__isnull=False, profile__city=city)[:6]
|
|
return qs
|
|
|
|
class TranslatorsCountry(TranslatorsAbstract):
|
|
model = Country
|
|
template_name = 'client/accounts/translators/translator_country.html'
|
|
|
|
def get_queryset(self):
|
|
country = self.object
|
|
self.kwargs['country'] = country
|
|
qs = User.objects.select_related('profile', 'country', 'city').filter(translator__isnull=False, profile__country=country)[:6]
|
|
return qs
|
|
|
|
|
|
class TranslatorsByCity(ListView):
|
|
model = City
|
|
template_name = 'client/accounts/translators/translators_by.html'
|
|
|
|
def get_queryset(self):
|
|
qs = User.objects.select_related('profile', 'translator').filter(translator__isnull=False, profile__city__isnull=False).values('profile__city').distinct()
|
|
|
|
ids = [item['profile__city'] for item in list(qs)]
|
|
sql = {'translators_count':
|
|
"""SELECT COUNT(*)
|
|
FROM accounts_user
|
|
LEFT JOIN accounts_profile ON accounts_user.id=accounts_profile.user_id
|
|
WHERE accounts_user.translator_id is not NULL AND accounts_profile.city_id=city_city.id"""}
|
|
qs = set(list(self.model.objects.filter(id__in=ids).extra(select=sql)))
|
|
cities = sorted(qs, key=lambda x: x.name)
|
|
return cities
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(TranslatorsByCity, self).get_context_data(**kwargs)
|
|
context['catalog_name'] = _(u'Города')
|
|
return context
|
|
|
|
class TranslatorsByCountry(ListView):
|
|
model = Country
|
|
template_name = 'client/accounts/translators/translators_by.html'
|
|
|
|
def get_queryset(self):
|
|
qs = User.objects.select_related('profile', 'translator').filter(translator__isnull=False, profile__country__isnull=False).values('profile__country').distinct()
|
|
|
|
ids = [item['profile__country'] for item in list(qs)]
|
|
sql = {'translators_count':
|
|
"""SELECT COUNT(*)
|
|
FROM accounts_user
|
|
LEFT JOIN accounts_profile ON accounts_user.id=accounts_profile.user_id
|
|
WHERE accounts_user.translator_id is not NULL AND accounts_profile.country_id=country_country.id"""}
|
|
qs = set(list(self.model.objects.filter(id__in=ids).extra(select=sql)))
|
|
cities = sorted(qs, key=lambda x: x.name)
|
|
return cities
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(TranslatorsByCountry, self).get_context_data(**kwargs)
|
|
context['catalog_name'] = _(u'Страны')
|
|
return context
|
|
|
|
|
|
class TranslatorProfile(DetailView):
|
|
model = User
|
|
template_name = 'client/accounts/translators/translator_profile.html'
|
|
slug_field = 'url'
|
|
|
|
def get_object(self, queryset=None):
|
|
url = self.kwargs['slug']
|
|
try:
|
|
|
|
obj = User.objects.get(url=url)
|
|
except User.DoesNotExist:
|
|
obj = get_object_or_404(User, id=url)
|
|
|
|
if not obj.translator:
|
|
raise Http404
|
|
return obj
|
|
|
|
|