def save_by_name(admin: dict): """ Save a new admin with field name as index :return: """ name = admin.get('name') if not name: raise Error(10003, 'Field name required!') admin['updated_at'] = china_tz(datetime.now()) admin['created_at'] = china_tz(datetime.now()) if '_id' in admin: del admin['_id'] entity = db.find_one({'name': name}) if entity: admin['created_at'] = entity['created_at'] if admin.get('new_password'): admin['password'] = sha256_pwd(admin['new_password'], entity['salt']) del admin['new_password'] else: admin['password'] = entity.get('password') admin['salt'] = entity['salt'] db.update_one({'_id': entity['_id']}, {"$set": admin}) else: admin['salt'] = random_alphanumeric(16) if not admin.get('new_password'): raise Error(1003, msg='new_password required!') admin['password'] = sha256_pwd(admin['new_password'], admin['salt']) del admin['new_password'] db.insert_one(admin) return find_by_name(name)
def save_by_title(history: dict): """ Save a new history with field title as index :return: """ title = history.get('title') if not title: raise Error(10003, 'Field title required!') history['updated_at'] = china_tz(datetime.now()) history['created_at'] = china_tz(datetime.now()) if '_id' in history: _id = ObjectId(history['_id']) del history['_id'] del history['created_at'] db.update_one({'_id': _id}, { "$set": history}) return find_by_title(title) entity = db.find_one({'title': title}) if entity: history['created_at'] = entity['created_at'] db.update_one({'_id': entity['_id']}, { "$set": history}) else: db.insert_one(history) return find_by_title(title)
def logout(token: str): admin = redis.get(AUTH_TOKEN_PREFIX + token) if admin: admin = parse_json(admin) db.update_one( {'_id': admin.get('_id')}, {'$set': dict(token='', updated_at=china_tz(datetime.now()))}) redis.delete(AUTH_TOKEN_PREFIX + token)
def add(word: dict): """ Add a new word :Returns: - Error with code 10003, if name not found in parameter. - Error with code 10200, if a record is found in db with duplicate name. """ # Validation name = word.get('name') if not name: raise Error(10003, 'Field name required!') w = word_collection.find_one({'name': name}) if w: raise Error(10200, 'Word exit with duplicate name: %s' % name) # Insert data to db word['updated_at'] = china_tz(datetime.now()) word['created_at'] = china_tz(datetime.now()) word_collection.insert_one(word)
def update_token(entity): redis.delete(AUTH_TOKEN_PREFIX + entity.get('token', '')) db.update_one({'_id': entity.get('_id')}, { '$set': dict(token=random_alphanumeric(64), login_time=china_tz(datetime.now())) }) entity = db.find_one({'_id': entity.get('_id')}, __projection) redis.setex(AUTH_TOKEN_PREFIX + entity.get('token'), 3600 * 24 * 7, to_json(entity)) return entity
def test_datetime(self): log.info('Test datetime start...') # log.info('tzs: %s', pytz.all_timezones) # Asia/Tokyo tokyo = pytz.timezone('Asia/Tokyo') cn = pytz.timezone('Asia/Shanghai') n = datetime.now() log.info('tz: %s', tokyo) log.info('tzinfo: %s', n.tzinfo) log.info('tzinfo.astimezone: %s', n.astimezone(tokyo).tzinfo) log.info('astimezone.tokyo: %s', n.astimezone(tokyo)) log.info('astimezone.cn: %s', n.astimezone(cn)) log.info('localize.tokyo: %s', tokyo.localize(n, is_dst=None)) log.info('localize.cn: %s', cn.localize(n, is_dst=None)) log.info('china_tz: %s', china_tz(n)) log.info('tzinfo.astimezone astimezone astimezone astimezone astimezone astimezone astimezone astimezone: %s', n.astimezone(tokyo).tzinfo)
def save(word: dict): """ Save a new word with field name as index :return: """ name = word.get('name') if not name: raise Error(10001, 'Field name required!') word['created_at'] = china_tz(datetime.now()) w = word_collection.find_one({'name': name}) if w: if '_id' in word: del word['_id'] word['created_at'] = w['created_at'] word_collection.update_one({'_id': w['_id']}, {"$set": word}) else: word_collection.insert_one(word) return word
def test_import(self): word_coll = self.db.Word log.info('word_coll: %s', word_coll) file = join(dirname(realpath(__file__)), 'seeds', 'data_word.txt') with open(file, 'r', encoding='utf-8') as f: for i, line in enumerate(f): if re.search(r'^\w+', line): # 单词行 ret_words = self.handle_line( i + 1, self.upgrade_poor_format(line)) if ret_words: for w in ret_words: w_db = word_coll.find_one({'name': w.get('name')}) if not w_db: w['created_at'] = china_tz(datetime.now()) # Remove fields with None value by constructing a new dict only having field with concrete value. word_coll.insert_one( {k: v for k, v in w.items() if v})
def update(_id: str, updated_time_on: bool, word: dict): """ Save a new word with field name as index :return: - Error with code 10003, if name not found in parameter. """ name = word.get('name') if not name: raise Error(10003, 'Field name required!') w = word_collection.find_one({'_id': ObjectId(_id)}) if not w: raise Error(10201, 'Word not found with _id; %s' % _id) word['created_at'] = w['created_at'] if updated_time_on: word['updated_at'] = china_tz(datetime.now()) else: word['updated_at'] = w['updated_at'] del word['_id'] word_collection.update_one({'_id': w['_id']}, { "$set": word}) return word