From 604ad98069c0144e03337cd9325b78becf1fb97e Mon Sep 17 00:00:00 2001 From: Alexander Kondratyev Date: Tue, 7 Mar 2017 23:34:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B8=D1=81=D0=BA=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=BD=D1=8B=D0=BC=20=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D0=BA=D0=B0=D0=BC.=20=D0=9F=D0=BE=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=20=D0=BF=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=D0=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trademark/lib/poiskznakov.py | 6 +- trademark/migrations/0001_initial.py | 15 ++- trademark/models.py | 100 ++++++++------ trademark/static/trademark/js/form.js | 21 +-- trademark/static/trademark/js/modal.js | 18 +-- trademark/static/trademark/less/result.less | 25 ++++ trademark/templates/trademark/form.html | 4 +- trademark/templates/trademark/modal.html | 122 +++++++++--------- .../templates/trademark/nice_block_busy.html | 15 ++- .../templates/trademark/search_detail.html | 17 +-- .../templates/trademark/trademark_detail.html | 2 +- trademark/templatetags/__init__.py | 0 trademark/templatetags/dict.py | 8 ++ trademark/views.py | 65 ++-------- 14 files changed, 215 insertions(+), 203 deletions(-) create mode 100644 trademark/templatetags/__init__.py create mode 100644 trademark/templatetags/dict.py diff --git a/trademark/lib/poiskznakov.py b/trademark/lib/poiskznakov.py index fe839d9..49ad5f5 100644 --- a/trademark/lib/poiskznakov.py +++ b/trademark/lib/poiskznakov.py @@ -24,9 +24,10 @@ class TrademarkSearchAPI(): response = urllib2.urlopen(api_request) data = json.load(response) + return data - def search_trademark(self, keyword, similarity=100): + def search_trademark(self, keyword, similarity=100, nices=''): request = { 'module': 'search', 'procedure': 'make_search', @@ -35,6 +36,7 @@ class TrademarkSearchAPI(): 'databases': [21, 23], 'use_translit': 'true', 'skip_expired': 'true', + 'icgs': [int(nice) for nice in nices.split(',')] } if similarity < 100: @@ -87,7 +89,7 @@ class TrademarkSearchAPI(): 'marks': list(ids), 'access_keys': list(keys) } - + data = self.send_request(request) return data['records_list']['21']['records'] diff --git a/trademark/migrations/0001_initial.py b/trademark/migrations/0001_initial.py index 2c6f15e..1a5f1ed 100644 --- a/trademark/migrations/0001_initial.py +++ b/trademark/migrations/0001_initial.py @@ -15,6 +15,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('request', models.CharField(max_length=200)), + ('nices', models.CharField(max_length=200)), ('slug', models.CharField(max_length=200)), ('status', models.IntegerField(default=0)), ('loaded', models.IntegerField(default=0)), @@ -34,6 +35,7 @@ class Migration(migrations.Migration): ('description', models.TextField()), ], options={ + 'ordering': ('nice_id',), 'verbose_name': 'nice', 'verbose_name_plural': 'classes', }, @@ -59,9 +61,10 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('title', models.TextField()), - ('nice', models.ForeignKey(to='trademark.Nice')), + ('nice_id', models.IntegerField()), ], options={ + 'ordering': ('nice_id',), 'verbose_name': 'product', 'verbose_name_plural': 'products', }, @@ -99,11 +102,11 @@ class Migration(migrations.Migration): name='Trademark', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), - ('title', models.CharField(max_length=255)), + ('title', models.CharField(max_length=255, null=True)), ('ext_id', models.CharField(unique=True, max_length=20)), - ('application_id', models.IntegerField()), - ('cert_id', models.IntegerField()), - ('owner', models.TextField()), + ('application_id', models.CharField(max_length=12, null=True)), + ('cert_id', models.CharField(max_length=12, null=True)), + ('owner', models.TextField(null=True)), ('image_url', models.URLField(null=True)), ('status', models.CharField(max_length=20)), ('source_url', models.URLField(null=True)), @@ -112,7 +115,7 @@ class Migration(migrations.Migration): ('expiration_at', models.DateField(null=True)), ('renewed_at', models.DateField(null=True)), ('priority_at', models.DateField(null=True)), - ('access_key', models.CharField(max_length=20)), + ('access_key', models.CharField(max_length=20, null=True)), ('nices', models.ManyToManyField(to='trademark.Nice')), ('products', models.ManyToManyField(to='trademark.Product')), ], diff --git a/trademark/models.py b/trademark/models.py index 8f3d13d..e5fbbe7 100644 --- a/trademark/models.py +++ b/trademark/models.py @@ -9,6 +9,9 @@ from lib.poiskznakov import TrademarkSearchAPI from unidecode import unidecode from django.template.defaultfilters import slugify + +from datetime import date +from datetime import timedelta # from .cms_appconfig import TrademarkConfig TrademarkAPI = TrademarkSearchAPI() @@ -49,12 +52,12 @@ class Nice(models.Model): class Product(models.Model): title = models.TextField() - nice = models.ForeignKey(Nice) + nice_id = models.IntegerField() class Meta: verbose_name = _('product') verbose_name_plural = _('products') - ordering = ('nice__nice_id', ) + ordering = ('nice_id', ) def __unicode__(self): # Python 3: def __str__(self): return self.title @@ -62,37 +65,6 @@ class Product(models.Model): def __str__(self): return self.title - @property - def cap_title(self): - line = self.title.strip().replace('[', '(').replace(']', ')') - if line[-1] == ".": - cap_line = line[0].upper() + line[1:-1] - else: - cap_line = line[0].upper() + line[1:] - - return cap_line - - @property - def titles(self): - titles = self.title.split(';') - try: - if len(titles) > 1: - capitalized = [] - for line in titles: - line = line.strip().replace('[', '(').replace(']', ')') - if line[-1] == ".": - cap_line = line[0].upper() + line[1:-1] - else: - cap_line = line[0].upper() + line[1:] - - capitalized.append(cap_line) - - return capitalized - else: - return [] - except: - return [] - class Trademark(models.Model): title = models.CharField(max_length=255, null=True) @@ -127,6 +99,7 @@ class Trademark(models.Model): class Keyword(models.Model): request = models.CharField(max_length=200) + nices = models.CharField(max_length=200) slug = models.CharField(max_length=200) status = models.IntegerField(default=0) loaded = models.IntegerField(default=0) @@ -141,6 +114,12 @@ class Keyword(models.Model): def __str__(self): return self.request + @property + def nice_objects(self): + nices = self.nices.split(',') + + return Nice.objects.filter(nice_id__in=nices) + @property def contains_results(self): search = self.searches.filter(similarity=100)[0] @@ -160,7 +139,7 @@ class Keyword(models.Model): def load_results(self): for search in self.searches.all(): - if search.status != "finished": + if search.status != "finished" or date.today() > search.loaded_at + timedelta(days=30): search.get_results() if len(self.searches.all()) == len(self.searches.filter(status="finished")): @@ -194,12 +173,48 @@ class Search(models.Model): return nices def send_request(self): - data = TrademarkAPI.search_trademark(keyword=self.keyword.request, similarity=self.similarity) + data = TrademarkAPI.search_trademark(keyword=self.keyword.request, similarity=self.similarity, nices=self.keyword.nices) self.search_id = data['search_id'] self.save() + def parse_products(self, nice_id, nice_description): + if len(nice_description) == 0: + return + + if nice_description[0:1].isdigit(): + nice_description = nice_description[nice_description.find('-') + 1:].strip() + + titles = nice_description.split(';') + + if len(titles) == 1: + try: + titles = nice_description.split(',') + except: + print nice_description + + if len(titles) > 0: + products = [] + for line in titles: + if len(line.strip()) == 0: + continue + + line = line.strip().replace('[', '(').replace(']', ')') + + if line[-1] == ".": + line = line[0].upper() + line[1:-1] + else: + line = line[0].upper() + line[1:] + + line = line.replace('\n', ' ') + line = ' '.join(line.split()) + + product, created = Product.objects.get_or_create(nice_id=nice_id, title=line) + products.append(product) + + return products + def get_results(self): results, status = TrademarkAPI.get_results(self.search_id) @@ -214,7 +229,7 @@ class Search(models.Model): return - if self.status =='loading': + if self.status == 'loading': return if results == {}: @@ -249,7 +264,7 @@ class Search(models.Model): instance.save() - for nice_id in trademark['icgs']: + for nice_id in trademark.get('icgs', []): nice, created = Nice.objects.get_or_create(nice_id=nice_id) instance.nices.add(nice) @@ -269,15 +284,16 @@ class Search(models.Model): instance.expiration_at = trademark.get('dateexp', '') instance.renewed_at = trademark.get('renewed', '') - for nice_id, nice_description in trademark['icgs'].iteritems(): - if nice_description[0:1].isdigit(): - nice_description = nice_description[nice_description.find('-') + 1:].strip() + for nice_id, nice_description in trademark.get('icgs', {}).iteritems(): nice_obj, created = Nice.objects.get_or_create(nice_id=nice_id) - product, created = Product.objects.get_or_create(nice=nice_obj, title=nice_description) - instance.products.add(product) + + products = self.parse_products(nice_id, nice_description) + + instance.products.add(*products) instance.save() + self.loaded_at = date.today() self.status = 'finished' self.save() diff --git a/trademark/static/trademark/js/form.js b/trademark/static/trademark/js/form.js index 5768c87..a4ecf73 100644 --- a/trademark/static/trademark/js/form.js +++ b/trademark/static/trademark/js/form.js @@ -60,17 +60,12 @@ TrademarkSearchForm = (function(searchContainer){ if(show_link && iteration > 5) { var href = '' $wait.html('Остановить поиск и перейти к результатам'); - } else { - } - if((completed || almost_completed) && advanced_form_scope.opened == false) { - document.location.href = tm_url + "#?nices=" + advanced_form_scope.classes_values.join(','); + if((completed || almost_completed)) { + document.location.href = tm_url; } else { - if (completed || almost_completed) { - advanced_form_scope.tm_url = tm_url; - } iteration += 1; setTimeout(function () { get_status(slug); @@ -98,7 +93,10 @@ TrademarkSearchForm = (function(searchContainer){ $sending.show(); $.post("/ru/trademarks/online-search/request/", - { 'keyword': request}).done(function(data) { + { + 'keyword': request, + 'nices': advanced_form_scope.classes_values.join(',') + }).done(function(data) { var tm_count = Math.floor(Math.random() * (3000000 - 1000000) + 1000000); $(searchContainer + ' .trademark-search-count-number').text(tm_count.toLocaleString() + ' знаков') $advanced_search.show(); @@ -133,6 +131,11 @@ TrademarkSearchForm = (function(searchContainer){ } }); $(searchContainer + ' .trademark-search-submit').click(function(){ - send(); + if (advanced_form_scope.classes_values != '') { + send(); + } else { + advanced_form_scope.callback = send; + advanced_form_scope.open(); + } }); }); diff --git a/trademark/static/trademark/js/modal.js b/trademark/static/trademark/js/modal.js index 445be19..fd1f597 100644 --- a/trademark/static/trademark/js/modal.js +++ b/trademark/static/trademark/js/modal.js @@ -8,6 +8,7 @@ angular.module('zuykov') $scope.classes = $scope.classes_values.length; $scope.animationsEnabled = true; $scope.opened = false; + $scope.callback = false; $scope.open = function (form_id) { @@ -26,26 +27,15 @@ angular.module('zuykov') $scope.classes_values = classes_values; $scope.classes = $scope.classes_values.length; - console.log($scope.classes_values, $scope.classes); - $scope.opened = false; - if (typeof(searchResults) != "undefined") { - $.urlParamUpdate('nices', $scope.classes_values.join(',')); - searchResults.expand(); - } - - if (typeof(searchResults) == 'undefined' && $scope.tm_url != false) { - document.location.href = $scope.tm_url + "#?nices=" + $scope.classes_values.join(','); + if ($scope.callback) { + $scope.callback(); } - console.log('load'); }, function () { $scope.opened = false; - console.log('cancle'); - if (typeof(searchResults) == 'undefined' && $scope.tm_url != false) { - document.location.href = $scope.tm_url + "#?nices=" + $scope.classes_values.join(','); - } + }); }; diff --git a/trademark/static/trademark/less/result.less b/trademark/static/trademark/less/result.less index 07798be..451f859 100644 --- a/trademark/static/trademark/less/result.less +++ b/trademark/static/trademark/less/result.less @@ -2,10 +2,31 @@ h2 { margin-top: 0; } + + .trademark-search-advanced-btn { + top: -32px !important; + } } .trademark-search { .trademark-search-form { margin-bottom: 5px; + + .trademark-search-advanced-btn { + position: relative; + z-index: 100; + right: 80px; + top: -37px; + float: right; + } + + .trademark-search-advanced-count { + font-size: 12px; + margin-top: 4px; + } + + .nice-selected { + margin-bottom: 5px; + } } .trademark-search-field { @@ -170,6 +191,10 @@ .trademark-register-block { background-color: #E8EEFC; + + .btn { + margin-right: 5px; + } } diff --git a/trademark/templates/trademark/form.html b/trademark/templates/trademark/form.html index 6f8d849..7e74eae 100644 --- a/trademark/templates/trademark/form.html +++ b/trademark/templates/trademark/form.html @@ -7,6 +7,7 @@ + {% include 'trademark/modal.html' with mode='form' %} Например, Coca-Cola
@@ -19,9 +20,6 @@
- - {% include 'trademark/modal.html' with mode='form' %} - diff --git a/trademark/templates/trademark/modal.html b/trademark/templates/trademark/modal.html index a7da86f..b13227d 100644 --- a/trademark/templates/trademark/modal.html +++ b/trademark/templates/trademark/modal.html @@ -1,88 +1,86 @@ {% load i18n menu_tags cms_tags %}
- + {% if mode != 'form' %} — чтобы уточнить классы МКТУ, регистрацию в которых хотите проверить. {% endif %} -
- -{% verbatim %} -
-

Поиск происходит {{ classes == 0 ? 'во всех классах' : 'в ' + classes + GetNoun(classes, ' классе', ' классах', ' классах') }}

- -
-{% endverbatim %} -
-{% if mode != 'form' %} — чтобы уточнить классы МКТУ, регистрацию в которых хотите проверить. {% endif %} - + {% verbatim %} +
+

Поиск происходит {{ classes == 0 ? 'во всех классах' : 'в ' + classes + GetNoun(classes, ' классе', ' классах', ' классах') }}

+
    +
  • {{ nice[id-1].id }} – {{ nice[id-1].title }}
  • +
+
+ {% endverbatim %} -
diff --git a/trademark/templates/trademark/nice_block_busy.html b/trademark/templates/trademark/nice_block_busy.html index 1b40e1f..4d8fa5f 100644 --- a/trademark/templates/trademark/nice_block_busy.html +++ b/trademark/templates/trademark/nice_block_busy.html @@ -1,15 +1,20 @@ +{% load dict %}

«{{ keyword|title }}» был зарегистрирован в следующих классах