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.
155 lines
7.0 KiB
155 lines
7.0 KiB
# -*- coding: utf-8 -*-
|
|
import os
|
|
from django.db import models
|
|
from django.db.models.signals import post_save
|
|
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
|
|
from functions.signal_handlers import post_save_handler
|
|
|
|
def get_storage_path(instance, filename):
|
|
return os.path.join('company', filename)
|
|
|
|
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),
|
|
)
|
|
rating = models.IntegerField(default=100) # добавить индекс в базе
|
|
#fields saves information about creating and changing model
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
logo = models.ImageField(upload_to=get_storage_path, blank=True)
|
|
|
|
class Meta:
|
|
ordering = ['-rating', 'id']
|
|
|
|
|
|
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):
|
|
return '%s%s/'%(self.catalog, self.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)
|
|
|
|
|
|
def calculate_rating(company):
|
|
rating_simple = {'country': 5, 'city': 5, 'address_inf': 10, 'phone': 10, 'fax': 5, 'email': 10,
|
|
'facebook': 5, 'twitter': 5, 'linkedin': 5, 'vk': 5, 'web_page': 10, #'logo': 20,
|
|
'specialization': 5, 'description':15, 'foundation': 5, 'staff_number': 5}# участие и посещение доделать
|
|
rating_methods = {'theme': 10, 'tag': 5, 'photo':5}
|
|
# base rating
|
|
rating = 100
|
|
for key, value in rating_simple.iteritems():
|
|
if getattr(company, key):
|
|
rating += value
|
|
|
|
themes = company.theme.all().count()
|
|
rating += themes * 10
|
|
tags = company.tag.all().count()
|
|
rating += tags * 10 if tags < 6 else 50
|
|
|
|
company.rating = rating
|
|
|
|
# call to prevent recursion
|
|
post_save.disconnect(create_company, sender=Company)
|
|
company.save()
|
|
post_save.connect(create_company, sender=Company)
|
|
|
|
|
|
def create_company(sender, instance, created, **kwargs):
|
|
post_save_handler(sender, instance=instance, **kwargs)
|
|
calculate_rating(instance)
|
|
|
|
def calculate_rating_for_translations(sender, instance, created, **kwargs):
|
|
company = instance.master
|
|
post_save.disconnect(calculate_rating_for_translations, sender=Company._meta.translations_model)
|
|
calculate_rating(company)
|
|
post_save.connect(calculate_rating_for_translations, sender=Company._meta.translations_model)
|
|
|
|
post_save.connect(create_company, sender=Company)
|
|
post_save.connect(calculate_rating_for_translations, sender=Company._meta.translations_model)
|
|
|