Esempio n. 1
0
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')
Esempio n. 2
0
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
Esempio n. 3
0
 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()
Esempio n. 4
0
 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]
Esempio n. 5
0
 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']
Esempio n. 6
0
 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
Esempio n. 7
0
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()
Esempio n. 8
0
def get_counter(uid):
    cs = leadok.database.Customer.__table__
    q = select([cs.c.new]).where(cs.c.uid == uid)
    return engine.execute(q).scalar()
Esempio n. 9
0
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
Esempio n. 10
0
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))
Esempio n. 11
0
def upgrade_lead(lead_id, **vals):
    t = Lead.__table__
    q = t.update(t.c.id == lead_id).values(**vals)
    engine.execute(q).close()
Esempio n. 12
0
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()
Esempio n. 13
0
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))
Esempio n. 14
0
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