def insert_new_lead(lead, winner): t = Lead.__table__ q = select([func.max(t.c.id2)]).\ where(t.c.winner == winner.uid) lead['winner'] = winner.uid lead['price'] = winner.get_price() for _ in range(5): try: max_id2 = engine.execute(q).scalar() or 0 lead['id2'] = max_id2 + 1 assigned_id = engine.\ execute(t.insert().values(lead)).inserted_primary_key[0] logger.info('Lead inserted into DB, id assigned: {}'.format(assigned_id)) return except Exception: time.sleep(0.25) for _ in range(5): try: max_id2 = engine.execute(q).scalar() or 0 lead['id2'] = max_id2 + random.choice(range(5, 13)) assigned_id = engine.\ execute(t.insert().values(lead)).inserted_primary_key[0] logger.info('Lead inserted into DB, id assigned: {}'.format(assigned_id)) return except Exception: time.sleep(0.5) logger.error('Lead has not been inserted into DB')
def increment_counter(uid): n = get_counter(uid) cs = leadok.database.Customer.__table__ q = cs.update(cs.c.uid == uid).\ values(new=n + 1) engine.execute(q).close() return True
def set_req(self, req): cs = leadok.database.Customer.__table__ data = dict(req_mon=req, req_tue=req, req_wed=req, req_thu=req, req_fri=req, req_sat=req, req_sun=req) q = cs.update(cs.c.uid == self.uid).values(data) engine.execute(q).close()
def __init__(self, domain=None): cs = leadok.database.Customer.__table__ cond = [ cs.c.archived == False, ] if domain is not None: cond.append(cs.c.domain == domain.name) q = select([cs]).where(and_(*cond)).order_by(cs.c.uid) self._data = list(engine.execute(q)) self.uids = [c['uid'] for c in self._data]
def __init__(self, uid, amount, date, method, status, comment=''): self.uid = uid self.amount = amount self.date = date self.method = method self.status = status self.comment = comment if not self.comment: t = customers_payment_data_table q = select([t]).where(t.c.uid == self.uid).\ where(t.c.payment_method == self.method) r = engine.execute(q).first() if r is not None: self.comment = r['payment_info']
def set_settings(self, data): days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] cs = leadok.database.Customer.__table__ for day in days: try: h_st = int(data['working_hours_start_' + day].split(':')[0]) m_st = int(data['working_hours_start_' + day].split(':')[1]) h_fi = int(data['working_hours_finish_' + day].split(':')[0]) m_fi = int(data['working_hours_finish_' + day].split(':')[1]) req = int(data['num_leads_required_' + day]) q = cs.update(cs.c.uid == self.uid).\ values({'beg_' + day: datetime.time(h_st, m_st), 'end_' + day: datetime.time(h_fi, m_fi), 'req_' + day: req}) engine.execute(q).close() except Exception: pass for key in ['on', 'balance_limit', 'notes', 'allow_to_set_req']: try: q = cs.update(cs.c.uid == self.uid).\ values({key: data[key]}) engine.execute(q).close() except Exception: pass try: q = cs.update(cs.c.uid == self.uid).\ values(buffer=data['buffer_customer']) engine.execute(q).close() except Exception: pass try: q = cs.update(cs.c.uid == self.uid).\ values(email_needed=data['send_email']) engine.execute(q).close() except Exception: pass logger.debug('Settings successfully set for ' 'customer [{}]'.format(self.uid)) return True
def get_customers_count_for_domain(domain_name): cs = leadok.database.Customer.__table__ q = select([func.count(cs.c.uid)]).where(cs.c.domain == domain_name) return engine.execute(q).scalar()
def get_counter(uid): cs = leadok.database.Customer.__table__ q = select([cs.c.new]).where(cs.c.uid == uid) return engine.execute(q).scalar()
def reset_counter(uid): cs = leadok.database.Customer.__table__ q = cs.update(cs.c.uid == uid).values(new=0) engine.execute(q).close() return True
def get_customer(uid): cs = leadok.database.Customer.__table__ q = select([cs]).where(cs.c.uid == uid).\ where(cs.c.archived == False) row = engine.execute(q).first() return None if row is None else Customer(dict(row))
def upgrade_lead(lead_id, **vals): t = Lead.__table__ q = t.update(t.c.id == lead_id).values(**vals) engine.execute(q).close()
def record_that_lead_was_seen_by_user(lead_id, ip_address): t = leads_history_table q = t.insert().values(action='seen_by_user', lead_id=lead_id, comment=str(ip_address)) engine.execute(q).close()
def get_lead_history(lead_id): t = leads_history_table q = select([t]).where(t.c.lead_id == lead_id).\ order_by(t.c.date.desc()) return list(engine.execute(q))
def get_price_sum(**kwargs): conditions = define_conditions(**kwargs) q = select([func.sum(Lead.__table__.c.price)]).where(conditions) result = engine.execute(q).scalar() return 0.0 if result is None else result