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.
276 lines
9.4 KiB
276 lines
9.4 KiB
#-*- coding: utf-8 -*-
|
|
|
|
# from django.core.urlresolvers import reverse
|
|
# from django.http import HttpResponseRedirect
|
|
from django.shortcuts import get_object_or_404
|
|
from django.views import generic
|
|
|
|
from django.http import HttpResponse
|
|
import json
|
|
|
|
from .models import *
|
|
from django.views.decorators.cache import never_cache
|
|
from django.db.models import Count
|
|
|
|
import pymorphy2
|
|
|
|
|
|
class SearchDetailView(generic.DetailView):
|
|
model = Keyword
|
|
context_object_name = 'keyword'
|
|
template_name = 'trademark/search_detail.html'
|
|
slug_field = 'slug'
|
|
view_url_name = 'trademark:search-detail'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(SearchDetailView, self).get_context_data(**kwargs)
|
|
|
|
self.get_object().save()
|
|
|
|
identity = self.get_object().searches.filter(similarity=146)[0]
|
|
identity_results = identity.results.filter(status='Acting')
|
|
identity_expired_results = identity.results.filter(status='Expired')
|
|
context['identity'] = identity_results
|
|
context['identity_expired'] = identity_expired_results
|
|
identity_ids = identity_results.values_list('id', flat=True)
|
|
nice_busy_ids = identity_results.values_list('nices__nice_id', flat=True).distinct()
|
|
|
|
contains = self.get_object().searches.filter(similarity=100)[0]
|
|
contains_results = contains.results.filter(title__isnull=False, status='Acting').exclude(id__in=identity_ids)
|
|
contains_expired_results = contains.results.filter(title__isnull=False, status='Expired').exclude(id__in=identity_ids)
|
|
context['contains'] = contains_results[:18]
|
|
contains_ids = contains_results.values_list('id', flat=True)
|
|
|
|
similar = self.get_object().searches.filter(similarity=97)[0]
|
|
similar_results = similar.results.filter(status='Acting').exclude(id__in=contains_ids | identity_ids)
|
|
similar_expired_results = similar.results.filter(status='Expired').exclude(id__in=contains_ids | identity_ids)
|
|
context['similar'] = similar_results[:12]
|
|
context['similar_expired'] = (contains_expired_results | similar_expired_results).distinct()
|
|
|
|
context['nice_busy'] = Nice.objects.filter(nice_id__in=nice_busy_ids).order_by('nice_id')
|
|
|
|
context['products_busy'] = {}
|
|
for nice in context['nice_busy']:
|
|
products = Product.objects.filter(id__in=identity_results.values_list('products__id'), nice_id=nice.nice_id)
|
|
context['products_busy'][nice.nice_id] = products
|
|
|
|
context['nice_available'] = Nice.objects.filter(nice_id__in=[x for x in range(1,46) if x not in nice_busy_ids]).order_by('nice_id')
|
|
|
|
return context
|
|
|
|
|
|
class TrademarkDetailView(generic.DetailView):
|
|
model = Trademark
|
|
context_object_name = 'trademark'
|
|
template_name = 'trademark/trademark_detail.html'
|
|
view_url_name = 'trademark:trademark-detail'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(TrademarkDetailView, self).get_context_data(**kwargs)
|
|
|
|
tm = self.get_object()
|
|
context['tm'] = tm
|
|
context['nice_available'] = Nice.objects.filter(nice_id__in=[x for x in range(1,46) if x not in tm.nices.values_list('nice_id', flat=True)]).order_by('nice_id')
|
|
|
|
context['products_busy'] = {}
|
|
for nice in tm.nices.all():
|
|
products = Product.objects.filter(id__in=tm.products.values_list('id'), nice_id=nice.nice_id)
|
|
context['products_busy'][nice.nice_id] = products
|
|
|
|
return context
|
|
|
|
|
|
class IndexView(generic.TemplateView):
|
|
template_name = 'trademark/index.html'
|
|
|
|
|
|
class NicesView(generic.TemplateView):
|
|
template_name = 'trademark/nices_catalog.html'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(NicesView, self).get_context_data(**kwargs)
|
|
|
|
context['nices'] = Nice.objects.all().order_by('nice_id')
|
|
|
|
return context
|
|
|
|
class NiceDetailView(generic.DetailView):
|
|
model = Nice
|
|
context_object_name = 'nice'
|
|
template_name = 'trademark/nice_detail.html'
|
|
view_url_name = 'trademark:nice-detail'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(NiceDetailView, self).get_context_data(**kwargs)
|
|
|
|
nice = self.get_object()
|
|
context['nice'] = nice
|
|
|
|
return context
|
|
|
|
class ProductDetailView(generic.DetailView):
|
|
model = Product
|
|
context_object_name = 'product'
|
|
template_name = 'trademark/product_detail.html'
|
|
view_url_name = 'trademark:product-detail'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(ProductDetailView, self).get_context_data(**kwargs)
|
|
|
|
product = self.get_object()
|
|
context['product'] = product
|
|
context['nice'] = Nice.objects.get(nice_id=product.nice_id)
|
|
|
|
context['trademarks_expired'] = product.trademark_set.annotate(num_nices=Count('nices')).filter(status='Expired', num_nices__lt=4).order_by('?')[:42]
|
|
context['trademarks_acting'] = product.trademark_set.annotate(num_nices=Count('nices')).filter(status='Acting', num_nices__lt=4).order_by('?')[:42]
|
|
|
|
return context
|
|
|
|
|
|
import threading
|
|
import time
|
|
|
|
class SearchResultsThread(threading.Thread):
|
|
def __init__(self, keyword, **kwargs):
|
|
self.keyword = keyword
|
|
super(SearchResultsThread, self).__init__(**kwargs)
|
|
|
|
def run(self):
|
|
loaded = False
|
|
while not loaded:
|
|
loaded = self.keyword.load_results()
|
|
time.sleep(5)
|
|
|
|
|
|
class Search(generic.View):
|
|
# def get(self, request):
|
|
# <view logic>
|
|
# return HttpResponse('result')
|
|
@never_cache
|
|
def get(self, request, slug):
|
|
keyword = get_object_or_404(Keyword, slug=slug)
|
|
|
|
identity = keyword.searches.filter(similarity=146)[0]
|
|
|
|
contains = keyword.searches.filter(similarity=100)[0]
|
|
|
|
similar = keyword.searches.filter(similarity=97)[0]
|
|
|
|
response = {
|
|
'identity': {
|
|
'id': identity.id,
|
|
'status': identity.status,
|
|
'nices': identity.nices.count(),
|
|
'count': identity.results.all().count()
|
|
},
|
|
'contains': {
|
|
'id': contains.id,
|
|
'status': contains.status,
|
|
'count': contains.results.all().count()
|
|
},
|
|
'similar': {
|
|
'id': similar.id,
|
|
'status': similar.status,
|
|
'count': similar.results.all().count()
|
|
},
|
|
'status': 'ok'
|
|
}
|
|
|
|
return HttpResponse(json.dumps(response), content_type='application/json')
|
|
|
|
def post(self, request):
|
|
keyword = request.POST.get('keyword', '')
|
|
nices = request.POST.get('nices', '')
|
|
|
|
if keyword:
|
|
k, created = Keyword.objects.get_or_create(request=keyword, nices=nices)
|
|
else:
|
|
return HttpResponse(json.dumps({ 'status': 'error', 'description': 'Empty request'}), content_type="application/json")
|
|
|
|
loading = SearchResultsThread(keyword=k)
|
|
loading.start()
|
|
|
|
identity = k.searches.filter(similarity=146)[0]
|
|
|
|
contains = k.searches.filter(similarity=100)[0]
|
|
|
|
similar = k.searches.filter(similarity=97)[0]
|
|
|
|
response = {
|
|
'identity': {
|
|
'id': identity.id,
|
|
'status': identity.status,
|
|
'nices': identity.nices.count(),
|
|
'count': identity.results.all().count()
|
|
},
|
|
'contains': {
|
|
'id': contains.id,
|
|
'status': contains.status,
|
|
'count': contains.results.all().count()
|
|
},
|
|
'similar': {
|
|
'id': similar.id,
|
|
'status': similar.status,
|
|
'count': similar.results.all().count()
|
|
},
|
|
'slug': k.slug,
|
|
'status': 'ok'
|
|
}
|
|
|
|
return HttpResponse(json.dumps(response), content_type="application/json")
|
|
|
|
|
|
@never_cache
|
|
def get_random_list_trademarks(request, status='Expired', count=10):
|
|
morph = pymorphy2.MorphAnalyzer()
|
|
|
|
trademark_list = []
|
|
expired_trademarks = Trademark.objects.filter(status=status).order_by('?')[:count*2]
|
|
|
|
for trademark in expired_trademarks:
|
|
if len(trademark.title.split(' ')) > 3:
|
|
continue
|
|
|
|
nice_id = None
|
|
normalize_title = None
|
|
for product in trademark.products.all():
|
|
words = product.title.split(' ')
|
|
normalize_title = None
|
|
if len(words) <= 3:
|
|
first_word = morph.parse(words[0])[0]
|
|
|
|
if first_word.tag.POS == 'NOUN':
|
|
normalize_title = product.title
|
|
if len(words) == 1:
|
|
try:
|
|
normalize_title = word.inflect({'sing', 'nomn'}).word
|
|
except:
|
|
continue
|
|
|
|
nice_id = product.nice_id
|
|
break
|
|
|
|
if not normalize_title:
|
|
continue
|
|
|
|
try:
|
|
glyph = Nice.objects.get(nice_id=nice_id).glyph or 'fa-snowflake-o'
|
|
except Nice.DoesNotExist:
|
|
# Быть такого не должно
|
|
glyph = 'fa-snowflake-o'
|
|
|
|
trademark_list.append({
|
|
'product': normalize_title.capitalize(),
|
|
'name': trademark.title.title(),
|
|
'trademark': trademark.id,
|
|
'glyph': glyph
|
|
})
|
|
|
|
response = {
|
|
'count': len(trademark_list),
|
|
'status': 'ok',
|
|
'items': trademark_list[:count]
|
|
}
|
|
|
|
return HttpResponse(json.dumps(response), content_type="application/json")
|
|
|
|
|