def SearchProducts(self, res, doc): keys = [] for key in re.split(r'[,;\s]+', res.text): if key: keys.append(real_translit(key)) qlist = [] params = [] db = self._db for field in ('id', 'partno1', 'partno2'): qlist.append(sql.get_f('id', 'product', sql.fin(field, keys, True))) params += keys idl = db.fetchvals(sql.union(qlist), params) if not idl: return False res.count = len(idl) f = ['ps.id_product = p.id', 'pv.id_cat = ps.id_prop1'] q = sql.get_f('pv.id', ['pview pv', 'prop_set ps'], f) q = sql.order(q, 'pv.num') q = sql.limit(q, 0, 1) q = sql.get_f(['id', '(%s) AS id_view' % q], 'product p', sql.fin('id', idl)) for (idp, id) in db.fetchall(q): if id in res.filters: view = res.filters[id] else: view = classes.View(doc, id) view.k = doc.getk([view.id_cat], 'id') + [None] * 7 view.parsemap(view.k) view.products = [] res.filters[id] = view view.products.append(idp) for view in res.filters.itervalues(): view.frow = [sql.fin('id_product', view.products)] view.count = len(view.products)
def index(self): self.oldurls = [] self.newurls = {} self.parent = None self.catlist = db.fetchvals(sql.get_f('p.translit', 'prop p, cat c', 'c.id = p.id')) site.gvar.traceurl = self.traceurl db.execute(sql.update('status', 'sitemap'), [0]) for (mod, data) in const.SITE_MAP.iteritems(): for path in data: url = geturl(None, [mod, path], alwaysext = True) self.traceurl(url) q = sql.get_f('*', 'prop', 'id_group = getpgbytr("articles")') for row in db.fetchobj(q): url = geturl(None, [const.MOD_ARTICLE, row.translit], alwaysext = True) self.traceurl(url) while True: self.urls = self.newurls.copy() if not self.urls: break self.newurls.clear() for url in self.urls.iterkeys(): self.addurl(url) return site.gvar.traceurl = None db.execute(sql.delete('sitemap', 'status = 0'), commit = True)
def f(): q = sql.get_f('s.id', 'store s', ['p.id = s.id_product', 's.quantity > 0']) q = sql.get_f('p.id', 'product p', sql.exists(q)) q = sql.order(q, 'p.id') pids = db.fetchvals(q) idmax = len(pids) - 1 idl = map(lambda x: str(pids[random.randint(0, idmax)]), xrange(10)) idl = ",".join(idl) site.config.set("RANDOM_PRODUCTS", idl) site.config.set("STATIC_PRODUCT", pids[0])
def attr(self, name): if name == 'url': r = self.doc.request query = dict(hl = self.hl, disc = r('disc'), adpr = r('adpr'), cdef = r('cdef')) if isfiche(self.source): return self.doc.geturl(self.param[:1] + self.param[2:], query = query, view = self.view, addmod = False) else: return self.doc.geturl(self.param[:const.PRODUCT_URL_LEN] + [self.partno1], query = query, addview = False, addmod = False) elif name == 'image': return self.doc.getimage(self.id_image, self, self.param) elif name == 'imagebig': if isproduct(self.source) and self.images: return self.images[0] elif name == 'images': q = sql.get_fval(['id_image'], 'image_deps', 'id_obj') q = sql.order(q, 'id') res = [] i = 1 for id in self.db.fetchvals(q, [self.id]): img = self.doc.getimage(id, self, self.param, i) res.append(img) i += 1 return res elif name == 'stored': qstore = sql.get_fval('SUM(quantity)', 'store', 'id_product') return self.db.fetchval(qstore, [self.id]) elif name == 'store': fields = ['SUM(s.quantity) AS quantity', 'pr.val1 AS eta', 'peta.name AS etaname'] f = ['p.id = s.id_store', 'id_product = %s'] f.append('pr.id_prop = p.id') f.append('pr.name = "ETA"') f.append('peta.translit = pr.val1') qstore = sql.get_f(fields, ['store s', 'prop p', 'param pr', 'prop peta'], f) qstore = sql.group(qstore, 'eta') qstore = sql.order(qstore, 'eta') res = Storage() res.data = self.db.fetchobj(qstore, [self.id]) res.quantity = self.stored res.available = True if self.stored else False q = sql.get_f('p.*', ['param pr', 'prop p'], ['pr.id_prop = %s', 'pr.name = "ETA"', 'p.translit = pr.val1']) res.default = self.db.fetchrowobj(q, [self.view.id_cat]) return res return None
def setcat(self, translit): """Устанавливает объект-категорию товара и его главное представление""" q = sql.get_f('c.id', 'prop p, cat c', ['p.translit = %s', 'c.id = p.id']) gvar.id_cat = db.fetchval(q, [translit]) # Установка категории (через создание объекта <Категория товара>) gvar.cat = classes.Cat(gvar.doc, gvar.id_cat) f = ['v.id_cat = c.id', 'v.pview = "S"', 'c.id = %s'] q = sql.get_f('v.id', 'pview v, cat c', f) # Установка представления (через создание объекта # <Представление товара>) gvar.view = classes.View(gvar.doc, db.fetchval(q, gvar.id_cat))
def prodlist(self, doc, qsort): # SQL запрос - временная таблица 1 с ID товарами фильтра sqlpids = self.sqlpids(self.doc.view.pview, self.ppair) q = sql.temp(sqlpids, 'plist', sql.get(['count(*)'], '%s'), ['id_product']) # Блокируем DB для выполнения пакета запросов в одной сессии db = site.db.self() try: # Задаем кол-во товаров и страницы self.prodcount = db.fetchval(q) t = [self.doc.tproduct, sql.tempname('plist') + ' ps'] f = ['ps.id_product = p.id'] q = sql.get_f(['p.id'], t, f) q = sql.order(q, qsort) q = sql.limit(q, self.ppg * (self.page - 1), self.ppg) pids = db.fetchvals(q) if not pids: return [] pl = self.__proddata(db, doc.view, pids) if not doc.view.iss: self.__prodhint(db, doc.view, pl) finally: pass #db.shunlock() prodlist = map(lambda pid: pl[pid], pids) return prodlist
def f(): f = ['ps.id_product = p.id', 'pr.id_prop = ps.id_prop1', 'pr.name = "ETA"'] q = sql.get_f('pr.val1', ['prop_set ps', 'param pr'], f) q = '(%s)' % sql.limit(q, 0, 1) q = sql.update('eta', 'product p') % q db.execute(q) f = ['s.id_product = p.id', 'pr.id_prop = s.id_store', 'pr.name = "ETA"'] q = sql.get_f('pr.val1', ['store s', 'param pr'], f) q = sql.order(q, 'pr.val1') q = '(%s)' % sql.limit(q, 0, 1) fu = sql.get_f('id', 'store s', 's.id_product = p.id') fu = sql.exists(fu) q = sql.update('eta', 'product p', filter = fu) % q db.execute(q) db.commit()
def load(self): if not 'param' in self: f = ['pv.pview = ps.pview'] f.append('ps.id_product = %s') f.append('ps.id_prop1 = pv.id_cat') p = [self.id] if self.psid: f.append('ps.id = %s') p.append(self.psid) else: f.append('ps.pview = %s') p.append(const.DEFAULT_PVIEW) q = sql.get_f('pv.id AS id_view, ps.*', ['pview pv', self.tpropset], f) row = self.db.fetchrowobj(q, p) self.param = self.doc.getprops(row) self.view = View(self.doc, row.id_view) if isproduct(self.source): self.price = self.doc.getprice(self.price, self.smallprice) self.code = formatnumber(self.id) self.note = self.note or ''
def params(self, params): if len(params) == 0: self.NotFound() id = self.__fastid(params) if id: pass #log('FAST ID for %s' % (params)) else: #log('SLOW ID for %s' % (params)) id = self.__slowid(params) if not id: self.NotFound("Can't find image ID") if 'oem' in params[:2]: return self.__ficheimage(id) q = sql.get_f(['i.type, i.image, s.eddate'], ['image i', 'sysid s'], ['s.id = i.id', 'i.id = %s']) obj = db.fetchrowobj(q, [id]) if not obj: self.NotFound() self.ext = img_typ2ext(obj.type) self.format = self.ext self.data = obj.image web.http.lastmodified(obj.eddate)
def __tryproduct(self, params): self.opt.check = True self.opt.pid = None if len(params) != const.PRODUCT_URL_LEN + 1 or self.cat.isfiche: return klist = self.k[:const.PRODUCT_URL_LEN] if None in klist: return klist = map(lambda k: k.id, klist) fields = map(lambda x: 'ps.id_prop%s' % x, xrange(1, 9)) fields.append('p.id') tables = [self.tproduct, self.tpropset] f = ['ps.id_product = p.id', 'p.partno1 = %s'] q = sql.get_f(fields, tables, f) for row in db.fetchobj(q, params[-1]): found = 0 props = [] for (key, val) in row.iteritems(): if key[:7] == 'id_prop': props.append(val) if val in klist: found += 1 if found == const.PRODUCT_URL_LEN: #del self.k[-1] self.k = self.getk(props, 'id') self.opt.pid = row.id self.opt.check = False
def sethintmap(self): views = self.doc.views self.hintmap = [] cnt = 0 for view in views: if view.iss: continue mapitem = Storage() mapitem.hints = [] mapitem.view = view (fields, tables, f) = view.nonssql([self.id]) q = sql.get_f(fields, tables, f) exc = eval(self.doc.cat.params('EXC_%s_HINTS' % view.pview) or '[]') for row in self.db.fetchobj(q): hints = [] for prop in xrange(1, len(view.groups) + 1): if prop in exc: continue s = str(prop) hints.append(row['n' + s]) hint = formatname(hints[1:]) mapitem.hints.append(hint) cnt += 1 self.hintmap.append(mapitem) if not cnt: self.hintmap = None
def getfid(field, value): t = 'fiche_price pc, fiche_prop_set ps' f = ['pc.id_product = ps.id_product'] f.append('ps.id_prop2 = %s') f.append('pc.%s = ' % field + '%s') q = sql.get_f('pc.id_product, ps.id, pc.id', t, f) return db.fetchrow(q, [idbb, value])
def sqlpids(self, pview, ppair, adfilter = [], usemem = False): f = self.sqlppair(ppair) f += ["ps.id_product = p.id"] f += ["ps.pview = '%s'" % pview] + adfilter if self.request('show_store'): fstore = sql.get_f('s.id', 'store s', ['s.id_product = p.id', 's.quantity > 0']) fstore = sql.exists(fstore) f += [fstore] if usemem: tpropset = const.MEM_SEARCH + ' ps' else: tpropset = self.tpropset field = 'ps.id_product' if not self.cat.isfiche: field = 'DISTINCT ' + field q = sql.get_f([field], tpropset + ', ' + self.tproduct, f) return q
def f(): q = sql.get_f("p.*", ['cat c', 'prop p'], ['c.id = p.id', 'c.visible = 1']) for cat in db.fetchobj(q): url = '/catalog/%s' if cat.translit == 'masla': url += '/brand' request = Request(url % cat.translit, dict(static = True, marker = 'menu', frontdata = True)) request.getdata()
def fetchobject(self, table, id, field = 'id'): if type(table) == list: t = table[0] f = map(lambda t2: '%s.%s = %s.%s' % (t, field, t2, field), table[1:]) f.append('%s.%s = %s' % (t, field, '%s')) q = sql.get_f(['*'], sql.joiner(table), f) else: q = sql.get_fval(['*'], table, field) return self.fetchrowobj(q, [id])
def clear(self, ftext): """Очистка наличия товаров на складе перед загрузкой новых данных о наличии""" # Получение идентификаторов товаров, данные о наличии которых # будут обновлятся f = self.parsefilter(ftext) q = sql.get_f('DISTINCT s.id_product', 'store s', f) idl = db.fetchvals(q) pcnt = len(idl) if pcnt: log("Found %d products" % pcnt) else: log("No products found, nothing to clear!", M_WARN) return # Вывод 5-ти случайных товаров из того списка, данные о наличии # которых будут очищены log('----------------------------') imax = min(pcnt, 5) for i in xrange(imax): index = random.randint(0, pcnt - 1) product = Product(None, idl[index]) log("Random product (%d of %d): %s" % (i + 1, imax, real_translit(product.name))) log('----------------------------') log('%d products will be deleted from store. Continue? (y/n)' % pcnt) # Подтверждение об удалении наличия (если требуется) if self.o.prompt: res = raw_input() else: res = "y" # Очистка наличия товаров из базы if res == 'y': idl = db.fetchvals(sql.get_f('id', 'store s', f)) q = sql.delete('store', sql.fin('id', idl)) db.execute(q) db.commit() log("Store records deleted") else: log("User abort, exiting...") sys.exit(0)
def prepare(self, doc): fblock = Storage() filters = [] ft = [] fp = [] params = {} filters = [] URL = '/lib/ichain.html' for key in ['mnf', 'mgroup', 'model', 'myear']: val = self(key) if val: ft.append(key + '=%s') fp.append(val) params[key] = val f = Storage() f.url = URL f.key = key f.val = val f.params = "&".join(map(lambda x: x[0] % urlquote(x[1]), zip(ft, fp[:-1]))) filters.append(f) else: q = sql.get_f('DISTINCT ' + key, 'dc_chain', ft) q = sql.order(q, key) fblock.data = [] for val in db.fetchvals(q, fp): if not val: continue row = Storage() row.name = val purl = params.copy() purl[key] = val row.url = URL row.params = dic2str(purl, "&", "%s=%s") fblock.data.append(row) fblock.key = key break if self('mgroup'): q = sql.get_f(['model', 'myear', 'chain', 'links'], 'dc_chain', ft) q = sql.order(q, 'model, myear') chains = db.fetchobj(q, fp) self.add(chains = chains) self.add(fblock = fblock, filters = filters, params = params)
def prepare(self, doc): gid = db.pgbytr("news") t = ['prop p', 'sysid s'] f = ['p.id = s.id', 'p.id_group = %s'] q = sql.get_f('p.*, s.*', t, f) q = sql.order(q, 's.addate DESC') news = [] for row in db.fetchobj(q, [gid]): row = classes.Prop(doc, row) row.new = newmarker(row.addate) news.append(row) self.add(news = news)
def f(): site.search.reload() q = sql.get(['COUNT(id)'], 'product') site.config.set('PRODUCTS', db.fetchval(q)) q = sql.get(['COUNT(id)'], 'fiche_prop_set') site.config.set('PRODUCTS', db.fetchval(q), 2) q = sql.get_f('p.translit', 'prop p, cat c', 'c.id = p.id') f = open(config.CAT_MAP, "w") for row in db.fetchvals(q): f.write('%s %s\n' % (row, row)) f.close()
def getprops(self, row): idl = map(lambda i: row['id_prop' + str(i + 1)], xrange(8)) f = sql.fin('id', vallist(idl)) props = db.fetchdic(sql.get_f(['id', 'translit'], 'prop', f)) dk = [] for id in idl: if id in props: dk.append(props[id]['translit']) else: dk.append(None) return dk
def prepare(self, doc): gid = db.pgbytr("advert") t = ['prop p', 'sysid s'] f = ['p.id = s.id', 'p.id_group = %s'] q = sql.get_f('p.*, s.*', t, f) q = sql.order(q, 's.addate DESC') q = sql.limit(q, 0, 1) adverts = [] for row in db.fetchobj(q, [gid]): row = classes.Prop(doc, row) adverts.append(row) self.add(adverts = adverts)
def reload(self): self._db.execute(const.SQL_SEARCH) q = sql.get_f(['id', 'name', 'fname', 'id_group'], 'prop', 'id < 10000000') self._props = self._db.fetchdic(q, 'id') re_w = re.compile(u'[a-z0-9]+') for (id, data) in self._props.iteritems(): name = data.name if data.fname and data.fname != name: name += data.fname name = real_translit(name).lower() name = u''.join(re_w.findall(name)) self._props[id] = (name, data.id_group, 0)
def adfloats(f): floats = [] tlist = dict(map(lambda x: (x.val1, x), filter(f, pdata))) keys = tlist.keys() if tlist: q = sql.get_f('*', 'prop', sql.fin('translit', keys, True)) for row in db.fetchobj(q, keys): prop = classes.Prop(doc, row) prop.evalname(self.ctx) prop.evalnote(self.ctx) prop.para = tlist[prop.translit] floats.append(prop) return floats
def __getdata(self, doc, text): if not text: return None props = self._getprops(text) if not props: return None q = sql.get_f(['pv.*'], 'pview pv, cat c', 'pv.id_cat = c.id') data = {} for row in self._db.fetchobj(q): view = classes.View(doc, row) frow = self._getfrow(props, view) if not frow: continue #else: # frow.append('id_prop1 = "%s"' % view.id_cat) view.k = doc.getk([view.id_cat], 'id') + [None] * 7 view.parsemap(view.k) frow += map(lambda s: "id_prop%d = %d" % s, view.ppair.iteritems()) view.frow = frow view.q = sql.get_f(['COUNT(DISTINCT id_product)'], const.MEM_SEARCH, frow) view.qpid = sql.get_f(['DISTINCT id_product'], const.MEM_SEARCH, frow) skip = False for (id, aview) in data.iteritems(): if (aview.id_cat == view.id_cat) and (aview.groups == view.groups): if view.num < aview.num: del data[aview.id] else: skip = True if not skip: data[view.id] = view return data
def prepare(self, doc): if self('static'): pid = site.config('STATIC_PRODUCT') else: idl = site.config('RANDOM_PRODUCTS') idl = idl.split(",") idx = random.randint(0, len(idl) - 1) pid = idl[idx] pid = toint(pid) product = classes.Product(doc, pid) self.add(product = product) gid = db.pgbytr("news") t = ['prop p', 'sysid s'] f = ['p.id = s.id', 'p.id_group = %s'] q = sql.get_f('p.*, s.*', t, f) q = sql.order(q, 's.addate DESC') q = sql.limit(q, 0, 3) news = [] for row in db.fetchobj(q, [gid]): row = classes.Prop(doc, row) row.new = newmarker(row.addate) news.append(row) self.add(news = news) gid = db.pgbytr("articles") t = ['prop p', 'sysid s'] f = ['p.id = s.id', 'p.id_group = %s'] q = sql.get_f('p.*, s.*', t, f) q = sql.order(q, 's.addate DESC') q = sql.limit(q, 0, 3) articles = [] for row in db.fetchobj(q, [gid]): row = classes.Prop(doc, row) row.new = newmarker(row.addate) row.url = doc.geturl([MOD_ARTICLE, row.translit], alwaysext = True) articles.append(row) self.add(articles = articles)
def sql(self, full = False): if not self.view.doc.cat.isproduct: return [] self.params() f = [] if self.price_min: f.append('p.price >= %.5f' % (self.price_min - 0.5)) if self.price_max: f.append('p.price < %.5f' % (self.price_max + 0.5)) if self.store: f.append(self.__sqlstore()) if full and f: f += ['ps.id_product = p.id'] f = sql.get_f('p.id', self.view.doc.tproduct, f) f = ['ps.id_product IN (%s)' % f] return f
def img_addcrclist(db, alist): if not alist: return lst = {} for (param, id) in alist: lst[getcrc(param)] = id crclist = lst.keys() q = sql.get_f('crc', 'mem_image_crc', sql.fin('crc', crclist, True)) crclist = db.fetchvals(q, crclist) for crc in crclist: if crc in lst: del lst[crc] q = sql.insert(['crc', 'id_image'], 'mem_image_crc') for (crc, id) in lst.iteritems(): db.execute(q, [crc, id])
def __prodhint(self, db, view, pl): doc = self.doc fields = ['p.id'] tables = [doc.tpropset] k = len(view.groups) + 1 for i in xrange(1, k): tables.append('LEFT JOIN prop p%d ON ps.id_prop%d = p%d.id' % (i, i, i)) fields.append('p%d.name AS n%d' % (i, i)) fields.append('p%d.translit AS t%d' % (i, i)) tables = ["\n ".join(tables)] tables.append(doc.tproduct) f = [sql.fin('p.id', pl.keys())] f.append("p.id = ps.id_product") f.append("ps.pview = '%s'" % view.pview) f += doc.sqlppair(self.ppair) if doc('adfilter'): f += doc('adfilter') q = sql.get_f(fields, tables, f) kparams = map(lambda k: k.translit, doc.kseries) exc = eval(doc.cat.params('EXC_%s_HINTS' % view.pview) or '[]') for row in db.fetchobj(q): hints = [] rate = 0 for prop in xrange(1, k): if prop in exc: continue s = str(prop) h = row['n' + s] p = row['t' + s] if p in kparams: rate -= 1 hints.append(h) hint = Storage() hint.name = formatname(hints[1:]) hint.rate = rate pl[row.id].hints.append(hint) sz = 3 for p in pl.itervalues(): besthints = sorted(p.hints, key = lambda h: h.rate)[:sz] besthints = map(lambda h: h.name, besthints) if len(p.hints) > sz: besthints.append('...') p.hint = " / ".join(besthints)
def load_store(self, pdata): """Загрузка единицы хранения товара из данных хэша pdata. Если единица товара создана/обновлена - возвращает True, иначе False """ # Поиск серийного номера товара в хэше (ошибочно назван partno) # Если нет серийного номера товара - выходим if 'partno2' in pdata: key = 'partno2' elif 'partno1' in pdata: key = 'partno1' else: return False partno = pdata[key] quantity = toint(pdata['quantity']) # Поиск объекта-склада, если не найден, # использовать склад по умолчанию if 'store'in pdata and pdata['store']: storeobj = db.fetchobject('prop', pdata['store'], 'translit') else: storeobj = self.whdef # Если нет информации о количестве товара для единицы хранения - выход if not quantity: return False # Поиск в базе идентификатора товара, если товар не найден - выход f = ['p.id = ps.id_product'] f.append(key + ' = %s') f += self.kfilter q = sql.get_f('p.id', 'product p, prop_set ps', f) pid = db.fetchval(q, [partno]) if pid == None: return False # Создание и сохранение единицы хранение товара store = Store(pdata) store.id_product = pid store.id_store = storeobj.id store.quantity = quantity store.save() return True
def prepare(self, doc): q = sql.get_f('*', 'prop p, sysid s', ['p.id = s.id', 'p.id_group = %s']) articles = {} gid = db.pgbytr("articles") for row in db.fetchobj(q, [gid]): (section, name) = row.name.split('/', 2) row = classes.Prop(doc, row) row.name = name row.url = doc.geturl([row.translit], alwaysext = True) row.new = newmarker(row.addate) if section in articles: articles[section].append(row) else: articles[section] = [row] for lst in articles.itervalues(): lst.sort(key = lambda x: x.addate, reverse = True) self.add(articles = articles)