def save(cls, user_name, id, title, content): summary = QiniuService.get_summary(content) if title is None: title = 'untitled' if id is None: file_name = md5(title + content + datetime.now().timestamp().__str__()) url = QiniuService.upload_doc(content, file_name) catalogue_index = cls.get_max_catalogue_index(user_name, 1) article = Article(title=title, file_key=file_name, user_name=user_name, url=url, summary=summary, catalogue_id=1, catalogue_index=catalogue_index) article.insert() return Article.select().filter( Article.user_name == user_name, Article.file_key == file_name).one().id article = Article.select().get(id) if user_name != article.user_name: raise ServerException(msg=f'您没有权限修改{article.user_name}的文章') file_name = md5(title + content) if article.file_key == file_name: return id url = QiniuService.upload_doc(content, article.file_key, file_name) article.url = url article.file_key = file_name article.summary = summary Article.update(article) return id
def save_graph(cls, user_name, id, type, title, data, img): if data: data = json.dumps(data) if title is None: title = 'Untitled' if id is not None: graph: Graph = Graph.select().get(id) graph.type = type graph.title = title data_key = md5(title + data) if graph.data_key != data_key: data_url = QiniuService.upload_doc(data, graph.data_key, data_key) graph.data_key = data_key graph.data_url = data_url img_key = md5(img) if graph.img_key != img_key: img_url = QiniuService.upload_img(img, graph.img_key, img_key) graph.img_key = img_key graph.img_url = img_url img_info = QiniuService.get_img_info(img_url) graph.width = img_info.get('width') graph.height = img_info.get('height') graph.size = img_info.get('size') graph.format = img_info.get('format') graph.color_model = img_info.get('colorModel') Graph.update(graph) return id else: data_key = md5(title + data + datetime.now().timestamp().__str__()) data_url = QiniuService.upload_doc(data, file_name=data_key) img_key = md5(img + datetime.now().timestamp().__str__()) img_url = QiniuService.upload_img(img, file_name=img_key) img_info = QiniuService.get_img_info(img_url) graph = Graph(user_name=user_name, title=title, data_key=data_key, data_url=data_url, type=type, img_key=img_key, img_url=img_url, width=img_info.get('width'), height=img_info.get('height'), size=img_info.get('size'), format=img_info.get('format'), color_model=img_info.get('colorModel')) graph.insert() return Graph.select().filter(Graph.data_key == data_key).one().id
def login(cls, name: str, password: str) -> bool: password = md5(password) try: user = User.select().filter(User.name == name, User.password == password).one() except NoResultFound: return False session['user'] = json.dumps(user.get_json()) return user is not None
def upload_image(cls, user_name, album_id, file): img = file.read() file_name = md5(img.decode('ISO-8859-1')) image = Image.select().filter(Image.user_name == user_name, Image.key == file_name).first() if image is not None: raise ServerException('图片已存在') url = QiniuService.upload_img(img, file_name=file_name) img_info = QiniuService.get_img_info(url) assert img_info is not None image = Image( album_id=album_id, user_name=user_name, key=file_name, url=url, width=img_info.get('width'), height=img_info.get('height'), size=img_info.get('size'), format=img_info.get('format'), color_model=img_info.get('colorModel') ) image.insert() return Image.select().filter(Image.key == file_name).one()
def register(cls, user: User) -> bool: if user.password is not None: user.password = md5(user.password) User.insert(user) return True