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.
 
 
 
 
 
 

117 lines
4.1 KiB

# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields
from django.contrib.contenttypes import generic
#models
from directories.models import Language, Currency
from city.models import City
from service.models import Service
#func
from functions.custom_fields import EnumField
from bitfield import BitField
from functions.db import db_table_exists
from django.conf import settings
#check if table exist and create flags if true
flags = [str(item.id) for item in Service.objects.all()] if db_table_exists('service_service') else []
class Country(TranslatableModel):
"""
Create Country model
Uses hvad.TranslatableModel which is child of django.db.models class
"""
services = BitField(flags=flags)
REGIONS =('europa', 'asia', 'america', 'africa')
url = models.SlugField(unique=True)
#relations
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')
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)
time_delivery = models.PositiveSmallIntegerField(blank=True, null=True)
#
region = EnumField(values=REGIONS)
#fields saves information about creating and changing model
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
#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=30),
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),
#-----meta data
title = models.CharField(max_length=250),
descriptions = models.CharField(max_length=250),
keywords = models.CharField(max_length=250),
)
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
from django.db.models.signals import post_save
from models import Country
from django.dispatch import receiver
from django.conf import settings
from functions.form_check import translit_with_separator
from functions.translate import get_translated_fields
@receiver(post_save, sender=Country)
def country_post_save_handler(sender, **kwargs):
"""
objects saves two times:
- first time Country object
- second time CountryTranslation object
object must have at least one Translation
"""
obj = kwargs['instance']
if isinstance(obj, Country):
# object is Country
# what languages must be
all_langs = [code for code, lang in settings.LANGUAGES]
# what languages are
obj_langs = obj.get_available_languages()
#
missing_languages = list(set(all_langs) - set(obj_langs))
# get first Translation object
translation = obj.translations.all()[0]
#
fields = get_translated_fields(obj)
for code in missing_languages:
# translate
obj.translate(code)
# go through all fields and set value
for field in fields:
setattr(obj, field, getattr(translation, field))
obj.save()
if isinstance(obj, CountryTranslation):
# object is Translation
print('success')
if obj.language_code == 'ru':
country = Country.objects.get(id=obj.master_id)
country.url = translit_with_separator(obj.name)
country.save()