def clear_expired(self): return self.collection.remove({ '$or': [ {'end_time': {'$lt': now(), '$ne': 0}}, {'end_time': 0, 'updated': {'$lt': now() - 3600 * 24 * 90}} ] })
def add_new_task_state(target, task_id): try: db.insert(''' INSERT INTO `system_domain` (`domain`, `domain_whois`, `ns_records`, `mx_records`) VALUES (%s, "", "", "") ON DUPLICATE KEY UPDATE `domain` = VALUES(`domain`) ''',target) db.insert(''' INSERT INTO `system_task_records` (`id`, `keywords`, `create_time`, `finish_time`, `state`) VALUES (%s, %s, %s, %s, %s)''', task_id, target, misc.now(), misc.original_time(), '0') except Exception as e: logging.error('添加任务状态失败') logging.error(traceback.format_exc()) logging.error('Caught an error when insert a new task state: %s' % str(e))
def search(self, keywords, cids=None, timestamp=0, sort_type=None): if isinstance(keywords, list): key_list = [remove_multi_space(safe_regex_str(kw)) for kw in keywords[:6] if kw and isinstance(kw, basestring)] # max 6 elif isinstance(keywords, basestring): key_list = [remove_multi_space(safe_regex_str(keywords))] else: key_list = [] _query = { '$and': [ {'title': re.compile(ur'.*{}.*'.format(key), re.IGNORECASE)} for key in key_list ], 'end_time': {'$gt': now()} } if cids is not None: _query['cid'] = { '$in': [cid for cid in cids if isinstance(cid, unicode)] } if timestamp: _query['updated'] = { '$lt': int(timestamp) } _sorts = self._make_sorts(sort_type) return self.find(_query).sort(_sorts).limit(self.MAX_QUERY)
def remove(self): self['login'] = u'.{}.{}'.format(self['login'], self['_id']) self['slug'] = u'.{}.{}'.format(self['slug'], self['_id']) self['updated'] = now() self['deleted'] = 1 self.save() return self['_id']
def media_safe_src(pic_url, timestamp=None): if not pic_url: return pic_url if not timestamp: timestamp = now() try: pair = '?' if '?' not in pic_url else '&' return u'{}{}t={}'.format(pic_url, pair, timestamp) except Exception as e: return str(e)
def find_404_content_file(): return { '_id': '.id-error-404', 'app_id': g.curr_app['_id'], 'slug': current_app.config.get('DEFAULT_404_SLUG'), 'template': current_app.config.get('DEFAULT_404_SLUG'), 'content_type': '.h', 'meta': {}, 'parent': u'', 'priority': 0, 'date': u'', 'content': u'', 'excerpt': u'', 'tags': [], 'terms': [], 'updated': now(), 'creation': now(), 'status': 1, }
def media_safe_splash(pic_url, timestamp=None): if not pic_url or not pic_url.startswith(current_app.config['RES_URL']): return pic_url if not timestamp: timestamp = now() try: pair = '?' if '?' not in pic_url else '&' style = 'imageView2/1/w/215/h/168/q/90' return u'{}{}t={}&{}'.format(pic_url, pair, timestamp, style) except Exception as e: return str(e)
def logout(): token = get_jwt_token() if token: prefix = current_app.config.get('INVALID_USER_TOKEN_PREFIX') expire = current_app.config.get('JWT_EXPIRATION_DELTA') current_app.redis.setex('{}{}'.format(prefix, token), True, expire) return { 'token': None, 'updated': now(), }
def find_by_activity(self, activity_key, timestamp=0): _query = { 'activity': activity_key, 'end_time': {'$gt': now()} } if timestamp: _query['updated'] = { '$lt': int(timestamp) } _sorts = [('updated', INDEX_DESC)] return self.find(_query).sort(_sorts).limit(self.MAX_QUERY)
def add_new_task_state(task_id, target_ip): try: logging.info('task id: {}'.format(task_id)) db.insert( ''' INSERT INTO `system_task_records` (`id`, `keywords`, `create_time`, `finish_time`, `state`) VALUES (%s, %s, %s, %s, %s)''', task_id, target_ip, misc.now(), misc.original_time(), '0') except Exception as e: logging.error('添加任务状态失败') logging.error(traceback.format_exc()) logging.error('Caught an error when insert a new task state: %s' % str(e))
def find_live(self, cids=None, timestamp=0, sort_type=None): _query = { 'end_time': {'$gt': now()}, 'coupon_id': {'$ne': u''} } if cids is not None: _query['cid'] = { '$in': [cid for cid in cids if isinstance(cid, unicode)] } if timestamp: _query['updated'] = { '$lt': int(timestamp) } _sorts = self._make_sorts(sort_type) return self.find(_query).sort(_sorts).limit(self.MAX_QUERY)
def create_task(self, task_params): """创建任务, 并推入队列""" new_task_entity = TaskRecords(keywords=task_params['keywords'], create_time=misc.now(), finish_time=misc.original_time()) self.db.add(new_task_entity) self.db.commit() new_task_entity = self.db.query(TaskRecords)\ .filter(TaskRecords.keywords == task_params['keywords'])\ .order_by(TaskRecords.id.desc())\ .limit(1)\ .one() new_task_id = new_task_entity.id self.push_task(new_task_id, task_params) self.db.close()
def post(self, *args, **kwargs): result_json = {'success': 0} current_user = self.get_secure_cookie(pre_system + 'username') if current_user != 'root': logging.error('not root') self.finish(result_json) return username = self.get_argument('username', None) password = self.get_argument('password', None) comment = self.get_argument('comment', '') group_id = 2 if not(username and password): self.finish(result_json) return sql = ''' INSERT INTO `system_admin_user` (`group_id`, `username`, `password`, `create_time`, `lastlogin_time`, `comment`) VALUES (%s, %s, md5(%s), %s, %s, %s); ''' try: current_time = now() last_login_time = original_time() effected_row = db.update(sql, group_id, username, password, current_time, last_login_time, comment) result_json['success'] = 1 user_data = db.query('SELECT `id`, `group_id`, `username`, `create_time`, `lastlogin_time`, `login_ip`, `comment` FROM `system_admin_user` WHERE `username` = %s', username)[0] user_data['create_time'] = user_data['create_time'].strftime('%Y-%m-%d %H:%M:%S') user_data['lastlogin_time'] = user_data['lastlogin_time'].strftime('%Y-%m-%d %H:%M:%S') result_json['data'] = user_data except Exception as e: logging.error(traceback.format_exc()) logging.error(e.message) self.write(result_json)
def timestamp(self): return unicode(now())
def filter_timestamp(url, updated=None, key=u't'): if updated is None: updated = now() return add_url_params(url, {key: updated}, unique=True)
def upload(): f = request.files['file'] activity = request.form.get('activity', u'') ext = split_file_ext(f.filename) if ext == 'json': items_list = json.loads(f.stream.read()) elif ext == 'zip': items_list = _unpack_items(f) else: raise Exception('must be json or zip') new_count = 0 update_count = 0 for item in items_list: item_id = unicode(item['item_id']) start_time = to_timestamp(item['coupon_start_time']) end_time = to_timestamp(item['coupon_end_time']) if now() >= (end_time + 3600 * 6): continue commodity = current_app.mongodb.\ Commodity.find_one_by_itemid(item_id) if not commodity: commodity = current_app.mongodb.Commodity() commodity['item_id'] = item_id new_count += 1 else: update_count += 1 commodity['cid'] = unicode(item['cid']) commodity['shop_type'] = item['shop_type'] commodity['shop_title'] = item['shop_title'] commodity['title'] = item['title'] commodity['src'] = item['pic_url'] if item['volume']: # incase volume not provided (with 0). commodity['volume'] = item['volume'] commodity['activity'] = process_slug(activity, False) commodity['price'] = parse_int(item['price'] * 100) commodity['income_rate'] = parse_int(item['income_rate'] * 100) commodity['commission'] = parse_int(item['commission'] * 100) if item['coupon_id']: # incase coupon_id is missing with activity. commodity['coupon_id'] = item['coupon_id'] commodity['coupon_info'] = item['coupon_info'] commodity['category'] = item['category'] commodity['start_time'] = start_time commodity['end_time'] = end_time commodity['click_url'] = item['click_url'] commodity['coupon_url'] = item['coupon_url'] commodity['coupon_click_url'] = item['coupon_click_url'] commodity['memo'] = item['memo'] commodity.save() flash('{} Commodities added, {} updated.'.format(new_count, update_count)) return_url = url_for('.index', last=f.filename) return redirect(return_url)