# -*- coding: utf-8 -*- from django.db import models from hvad.models import TranslatableModel, TranslatedFields, TranslationManager from django.contrib.contenttypes import generic from django.utils import translation from django.utils.translation import ugettext as _ from functions.custom_fields import LocationField from functions.models_methods import ExpoManager from functions.model_mixin import ExpoMixin class Company(TranslatableModel, ExpoMixin): """ Create Company model Uses hvad.TranslatableModel which is child of django.db.models class """ catalog = '/members/' catalog_name = _(u'Участники:') search_name = None objects = ExpoManager() files = generic.GenericRelation('file.FileModel', content_type_field='content_type', object_id_field='object_id') url = models.SlugField(max_length=255) #relations creator = models.ForeignKey('accounts.User', verbose_name='Создатель', related_name='created_company', blank=True, null=True) theme = models.ManyToManyField('theme.Theme', verbose_name='Отрасль', blank=True, null=True, related_name='companies') tag = models.ManyToManyField('theme.Tag', verbose_name='Теги', blank=True, null=True, related_name='companies') country = models.ForeignKey('country.Country', verbose_name='Страна', blank=True, null=True, on_delete=models.PROTECT, related_name='companies') city = models.ForeignKey('city.City', verbose_name='Город', blank=True, null=True, on_delete=models.PROTECT, related_name='companies') #address. uses LocationField. saves data in json format address = LocationField(verbose_name='Адрес', blank=True) staff_number = models.CharField(verbose_name='Количество сотрудников', max_length=50, blank=True) phone = models.BigIntegerField(verbose_name='Телефон', blank=True, null=True) fax = models.BigIntegerField(verbose_name='Факс', blank=True, null=True) web_page = models.CharField(verbose_name='Веб-сайт',max_length=255, blank=True) email = models.EmailField(verbose_name='Email', blank=True) facebook = models.URLField(verbose_name=_(u'Facebook'), blank=True) twitter = models.URLField(verbose_name=_(u'Twitter'), blank=True) linkedin = models.URLField(verbose_name=_(u'LinkedIn'), blank=True) vk = models.URLField(verbose_name=_(u'В контакте'), blank=True) foundation = models.PositiveIntegerField(verbose_name='Год основания', blank=True, null=True) #translation fields translations = TranslatedFields( name = models.CharField(verbose_name='Название компании', max_length=255), specialization = models.CharField(verbose_name='Специализация', max_length=255, blank=True), description = models.TextField(verbose_name='О компании', blank=True), address_inf = models.TextField(verbose_name='Доп инф по адресу', blank=True), #-----meta title = models.CharField(max_length=255), descriptions = models.CharField(max_length=255), keywords = models.CharField(max_length=255), ) #fields saves information about creating and changing model created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) def __unicode__(self): return self.lazy_translation_getter('name', self.pk) def get_index_text(self): translation.activate('ru') translations = self.translations.all() names = ' '.join([tr.name for tr in translations]) specializations = ' '.join([tr.specialization for tr in translations]) themes = ' '.join([' '.join(theme.get_all_names()) for theme in self.theme.all()]) tags = ' '.join([' '.join(tag.get_all_names()) for tag in self.tag.all()]) return names + ' ' + specializations + ' ' + themes + ' ' + tags def get_catalog_url(self): return '/members/' def get_permanent_url(self): url = '%smember-%s'%(self.catalog, self.url) return url def get_expositions_number(self): return len(self.exposition_companies.all()) def get_conferences_number(self): return len(self.conference_companies.all()) def get_seminars_number(self): return len(self.seminar_companies.all()) def get_webinars_number(self): return len(self.webinar_companies.all()) def get_events_number(self): expositions = self.exposition_companies.all() conferences = self.conference_companies.all() seminars = self.seminar_companies.all() webinars = self.webinar_companies.all() return len(list(expositions)+list(conferences)+list(seminars)+list(webinars)) def get_events(self): expositions = self.exposition_companies.all() conferences = self.conference_companies.all() seminars = self.seminar_companies.all() webinars = self.webinar_companies.all() return list(expositions)+ list(conferences)+list(seminars)+list(webinars) from django.db.models.signals import post_save from functions.signal_handlers import post_save_handler post_save.connect(post_save_handler, sender=Company)