Ejemplo n.º 1
0
 def find_one_by_id(id):
     db = CouponsAlchemyDB()
     voucher_dict = db.find_one("vouchers", **{'id': id})
     if voucher_dict:
         voucher = Vouchers.from_dict(voucher_dict)
         return voucher
     return False
Ejemplo n.º 2
0
 def find_one(code, db=None):
     if not db:
         db = CouponsAlchemyDB()
     voucher_dict = db.find_one("vouchers", **{'code': code})
     if voucher_dict:
         voucher = Vouchers.from_dict(voucher_dict)
         return voucher
     return False
Ejemplo n.º 3
0
def validate_permission(permission):
    if not permission or not permission.value:
        return True
    agent_id = get_agent_id()
    db = CouponsAlchemyDB()
    agent_permission_dict = db.find_one("agent_permission", **{'agent_id': agent_id, 'permission_id': permission.value})
    if not agent_permission_dict:
        return False
    return True
Ejemplo n.º 4
0
 def find_one(id, db=None):
     id = binascii.a2b_hex(id)
     if not db:
         db = CouponsAlchemyDB()
     rule_dict = db.find_one("rule", **{'id': id})
     if rule_dict:
         rule_dict['id'] = binascii.b2a_hex(rule_dict['id'])
         rule = Rule(**rule_dict)
         return rule
     return False
Ejemplo n.º 5
0
 def find_one_all_vouchers(code, from_date, db=None):
     # from_date must be a timezone unaware UTC datetime object
     if not db:
         db = CouponsAlchemyDB()
     voucher_dict = db.find_one("all_vouchers", **{
         'code': code,
         'from': from_date
     })
     if voucher_dict:
         voucher = Vouchers.from_dict(voucher_dict)
         return voucher
     return False
Ejemplo n.º 6
0
def login():
    # return True
    agent_name = request.headers.get('X-API-USER', None)
    authorization = request.headers.get('X-API-TOKEN', None)
    authenticated = False
    if agent_name and authorization:
        db = CouponsAlchemyDB()
        token = db.find_one("tokens", **{'token': authorization, 'agent_name': agent_name})
        if token:
            user_dict = dict()
            user_dict['agent_id'] = token['agent_id']
            user_dict['agent_name'] = token['agent_name']
            user = User(**user_dict)
            setattr(request, 'user', user)
            authenticated = True
    if not authenticated:
        user_dict = dict()
        user_dict['agent_id'] = 0
        user_dict['agent_name'] = u'anonymous'
        user = User(**user_dict)
        setattr(request, 'user', user)

    return authenticated
Ejemplo n.º 7
0
 def save(self):
     values = self.get_value_dict()
     db = CouponsAlchemyDB()
     db.begin()
     # update_cache = False
     try:
         # check if the rule being created already exists, if yes, just return rule id
         existing_rule = db.find_one("rule", **{'id': self.id_bin})
         if existing_rule:
             # call is to update the existing rule with new attributes, just update and save it
             # update_cache = True
             logger.error('Trying to update an existing rule {}'.format(
                 self.__dict__))
             assert False, "A Rule is immutable."
         # no existing rule exists for this id, now check if a rule exists for given attributes
         rule_list = db.find("rule", **{'sha2hash': values.get('sha2hash')})
         if rule_list:
             for rule in rule_list:
                 rule['id'] = binascii.b2a_hex(rule['id'])
                 rule_obj = Rule(**rule)
                 if self == rule_obj:
                     self.id = rule_obj.id
                     break
         else:
             # update_cache = True
             db.insert_row("rule", **values)
         db.commit()
     except Exception as e:
         logger.exception(e)
         db.rollback()
         return False
     # else:
     #     db.commit()
     # if update_cache:
     #     self.update_cache()
     return self.id