def post(self): err = JsOb() if not self.json.email: err.email = 'Please input email' elif not is_email(self.json.email): err.email = 'Email not valid' if not self.json.password: err.password = '******' if not self.json.captcha_code: err.captcha_code = 'Please input captcha code' elif not captcha_verify(self.json.captcha_key, self.json.captcha_code): err.captcha_code = 'captcha code incorrect' if not err: try: user = User.verify(self.json.email, self.json.password) if user: if user._role == 'Affiliate' or user._role == 'Advertiser': err.email = u'You don\'t have a login permissions!' else: self._session_new(self.json.email, user._id) user.last_login = DateTime().current_time user.save() except UserNotFoundError: err.email = "email not found" except PasswordNotMatchError: err.password = "******" self.render(err)
def post(self): err = JsOb() if not self.json.email: err.email = 'Please input email' elif not is_email(self.json.email): err.email = 'Email not valid' if not self.json.password: err.password = '******' if not self.json.captcha_code: err.captcha_code = 'Please input captcha code' elif not captcha_verify(self.json.captcha_key, self.json.captcha_code): err.captcha_code = 'captcha code incorrect' if not err: try: user = User.verify(self.json.email, self.json.password) if user: affiliate = Affiliate.find_one(dict(user_id=int(user._id))) if affiliate: if affiliate.status == '1': self._session_new(self.json.email, user._id) user.last_login = DateTime().current_time user.save() else: err.application = 'The Account is Approving....' else: err.email = 'Affiliate not found, Please call the manager!' except UserNotFoundError: err.email = "email not found" except PasswordNotMatchError: err.password = "******" self.render(err)
def post(self): err = JsOb() pause_date = self.json.pause_date if not pause_date: err.pause_date = u'pause_date cat\'t be empty!' elif Tool.str_datetime(pause_date, '%Y-%m-%d %H:%M:%S') <= datetime.datetime.now(): err.pause_date = u'The pause time can\'t less than Now!' if not err: self.offer_id = self.json.offer_id job_type = 'date' store_executor_alias = 'offer' process_count = 5 max_instances = 100 scheduler = ScheInterface(job_type, store_executor_alias, process_count) res = scheduler.add_date_job(pause, [self.offer_id], pause_date, max_instances, self.job_listener) if res: scheduler.sche.start() self.send_email(pause_date, self.offer_id) offer = Offers.find_one(dict(_id=int(self.offer_id))) offer.pause_job_set = True offer.last_update = DateTime().current_time offer.save() else: err.pause_date = 'Add pause job failure!' self.render(err)
def query(cls, start, end=None): if not end: end = DateTime().today spec = { 'is_api': {"$ne": True}, 'traffic_time': { '$lte': end, '$gte': start } } return cls.find(spec)
class PostBack(Doc): structure = dict( _id=int, affiliate_id=int, url=str, createdTime=str, last_update=str, deleted=bool ) _t = DateTime() default_values = dict( createdTime=_t.current_time, last_update=_t.current_time, deleted=False ) @classmethod def add(cls, affiliate_id, url): post_back = PostBack(dict( _id=_gid(GidKey.post_back_key), affiliate_id=int(affiliate_id), url=url ), True) post_back.save() return True @classmethod def update(cls, _id, url): post_back = PostBack.find_one(dict(_id=int(_id))) if not post_back: raise PostBackNotFound post_back.url = url post_back.last_update = cls._t.current_time post_back.save() return post_back @classmethod def delete(cls, _id): post_back = PostBack.find_one(dict(_id=int(_id))) post_back.deleted = True post_back.last_update = cls._t.current_time post_back.save() return True @classmethod def query(cls, affiliate_id): return PostBack.find(dict(affiliate_id=int(affiliate_id), deleted=False))
def get(self): status_type = {'0': False, '1': True} affiliate_id = int(self.get_argument('affiliate_id')) status = self.get_argument('status') limit = int(self.get_argument('limit')) page = int(self.get_argument('page')) skip = (page - 1) * limit spec = dict(affiliate_id={"$in": [affiliate_id]} if affiliate_id else {'$ne': ''}, status=status_type[status] if status else {'$ne': ''}, currency=self.get_argument('currency', {'$ne': ''}), createdTime={ '$gte': self.get_argument('start', _t.get_day(6)), '$lte': self.get_argument('end', _t.today), }) invoices = Invoice._get_sum(spec, limit=limit, offset=skip) invoice_count = Invoice.count(spec) for i in invoices: affiliate = User.find_one(dict(_id=int(i.affiliate_id))) i['affiliate_name'] = affiliate.account self.finish(dict(invoices=invoices, invoice_count=invoice_count))
class Advertisers(Doc): structure = dict( _id=int, user_id=int, country=str, account_manager=int, pm=int, offer_count=int, white_list=list, create_time=str, last_update=str, status=str, # 0 delete, 1 active, 2 pause ) t = DateTime() required_fields = [] default_values = { 'create_time': t.current_time, 'last_update': t.current_time, } @classmethod def _save(cls, **kwargs): kwargs.update(dict(_id=_gid(GidKey.advertiser_key))) ads = Advertisers(kwargs, True) ads.save() return ads @classmethod def _update(cls, ad_id, **kwargs): ads = cls.find_one(dict(_id=int(ad_id))) ads._id = int(ad_id) ads.country = kwargs['country'] ads.white_list = kwargs['white_list'] ads.account_manager = kwargs['account_manager'] ads.pm = kwargs.get('pm') ads.last_update = cls.t.current_time ads.status = kwargs['status'] ads.save() return ads
def data_init(): _t = DateTime() for r in ['AM', 'Affiliate', 'Advertiser', 'BD', 'PM']: role = Role( dict(_id=gid(GidKey.role_key), name=r, permission=[], createdTime=_t.current_time, last_update=_t.current_time, deleted=False)) role.save() user = User( dict(_id=gid(GidKey.user_key), account='admin', email=['*****@*****.**'], password=User.encrypt_pwd(u'chizicheng521'), role_id='', is_admin=True, createdTime=_t.current_time, last_update=_t.current_time, last_login=_t.current_time, deleted=False)) user.save()
class Category(Doc): structure = dict( _id=int, name=str, status=int, # 0 deleted; 1 active; 2 pending; create_time=str, last_update=str, ) _t = DateTime() default_values = dict( create_time=_t.current_time, last_update=_t.current_time, ) @classmethod def _query(cls): cat = cls.find(dict(status={"$ne": '0'})) return cat @classmethod def _save(cls, name, status): cat = Category( dict(_id=_gid(GidKey.category_key), name=name, status=status), True) cat.save() @classmethod def _update(cls, cat_id, **kwargs): cat = cls.find_one(dict(_id=int(cat_id))) cat._id = int(cat_id) cat.name = kwargs['name'] cat.status = kwargs['status'] cat.last_update = cls._t.current_time cat.save()
class EMail(Doc): structure = dict( _id=int, user_id=int, model_id=int, subject=str, sender=str, receiver=list, fail_receiver=[ { 'receiver': str, 'reason': str, } ], message=str, createdTime=str, sendTime=str, ) _t = DateTime() indexes = [ {'fields': ['_id']}, {'fields': ['receiver']}, ] default_values = { 'sender': e_conf.get('sender'), 'fail_receiver': [], 'createdTime': _t.current_time, 'sendTime': _t.current_time } gearman_email_job = EmailClient(GEARMAN_SERVER) @classmethod def _create(cls, **kwargs): e_mail = EMail(gen_skel=True) e_mail._id = _gid(GidKey.email_key) e_mail.user_id = int(kwargs.get('user_id')) e_mail.model_id = int(kwargs.get('model_id')) e_mail.subject = kwargs.get('subject') e_mail.sender = kwargs.get('sender', e_mail.sender) e_mail.receiver = kwargs.get('receiver') e_mail.message = kwargs.get('message') e_mail.save() cls._send(e_mail) # 此处暂时先存后发送 @classmethod def _send(cls, e_mail): message = cls.gearman_email_job.create_message(e_mail.subject, e_mail.message) res = cls.gearman_email_job.send( e_conf.get('email_server'), e_conf.get('email_server_port'), e_mail.sender, e_mail.receiver, e_conf.get('user_name'), e_conf.get('password'), e_conf.get('worker'), message, e_mail._id, ) return res @classmethod def _check_email_stats(cls, _id, res): cls.gearman_email_job.check_email_status(res) @classmethod def _query(cls, user_id, limit=0, offset=0): spec = dict(user_id=int(user_id)) fields = dict(_id=1, subject=1, sendTime=1) return cls.find(spec, fields, limit=limit, skip=offset) @classmethod def _fail_email(cls, _id, receiver, reason=None): e = cls.find_one({'_id': int(_id)}) e.fail_receiver.append(dict(receiver=receiver, reason=reason)) e.save()
class OfferAffiliate(Doc): structure = dict( _id=int, offer_id=int, affiliate_id=int, payout=float, day_cap=int, month_cap=int, total_cap=int, cost=float, cap=dict, status=str, # 0 rejected, 1 approved, 2 pending create_time=str, last_update=str, traffic_time=str, clicks=int, conversions=int, deleted=bool # 0 delete ) _t = DateTime() default_values = { 'create_time': _t.current_time, 'last_update': _t.current_time, 'traffic_time': '', 'deleted': False } @classmethod def _save(cls, **kw): day_cap = 10000000 month_cap = 10000000 total_cap = 10000000 if kw.get('day_cap'): day_cap = int(kw.get('day_cap')) if kw.get('month_cap'): month_cap = int(kw.get('month_cap')) if kw.get('total_cap'): total_cap = int(kw.get('total_cap')) offer_affiliate = OfferAffiliate( dict(_id=_gid(GidKey.off_aff_key), offer_id=int(kw.get('offer_id')), affiliate_id=int(kw.get('affiliate_id')), payout=float(kw.get('payout', 0)) if kw.get('payout') else 0, day_cap=day_cap, month_cap=month_cap, total_cap=total_cap, cap=dict(day=day_cap, month=month_cap, total=total_cap), status=kw.get('status', '2')), True) offer_affiliate.save() return offer_affiliate @classmethod def _query(cls, offer_ids=None, affiliate_id=None): spec = {'deleted': False} if offer_ids: spec.update( dict(offer_id={ "$in": [int(offer_id) for offer_id in offer_ids] })) if affiliate_id: spec.update(dict(affiliate_id=int(affiliate_id))) return cls.find(spec) @classmethod def _delete(cls, _id): if _id: off_aff = OfferAffiliate.find_one(dict(_id=int(_id))) off_aff.deleted = True off_aff.save() return True return False @classmethod def _update(cls, _id, **kw): off_aff = cls.find_one(dict(_id=int(_id))) if kw.get('affiliate_id'): off_aff.affiliate_id = int(kw.get('affiliate_id')) if kw.get('offer_id'): off_aff.offer_id = int(kw.get('offer_id')) if kw.get('payout'): off_aff.payout = float(kw.get('payout')) if kw.get('day_cap'): off_aff.day_cap = float(kw.get('day_cap')) if kw.get('month_cap'): off_aff.month_cap = float(kw.get('month_cap')) if kw.get('total_cap'): off_aff.total_cap = float(kw.get('total_cap')) off_aff.cap = dict(day=off_aff.day_cap, month=off_aff.month_cap, total=off_aff.total_cap), if kw.get('status'): off_aff.status = kw.get('status') off_aff.last_update = cls._t.current_time off_aff.save() return True
class Role(Doc): ''' Fields: permission 权限组 {mail: {create: True, delete: False, update: True, query: True}, invoice: {}, ...} ''' structure = dict(_id=int, name=str, permission=dict, createdTime=str, last_update=str, deleted=bool) _t = DateTime() indexes = [ { 'fields': ['_id'] }, { 'fields': ['name', 'deleted'] }, ] default_values = { 'createdTime': _t.current_time, 'last_update': _t.current_time, 'deleted': False } @classmethod def affiliate(cls): return cls.find_one(dict(name='Affiliate')) @classmethod def advertiser(cls): return cls.find_one(dict(name='Advertiser')) @classmethod def finance(cls): return cls.find_one(dict(name='Finance')) @classmethod def am(cls): return cls.find_one(dict(name='AM')) @classmethod def bd(cls): return cls.find_one(dict(name='BD')) @classmethod def pm(cls): return cls.find_one(dict(name='PM')) @classmethod def _create(cls, **kwargs): role = Role(gen_skel=True) role._id = _gid(GidKey.role_key) role.name = kwargs.get('name') role.permission = kwargs.get('permission') role.save() @classmethod def _delete(cls, _id): role = cls._get(_id) role.deleted = True role.save() @classmethod def _get(cls, _id): return cls.find_one(dict(_id=int(_id))) @classmethod def _update(cls, _id, **kwargs): role = cls._get(_id) role.name = kwargs.get('name', role.name) role.permission = kwargs.get('permission', role.permission) role.last_update = cls._t.current_time role.save()
class EmailModel(Doc): structure = dict(_id=int, model_name=str, content=str, createdTime=str, last_update=str, deleted=bool) _t = DateTime() indexes = [ { 'fields': ['_id'] }, { 'fields': ['model_name', 'deleted'] }, ] default_values = { 'createdTime': _t.current_time, 'last_update': _t.current_time, 'deleted': False } @classmethod def _create(cls, **kwargs): e_model = EmailModel( dict(_id=_gid(GidKey.e_model_key), model_name=kwargs.get('model_name'), content=kwargs.get('content')), True) e_model.save() @classmethod def _delete(cls, _id): e_model = cls._get(_id) e_model.deleted = True e_model.save() return True @classmethod def _get(cls, _id, fields=None): if not _id: return {} spec = dict(_id=int(_id)) if not fields: e_model = cls.find_one(spec) else: e_model = cls.find_one(spec, fields) return e_model @classmethod def _query(cls, spec, fields=None): return cls.find_one(spec) @classmethod def _update(cls, _id, model_name, content): e_model = cls._get(_id) e_model._id = int(_id) e_model.model_name = model_name if model_name else e_model.model_name e_model.content = content if content else e_model.content e_model.last_update = cls._t.current_time e_model.save()
class Invoice(Doc): structure = dict( _id=int, user_id=int, affiliate_id=int, time_range=dict(start=str, end=str), _invoice=[ dict(offer_id=int, actions=int, amount=float, real_pay=float, remark=str) ], _invoice_number=str, currency=int, createdTime=str, last_update=str, deleted=bool, status=bool, # 0: ) _t = DateTime() indexes = [ { 'fields': ['_id'] }, { 'fields': ['affiliate_id', 'deleted'] }, ] default_values = { 'createdTime': _t.today, 'last_update': _t.current_time, 'deleted': False, 'status': False } @classmethod def _create(cls, **kwargs): _id = _gid(GidKey.invoice_key) invoice = Invoice( dict(_id=_id, user_id=int(kwargs.get('user_id')), affiliate_id=int(kwargs.get('affiliate_id')), time_range=kwargs.get('time_range'), _invoice=kwargs.get('_invoices'), _invoice_number='newborn-{}'.format(_id), currency=kwargs.get('currency')), True) invoice.save() @classmethod def _delete(cls, _id): invoice = cls._get(_id) invoice.deleted = True invoice.save() @classmethod def _get(cls, _id): return cls.find_one(dict(_id=int(_id))) @classmethod def _query(cls, spec, limit=10, offset=0): return cls.find(spec, limit=limit, skip=offset) @classmethod def _update(cls, _id, **kwargs): invoice = cls._get(_id) invoice.status = True invoice._id = int(_id) invoice.save() @classmethod def _get_sum(cls, spec, limit=10, offset=0): datas = cls._query(spec, limit=limit, offset=offset) for invoice in datas: invoice._invoice = InterFace.get_sum_invoice(invoice._invoice) return datas
def pause(offer_id): offer = Offers.find_one(dict(_id=int(offer_id))) if offer: offer.status = '0' offer.last_update = DateTime().current_time offer.save()
class User(Doc): structure = dict(_id=int, account=str, email=list, password=str, skype_id=str, phone=str, role_id=int, parent_id=int, is_admin=bool, createdTime=str, last_update=str, last_login=str, deleted=bool) _t = DateTime() indexes = [ { 'fields': ['_id'] }, { 'fields': ['account', 'deleted'] }, ] default_values = { 'is_admin': False, 'createdTime': _t.current_time, 'last_update': _t.current_time, 'last_login': _t.current_time, 'deleted': False } @classmethod def _create(cls, **kw): password = kw.get('password', '') password = cls.encrypt_pwd(password) user = User( dict(_id=_gid(GidKey.user_key), email=kw.get('email'), password=password, account=kw.get('account'), role_id=int(kw.get('role_id')) if kw.get('role_id') else '', skype_id=kw.get('skype_id'), phone=kw.get('phone')), True) user.save() return user @classmethod def _delete(cls, _id): user = cls._get(_id) user.deleted = True user.save() return user @classmethod def _update(cls, _id, **kw): user = cls._get(_id) role_id = kw.get('role_id') user.role_id = int(role_id) if role_id else user.role_id user.account = kw.get('account') user.email = kw.get('email') password = kw.get('password') user.password = cls.encrypt_pwd( password) if user.password != password else password user.skype_id = kw.get('skype_id') user.last_update = cls._t.current_time user.save() return user @classmethod def _update_account(cls, _id, **kw): password = kw.get('password', '') password = cls.encrypt_pwd(password) user = cls._get(_id) user.account = kw.get('account') user.email = kw.get('email') user.password = password user.skype_id = kw.get('skype_id') user.phone = kw.get('phone') user.last_update = cls._t.current_time user.save() return True @classmethod def _get(cls, _id): return cls.find_one(dict(_id=int(_id))) @classmethod def verify(cls, email, password): _password = cls.encrypt_pwd(password) user = cls.find_one(dict(email=email, deleted=False)) if not user: raise UserNotFoundError() if _password != user.password: raise PasswordNotMatchError() return user @property def _role(self): if self.is_admin: return 'admin' role = Role.find_one(dict(_id=self.role_id)) return role.name @classmethod def encrypt_pwd(cls, password): m = hashlib.md5(SECRET) m.update(password) password = m.hexdigest() return password @classmethod def _query(cls): spec = dict(deleted=False, is_admin=False) return cls.find(spec) @classmethod def password_verify(cls, password): pattern = re.compile('^([a-zA-Z]+)([0-9]+)[0-9A-Za-z]$') match = pattern.findall(password) if not match: res = u'password must be start with string and include number!' elif len(password) < 6: res = u'password must be greater than 6!' else: res = u'Ok!' return res
class Affiliate(Doc): structure = dict( _id=int, user_id=int, country=str, status=str, # 0 deleted; 1 active; 2 pending; account_manager=int, company=str, create_time=str, last_update=str, payment={ 'invoice_frequency': int, 'threshold': float, 'payment_method': str, 'beneficiary': str, 'account_number': str, 'bank': str, 'route': str, 'paypal': str }) _t = DateTime() default_values = { 'payment': { 'invoice_frequency': '', 'threshold': '', 'payment_method': '', 'beneficiary': '', 'account_number': '', 'bank': '', 'route': '', 'paypal': '' } } @classmethod def _save(cls, **kw): aff = Affiliate( dict(_id=_gid(GidKey.affiliate_key), user_id=int(kw.get('user_id')), country=kw.get('country'), account_manager=int(kw.get('account_manager')) if kw.get('account_manager') else None, company=kw.get('company'), status=kw.get('status', '1'), create_time=cls._t.today, last_update=cls._t.today, payment=kw.get('payment')), True) aff.save() return aff @classmethod def _update(cls, aff_id, **kwargs): now = datetime.now() last_update = Tool.datetime_str(now, "%Y%m%d-%H:%M:%S") aff = cls.find_one(dict(_id=int(aff_id))) aff._id = int(aff_id) aff.country = kwargs['country'] if kwargs.get('account_manager'): aff.account_manager = int(kwargs['account_manager']) aff.company = kwargs['company'] aff.status = kwargs.get('status') aff.last_update = last_update aff.payment = kwargs.get('payment') aff.save()
def get(self): active_traffic_time = DateTime.get_day(6) offers = Offers.query(active_traffic_time) self.finish(dict(offers=offers))