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.
88 lines
3.3 KiB
88 lines
3.3 KiB
# -*- coding: utf-8 -*-
|
|
from django.db import models
|
|
from django.utils.translation import ugettext as _
|
|
from django.contrib.contenttypes import generic
|
|
from django.db.models.signals import post_save, pre_save
|
|
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
|
|
from bitfield import BitField
|
|
# 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 functions.db import db_table_exists
|
|
from functions.signal_handlers import post_save_handler, pre_save_handler
|
|
from django.utils.translation import get_language as lang
|
|
|
|
# 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 []
|
|
REGIONS =('europa', 'asia', 'america', 'africa')
|
|
|
|
class CountryManager(TranslationManager):
|
|
def all(self):
|
|
"""
|
|
hack
|
|
"""
|
|
return super(TranslationManager, self).all().filter(translations__language_code=lang()).order_by('translations__name')
|
|
|
|
def safe_get(self, **kwargs):
|
|
model = self.model
|
|
try:
|
|
return model.objects.get(**kwargs)
|
|
except:
|
|
return None
|
|
|
|
class Country(TranslatableModel):
|
|
"""
|
|
Create Country model
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
|
|
objects = CountryManager()
|
|
services = BitField(flags=flags)
|
|
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=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),
|
|
#-----meta data
|
|
title = models.CharField(max_length=250),
|
|
descriptions = models.CharField(max_length=250),
|
|
keywords = models.CharField(max_length=250),
|
|
|
|
)
|
|
|
|
# class Meta:
|
|
# ordering = ['translations__name']
|
|
|
|
def __unicode__(self):
|
|
return self.lazy_translation_getter('name', unicode(self.pk))
|
|
|
|
post_save.connect(post_save_handler, sender=Country) |