Country refactor

remotes/origin/1203
Nazar Kotjuk 10 years ago
parent be10fba319
commit ca5000b695
  1. 16
      accounts/tests.py
  2. 9
      city/models.py
  3. 3
      city/signals.py
  4. 16
      city/tests.py
  5. 132
      country/models.py
  6. 1
      country/signals.py
  7. 16
      country/tests.py
  8. 1
      country/urls.py
  9. 5
      country/views.py
  10. 1
      proj/urls.py

@ -1,16 +0,0 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

@ -75,7 +75,6 @@ class City(TranslatableModel):
def get_events(self):
"""
returns nearest expos in this city
:return:
"""
now = date.today()
return Exposition.objects.filter(data_begin__gte=now, city=self).order_by('data_begin')[:3]
@ -87,16 +86,16 @@ class City(TranslatableModel):
return self.catalog+self.url
def expositions_number(self):
return Exposition.objects.filter(city=self.id).count()
return Exposition.objects.filter(city=self).count()
def conferences_number(self):
return Conference.objects.filter(city=self.id).count()
return Conference.objects.filter(city=self).count()
def seminars_number(self):
return Seminar.objects.filter(city=self.id).count()
return Seminar.objects.filter(city=self).count()
def webinars_number(self):
return Webinar.objects.filter(city=self.id).count()
return Webinar.objects.filter(city=self).count()
def get_parent(self):
"""

@ -1,3 +0,0 @@
# -*- coding: utf-8 -*-

@ -1,16 +0,0 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

@ -2,6 +2,7 @@
from datetime import date
from django.db import models
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes import generic
from django.db.models.signals import post_save, pre_save
from hvad.models import TranslatableModel, TranslatedFields
@ -9,26 +10,28 @@ from bitfield import BitField
from manager import CountryManager, AreaManager
from directories.models import Language, Currency
from city.models import City
from service.models import Service
from exposition.models import Exposition
from place_exposition.models import PlaceExposition
from organiser.models import Organiser
from conference.models import Conference
from seminar.models import Seminar
from webinar.models import Webinar
from functions.db import db_table_exists
from functions.signal_handlers import post_save_handler, pre_save_handler
from service.models import Service
from functions.db import db_table_exists
# check if table exist and create flags if true
#flags = [str(item.url) for item in Service.objects.all()] if db_table_exists('service_service') else []
flags = ['catalog', 'translator', 'participation', 'remote', 'tickets', 'visit', 'buildstand']
class Area(TranslatableModel):
"""
Store information about geographical zones
"""
translations = TranslatedFields(
name = models.CharField(max_length=255),
name=models.CharField(verbose_name=_(u'Название'), max_length=255),
)
objects = AreaManager()
@ -39,23 +42,40 @@ class Area(TranslatableModel):
return self.lazy_translation_getter('name', unicode(self.pk))
def countries(self):
"""
returns countries of current area
"""
lang = translation.get_language()
return Country.objects.select_related('exposition_country')\
.filter(exposition_country__country__isnull=False, translations__language_code=lang, area=self).distinct().order_by('translations__name')
.filter(exposition_country__country__isnull=False, translations__language_code=lang, area=self)\
.distinct().order_by('translations__name')
def expos(self):
"""
return expos that occur in current area
"""
countries = self.countries()
return Exposition.objects.filter(country__in=countries)
def get_sub_categories(self):
objects = [{'text':item.name, 'id':item.id, 'name':'co', 'sub':True} for item in self.countries()]
"""
returns list with countries data that connected to current area
uses in search
"""
objects = [{'text': item.name, 'id': item.id, 'name': 'co', 'sub': True} for item in self.countries()]
return objects
def get_parent(self):
parent = {}
return parent
"""
returns empty dict, cause area has no parents
uses in search
"""
return {}
def get_index_text(self):
"""
returns string of names in all languages for indexing it in search engine
"""
translation.activate('ru')
translations = self.translations.all()
names = ' '.join([tr.name for tr in translations])
@ -64,51 +84,46 @@ class Area(TranslatableModel):
class Country(TranslatableModel):
"""
Create Country model
Uses hvad.TranslatableModel which is child of django.db.models class
Stores information about countries
area- parent, city - child in search
"""
objects = CountryManager()
catalog = '/country/'
services = BitField(flags=flags)
url = models.SlugField(unique=True)
old_url = models.CharField(unique=True, max_length=55)
inflect = models.CharField(max_length=255, blank=True)
# relations
area = models.ForeignKey(Area)
big_cities = models.ManyToManyField(City, blank=True, null=True, related_name='cities')
capital = models.ForeignKey(City,blank=True, null=True, on_delete=models.PROTECT, related_name='capital')
url = models.SlugField(verbose_name=_(u'Url'), unique=True)
old_url = models.CharField(verbose_name=_(u'Старый урл'), unique=True, max_length=55)
# inflect name for russian language. example- в Росии
inflect = models.CharField(verbose_name=_(u'Склонение'), max_length=255, blank=True)
area = models.ForeignKey(Area, verbose_name=_(u'Географическая зона'))
logo = models.ImageField(verbose_name='Logo', upload_to='country/logo/', blank=True, max_length=255)
big_cities = models.ManyToManyField(City, verbose_name=_(u'Большые города'), blank=True, null=True, related_name='cities')
capital = models.ForeignKey(City, verbose_name=_(u'Столица'), blank=True, null=True, on_delete=models.PROTECT, related_name='capital')
language = models.ManyToManyField(Language, blank=True, null=True)
currency = models.ManyToManyField(Currency, blank=True, null=True)
population = models.PositiveIntegerField(blank=True, null=True)
teritory = models.PositiveIntegerField(blank=True, null=True)
timezone = models.FloatField(blank=True, null=True)
phone_code = models.PositiveIntegerField(blank=True, null=True)
currency = models.ManyToManyField(Currency, verbose_name=_(u'Валюта'), blank=True, null=True)
population = models.PositiveIntegerField(verbose_name=_(u'Население'), blank=True, null=True)
teritory = models.PositiveIntegerField(verbose_name=_(u'Територия'), blank=True, null=True)
timezone = models.FloatField(verbose_name=_(u'Часовой пояс'), blank=True, null=True)
phone_code = models.PositiveIntegerField(verbose_name=_(u'Тел. Код страны'), blank=True, null=True)
time_delivery = models.PositiveSmallIntegerField(blank=True, null=True)
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)
latitude = models.FloatField(verbose_name=_(u'Широта'), blank=True, null=True)
longitude = models.FloatField(verbose_name=_(u'Долгота'), blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
country_code = models.CharField(max_length=2)
country_code = models.CharField(verbose_name=_(u'Код страны(Alpha2)'), max_length=2)
# connection with FileModel by ContentType
files = generic.GenericRelation('file.FileModel',content_type_field='content_type', object_id_field='object_id')
#translated fields
translations = TranslatedFields(
name = models.CharField(max_length=255),
description = models.TextField(blank=True),
transport = models.TextField(blank=True),
#------visa inf
rules = models.TextField(blank=True),
documents = models.TextField(blank=True),#pdf?
consulate = models.TextField(blank=True),
name=models.CharField(max_length=255),
description=models.TextField(blank=True),
transport=models.TextField(blank=True),
rules=models.TextField(blank=True),
documents=models.TextField(blank=True),
consulate=models.TextField(blank=True),
#-----meta data
title = models.CharField(max_length=255),
descriptions = models.CharField(max_length=255),
keywords = models.CharField(max_length=255),
title=models.CharField(max_length=255),
descriptions=models.CharField(max_length=255),
keywords=models.CharField(max_length=255),
)
logo = models.ImageField(verbose_name='Logo', upload_to='country/logo/', blank=True, max_length=255)
class Meta:
ordering = ['translations__name']
@ -117,63 +132,52 @@ class Country(TranslatableModel):
return self.lazy_translation_getter('name', unicode(self.pk))
def get_events(self):
"""
returns nearest expos in this country
"""
now = date.today()
return Exposition.objects.filter(data_begin__gte=now, country=self).order_by('data_begin')[:3]
def get_places(self):
return PlaceExposition.objects.filter(country=self)[:3]
def get_organisers(self):
return Organiser.objects.filter(country=self)
def get_permanent_url(self):
return self.catalog+self.url
def events_catalog(self):
return Exposition.catalog+'country-%s'%self.url
def places_catalog(self):
return PlaceExposition.catalog+'country-%s'%self.url
def expositions_number(self):
return len(Exposition.objects.filter(country=self.id))
return Exposition.objects.filter(country=self).count()
def conferences_number(self):
return Conference.objects.filter(country=self.id).count()
return Conference.objects.filter(country=self).count()
def seminars_number(self):
return Seminar.objects.filter(country=self.id).count()
return Seminar.objects.filter(country=self).count()
def webinars_number(self):
return Webinar.objects.filter(country=self.id).count()
return Webinar.objects.filter(country=self).count()
def active_cities(self):
result = list(set(City.used.active_qs().filter(country=self)))
result.sort(key=lambda x:x.name)
result.sort(key=lambda x: x.name)
return result
lang = translation.get_language()
#return City.objects.select_related('exposition_city')\
# .filter(exposition_city__city__isnull=False, translations__language_code=lang, country=self).distinct().order_by('translations__name')
def get_sub_categories(self):
objects = [{'text':item.name, 'id':item.id, 'name':'ci', 'sub': False} for item in self.active_cities()]
objects = [{'text': item.name, 'id': item.id, 'name': 'ci', 'sub': False} for item in self.active_cities()]
return objects
def get_parent(self):
parent = {'text' : self.area.name, 'id': self.area.id, 'name': 'area'}
parent = {'text': self.area.name, 'id': self.area.id, 'name': 'area'}
return parent
def get_index_text(self):
"""
returns string of names in all languages for indexing it in search engine
"""
translation.activate('ru')
translations = self.translations.all()
names = ' '.join([tr.name for tr in translations])
return names
pre_save.connect(pre_save_handler, sender=Country)
post_save.connect(post_save_handler, sender=Country)

@ -1 +0,0 @@
# -*- coding: utf-8 -*-

@ -1,16 +0,0 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

@ -4,5 +4,6 @@ from views import CountryView
urlpatterns = patterns('',
# does not uses yet
url(r'(?P<slug>.*)', CountryView.as_view()),
)

@ -3,6 +3,9 @@ from models import Country
class CountryView(DetailView):
"""
this view is not used yet
"""
model = Country
slug_field = 'url'
template_name = 'country/country.html'
template_name = 'client/country/country.html'

@ -55,7 +55,6 @@ urlpatterns = patterns('',
url(r'^', include('company.urls')),
url(r'^', include('photoreport.urls')),
url(r'^', include('article.urls')),
url(r'^country/', include('country.urls')),
url(r'^city/', include('city.urls')),
url(r'^organiser/', include('organiser.urls')),
url(r'^gallery/', include('photologue.client_urls')),

Loading…
Cancel
Save