@ -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 )
@ -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 ( )