def vercode_del(file_url_obj=None, expiration_time=None): ''' 删验证码 :param url: :param expiration_time:顺便删除过期的验证码 :return: ''' if file_url_obj: file_del(file_url_obj=file_url_obj) mdb_web.db.verify_code.delete_many({ 'url.key': file_url_obj["key"], "type": "image" }) if expiration_time: codes = mdb_web.db.verify_code.find({ 'time': { "$lt": time.time() - expiration_time }, "type": "image" }) for code in codes: file_del(code["url"]) mdb_web.db.verify_code.delete_one({'_id': code["_id"]})
def del_media(user_id=None): """ :param user_id: 媒体为管理端(系统)所有时需要传入user_id = 0 :return: """ if user_id is None: user_id = current_user.str_id media_ids = json_to_pyseq(request.argget.all("ids", [])) deleted_count = 0 for tid in media_ids: media = mdbs["web"].db.media.find_one( {"_id": ObjectId(tid), "user_id": user_id}) r = mdbs["web"].db.media.delete_one( {"_id": ObjectId(tid), "user_id": user_id}) # 是否存在上传的文件 if r.deleted_count and "url" in media and media["url"]: file_del(media["url"]) deleted_count += 1 else: deleted_count += 1 if deleted_count: data = {"msg": gettext("{} files have been deleted").format( deleted_count), "msg_type": "s", "custom_status": 204} else: data = { "msg": gettext("Failed to delete"), "msg_type": "w", "custom_status": 400} return data
def delete_sys_message(): """ 删除已发送邮件 :return: """ ids = json_to_pyseq(request.argget.all("ids", [])) for i, tid in enumerate(ids): ids[i] = ObjectId(tid) q = {"_id": {"$in": ids}} # 查找出要删除中的邮件消息 q2 = deepcopy(q) q2["type"] = "email" msgs = mdbs["sys"].db.sys_message.find(q2) rm_imgs = [] # 要删除的邮件消息图片 for msg in msgs: srcs = richtext_extract_img(richtext=msg["html"]) rm_imgs.extend(srcs) for rm_img in rm_imgs: # 获取图片url中唯一部分 key = os.path.split(rm_img) if len(key) > 0: key = key[-1] else: continue # 查找出图片对应的key img_obj = mdbs["sys"].db.sys_msg_img.find_one( {"imgs.key": { "$regex": key }}, regular_escape=False) if img_obj: for img in img_obj["imgs"]: # 删除 file_del(img) mdbs["sys"].db.sys_msg_img.delete_one({"_id": img_obj["_id"]}) r = mdbs["sys"].db.sys_message.delete_many(q) if r.deleted_count: data = { "msg": gettext("Successfully deleted"), "msg_type": "s", "custom_status": 204 } else: data = { "msg": gettext("Failed to delete"), "msg_type": "w", "custom_status": 400 } return data
def avatar_upload(): ''' 头像上传 :return: ''' result = None imgfile_base = request.argget.all("imgfile_base") if imgfile_base: result = fileup_base_64(uploaded_files = [imgfile_base], prefix="user_avatar/") else: file = request.files['imgfile'] if file: tailoring = request.argget.all('tailoring') if tailoring: if not isinstance(tailoring, dict): tailoring = json.loads(tailoring) for k in ["width", "height", "x", "y", "rotate"]: tailoring.setdefault(k, 0) result = file_up(uploaded_files=[file], prefix="user_avatar/", tailoring=tailoring) data = {} if result: result = result[0] user = mdb_user.db.user.find_one({"_id":current_user.id}) if user: if result["key"] != user['avatar_url']["key"]: # 当使用了不同的名字删除老的头像 file_del(user['avatar_url']) update_data = { "avatar_url" : result } r = mdb_user.db.user.update_one({"_id":current_user.id}, {"$set":update_data}) if not r.matched_count: data = {'msg':gettext("Save failed"), 'msg_type':"w", "http_status":400} else: if result["type"] == "local": # 如果保存再本地的话, 保存为一定尺寸大小 path = "{}{}".format(APPS_PATH, get_file_url(result)) imgcp = ImageCompression(path, path) ava_size = get_config("account", "USER_AVATAR_SIZE") imgcp.custom_pixels(ava_size[0], ava_size[1]) data = {'msg':gettext("Save successfully"), 'msg_type':"s", "http_status":201} if not data: data = {'msg':gettext("Upload failed"), 'msg_type':"w", "http_status":400} return data
def del_display_setting(): """ :return: """ display_setting_ids = json_to_pyseq(request.argget.all("ids", [])) deleted_count = 0 theme_name = get_config("theme", "CURRENT_THEME_NAME") for tid in display_setting_ids: display_setting = mdbs["sys"].db.theme_display_setting.find_one({ "_id": ObjectId(tid), "theme_name": theme_name }) r = mdbs["sys"].db.theme_display_setting.delete_one({ "_id": ObjectId(tid), "theme_name": theme_name }) # 是否存在上传的文件 if r.deleted_count and "url" in display_setting and display_setting[ "url"]: file_del(display_setting["url"]) deleted_count += 1 else: deleted_count += 1 if deleted_count: data = { "msg": gettext("{} files have been deleted").format(deleted_count), "msg_type": "s", "custom_status": 204 } else: data = { "msg": gettext("Failed to delete"), "msg_type": "w", "custom_status": 400 } return data
def delete_post(ids=[]): """ 完全删除一篇文章 :return: """ posts = mdbs["web"].db.post.find( {"_id": {"$in": ids}, "is_delete": {"$in": [2, 3]}}, {"imgs": 1}) rm_pids = [] if posts.count(True): for post in posts: # 删用户的post喜欢标记 mdbs["user"].db.user_like.update_many({"type": "post", "values": str(post["_id"])}, {"$pull": {"values": str(post["_id"])}}) # 删图片 for img in post["imgs"]: file_del(img) if "cover_url" in post: file_del(post["cover_url"]) # 删post pv mdbs["web"].db.access_record.delete_many({"post_id": post["_id"]}) # 删除评论 comments = mdbs["web"].db.comment.find( {"_target_id": post["_id"]}, {"_id": 1}) mdbs["web"].db.comment.delete_many({"_target_id": post["_id"]}) # 删用户的评论喜欢标记 for comment in comments: mdbs["user"].db.user_like.update_many({"type": "comment", "values": str( comment["_id"])}, {"$pull": {"values": str(comment["_id"])}}) rm_pids.append(post["_id"]) r = mdbs["web"].db.post.delete_many( {"_id": {"$in": rm_pids}, "is_delete": {"$in": [2, 3]}}) if r.deleted_count: data = {"msg": gettext("Removed from the database, {}").format( r.deleted_count), "msg_type": "s", "custom_status": 204} else: data = {"msg": gettext("No match to relevant data"), "msg_type": "w", "custom_status": 400} return data
def clean_tempfile(user_id, type, old_file=[], keey_file=[]): """ 清理数据库中tempfile文档中保存的临时文件 :param user_id: 用户ID :param type: 临时文件类型 :param old_file: 已存在, 但不知是否在保留文件中的文件 :param keey_file: 当前临时文件中需要保留的文件 :return: """ tempfiles = mdbs["web"].db.tempfile.find({ "type": type, "user_id": user_id }) files = [] # 找出被上传后没有使用的图片: temppaths = [] for tempfile in tempfiles: temppaths.extend(tempfile["paths"]) temppaths.extend(old_file) need_rm_files = temppaths[:] for img in temppaths: path = get_file_url(img) if not path: continue for src in keey_file: if not src: continue if path.lower() in src.lower(): files.append(img) need_rm_files.remove(img) break # 文章中没有的图片就删除 for rf in need_rm_files: file_del(rf) # 更tempfile数据库的的数据 mdbs["web"].db.tempfile.delete_many({"type": type, "user_id": user_id}) return files
def del_display_setting(): ''' :return: ''' user_id = 0 display_setting_ids = json_to_pyseq(request.argget.all("ids", [])) deleted_count = 0 for id in display_setting_ids: display_setting = mdb_sys.db.theme_display_setting.find_one({ "_id": ObjectId(id), "user_id": user_id }) r = mdb_sys.db.theme_display_setting.delete_one({ "_id": ObjectId(id), "user_id": user_id }) # 是否存在上传的文件 if r.deleted_count and "url" in display_setting and display_setting[ "url"]: file_del(display_setting["url"]) deleted_count += 1 else: deleted_count += 1 if deleted_count: data = { "msg": gettext("{} files have been deleted").format(deleted_count), "msg_type": "s", "http_status": 204 } else: data = { "msg": gettext("Failed to delete"), "msg_type": "w", "http_status": 400 } return data
def del_display_setting(): """ :return: """ display_setting_ids = json_to_pyseq(request.argget.all("ids", [])) theme_name = request.argget.all("theme_name") s, r = arg_verify([(gettext("theme name"), theme_name)], required=True) if not s: return r deleted_count = 0 for tid in display_setting_ids: display_setting = mdbs["sys"].db.theme_display_setting.find_one( {"_id": ObjectId(tid), "theme_name": theme_name }) r = mdbs["sys"].db.theme_display_setting.delete_one( {"_id": ObjectId(tid), "theme_name": theme_name }) # 是否存在上传的文件 if r.deleted_count and "url" in display_setting and display_setting["url"]: file_del(display_setting["url"]) deleted_count += 1 else: deleted_count += 1 if deleted_count: data = {"msg": gettext("{} files have been deleted").format( deleted_count), "msg_type": "s", "custom_status": 204} else: data = { "msg": gettext("Failed to delete"), "msg_type": "w", "custom_status": 400} return data
def edit_display_setting(): """ :return: """ display_setting_id = request.argget.all("id") name = request.argget.all("name") link = request.argget.all("link") link_name = request.argget.all("link_name") link_open_new_tab = str_to_num(request.argget.all("link_open_new_tab", 1)) title = request.argget.all("title") text = request.argget.all("text", "") text_html = request.argget.all("text_html", "") code = request.argget.all("code", "") code_type = request.argget.all("code_type") switch = request.argget.all("switch") category_id = request.argget.all("category_id") s, r = arg_verify([("id", display_setting_id)], required=True) if not s: return r s, r = arg_verify([(gettext("name"), name)], required=True) if not s: return r if code_type == "json": code = json_to_pyseq(code) theme_name = get_config("theme", "CURRENT_THEME_NAME") old_display_setting = mdbs["sys"].db.theme_display_setting.find_one({ "_id": ObjectId(display_setting_id), "theme_name": theme_name }) # 如果只是更新图片, 则保存上传图片 if request.files: data = file_upload(return_key=True, prefix="multidisplay_setting/{}/".format( old_display_setting["type"])) if data["msg_type"] != "s": return data else: # 删除旧的图片 if "url" in old_display_setting: file_del(old_display_setting["url"]) temp_url = None if "keys" in data: for key in data["keys"]: temp_url = key if temp_url: mdbs["sys"].db.theme_display_setting.update_one( { "_id": ObjectId(display_setting_id), "theme_name": theme_name }, {"$set": { "url": temp_url }}) data = { "msg": gettext("Update picture successfully"), "msg_type": "s", "custom_status": 201 } else: data = { "msg": gettext("Failed to update"), "msg_type": "e", "custom_status": 400 } return data category = "Default" not_updated_category = False if category_id is None: # 不更新category not_updated_category = True elif category_id and category_id.lower() != "default": display_setting_category = mdbs["web"].db.category.find_one( {"_id": ObjectId(category_id)}) if display_setting_category: category = display_setting_category["name"] elif category_id.lower() == "default": category_id = "" # 处理其他字段更新 query = { "name": name, "type": old_display_setting["type"], "_id": { "$ne": ObjectId(display_setting_id) }, "theme_name": theme_name } if mdbs["sys"].db.theme_display_setting.find_one(query): type_alias = old_display_setting["type"] for k, v in get_config("category", "CATEGORY_TYPE").items(): if v == old_display_setting["type"]: type_alias = k break data = { "msg": gettext('The type "{}" exists in the name "{}"').format( type_alias, name), "msg_type": "w", "custom_status": 403 } else: # 获取text_html使用的图片 old_imgs = old_display_setting.get("text_imgs", []) if text_html: srcs = richtext_extract_img(richtext=text_html) else: srcs = [] text_imgs = clean_tempfile(user_id=current_user.str_id, type="image", old_file=old_imgs, keey_file=srcs) info = { "name": name, "link": link, "link_name": link_name, "link_open_new_tab": link_open_new_tab, "title": title, "text": text, "text_html": text_html, "code": code, "code_type": code_type, "text_imgs": text_imgs, } if switch is not None and switch.isdigit(): info["switch"] = int(switch) if not not_updated_category: info["category_id"] = category_id info["category"] = category r = mdbs["sys"].db.theme_display_setting.update_one( { "_id": ObjectId(display_setting_id), "theme_name": theme_name }, {"$set": info}) if r.modified_count: data = { "msg": gettext("Modify the success"), "msg_type": "s", "custom_status": 201 } else: data = { "msg": gettext("The content is not modified"), "msg_type": "w", "custom_status": 400 } return data
def edit_media(user_id=None): """ :param user_id: 媒体为管理端(系统)所有时需要传入user_id = 0 :return: """ if user_id is None: user_id = current_user.str_id media_id = request.argget.all("id") name = request.argget.all("name") link = request.argget.all("link") link_name = request.argget.all("link_name") link_open_new_tab = str_to_num(request.argget.all("link_open_new_tab", 1)) title = request.argget.all("title") text = request.argget.all("text", "") text_html = request.argget.all("text_html", "") category_id = request.argget.all("category_id") s, r = arg_verify([("id", media_id)], required=True) if not s: return r s, r = arg_verify([(gettext("name"), name)], required=True) if not s: return r old_media = mdb_web.db.media.find_one({"_id": ObjectId(media_id)}) # 如果只是更新图片, 则保存上传图片 if request.files: data = file_upload(return_key=True, prefix="multimedia/{}/".format(old_media["type"])) if data["msg_type"] != "s": return data else: # 删除旧的图片 file_del(old_media["url"]) temp_url = None if "keys" in data: for key in data["keys"]: temp_url = key if temp_url: mdb_web.db.media.update_one( { "_id": ObjectId(media_id), "user_id": user_id }, {"$set": { "url": temp_url }}) data = { "msg": gettext("Update picture successfully"), "msg_type": "s", "http_status": 201 } else: data = { "msg": gettext("Failed to update"), "msg_type": "e", "http_status": 400 } return data category = "Default" not_updated_category = False if category_id is None: # 不更新category not_updated_category = True elif category_id and category_id.lower() != "default": media_category = mdb_web.db.category.find_one( {"_id": ObjectId(category_id)}) if media_category: category = media_category["name"] elif category_id.lower() == "default": category_id = "" # 处理其他字段更新 query = { "name": name, "type": old_media["type"], "_id": { "$ne": ObjectId(media_id) } } if mdb_web.db.media.find_one(query): type_alias = old_media["type"] for k, v in get_config("category", "CATEGORY_TYPE").items(): if v == old_media["type"]: type_alias = k break data = { "msg": gettext('The type "{}" exists in the name "{}"').format( type_alias, name), "msg_type": "w", "http_status": 403 } else: # 获取text_html使用的图片 old_imgs = old_media.get("text_imgs", []) if text_html: srcs = richtext_extract_img(richtext=text_html) else: srcs = [] text_imgs = clean_tempfile(user_id=current_user.str_id, type="image", old_file=old_imgs, keey_file=srcs) info = { "name": name, "link": link, "link_name": link_name, "link_open_new_tab": link_open_new_tab, "title": title, "text": text, "text_html": text_html, "text_imgs": text_imgs } if not not_updated_category: info["category_id"] = category_id info["category"] = category r = mdb_web.db.media.update_one( { "_id": ObjectId(media_id), "user_id": user_id }, {"$set": info}) if r.modified_count: data = { "msg": gettext("Modify the success"), "msg_type": "s", "http_status": 201 } else: data = { "msg": gettext("The content is not modified"), "msg_type": "w", "http_status": 400 } return data
def avatar_upload(): """ 头像上传 :return: """ result = None imgfile_base = request.argget.all("imgfile_base") max_size_mb = get_config("account", "USER_AVATAR_MAX_SIZE") max_size_b = max_size_mb * 1024 * 1024 if imgfile_base: if len(imgfile_base) > max_size_b: data = { "msg": gettext("Upload avatar image can not exceed {}M".format( max_size_mb)), "msg_type": "w", "http_status": 413 } return data else: result = fileup_base_64(uploaded_files=[imgfile_base], prefix="user_avatar/") else: file = request.files['upfile'] if len(file.read()) > max_size_b: data = { "msg": gettext("Upload avatar image can not exceed {}M".format( max_size_mb)), "msg_type": "w", "http_status": 413 } return data if file: tailoring = request.argget.all('tailoring') if tailoring: if not isinstance(tailoring, dict): tailoring = json.loads(tailoring) for k in ["width", "height", "x", "y", "rotate"]: tailoring.setdefault(k, 0) result = file_up(uploaded_files=[file], prefix="user_avatar/", tailoring=tailoring) data = {} if result: result = result[0] user = get_one_user(user_id=current_user.str_id) if user: if user['avatar_url'] and "key" in user['avatar_url'] \ and result["key"] != user['avatar_url']["key"]: # 当使用了不同的名字删除老的头像 file_del(user['avatar_url']) update_data = {"avatar_url": result} r = update_one_user(user_id=current_user.str_id, updata={"$set": update_data}) if not r.matched_count: data = { 'msg': gettext("Save failed"), 'msg_type': "w", "http_status": 400 } else: if result["type"] == "local": # 如果保存再本地的话, 保存为一定尺寸大小 path = "{}{}".format(APPS_PATH, get_file_url(result)) imgcp = ImageCompression(path, path) ava_size = get_config("account", "USER_AVATAR_SIZE") imgcp.custom_pixels(ava_size[0], ava_size[1]) data = { 'msg': gettext("Save successfully"), 'msg_type': "s", "http_status": 201 } if not data: data = { 'msg': gettext("Upload failed"), 'msg_type': "w", "http_status": 400 } # 清理user信息数据缓存 delete_user_info_cache(user_id=current_user.str_id) return data