def attention(): u = require_token() if u.name[:4] == 'tmp_': abort(403) s = request.form.get('switch') if s not in ['0', '1']: abort(422) pid = get_num(request.form.get('pid')) post = Post.query.get(pid) if not post: abort(404) at = Attention.query.filter_by(name_hash=hash_name(u.name), pid=pid).first() if not at: at = Attention(name_hash=hash_name(u.name), pid=pid, disabled=True) db.session.add(at) if(at.disabled != (s == '0')): at.disabled = (s == '0') post.likenum += 1 - 2 * int(s == '0'); db.session.commit() return { 'code': 0, 'likenum': post.likenum, 'attention': (s=='1') }
def do_comment(): u = require_token() pid = get_num(request.form.get('pid')) post = Post.query.get(pid) if not post: abort(404) if post.deleted: abort(451) content = request.form.get('text') content = content.strip() if content else None content = '[tmp]\n' + content if u.name[:4] == 'tmp_' else content if not content or len(content) > 4096: abort(422) c = Comment( name_hash = hash_name(u.name), content = content, ) post.comments.append(c) post.comment_timestamp = c.timestamp db.session.commit() return { 'code': 0, 'data': pid }
def do_post(): u = require_token() content = request.form.get('text') content = content.strip() if content else None content = '[tmp]\n' + content if u.name[:4] == 'tmp_' else content post_type = request.form.get('type') cw = request.form.get('cw') cw = cw.strip() if cw else None if not content or len(content) > 4096: abort(422) if cw and len(cw)>32: abort(422) p = Post( name_hash = hash_name(u.name), content = content, post_type = post_type, cw = cw or None, likenum = 1, comments = [] ) if post_type == 'text': pass elif post_type == 'image': # TODO p.file_url = 'foo bar' else: abort(422) db.session.add(p) db.session.commit() tags = re.findall('(^|\s)#([^#\s]{1,32})', content) #print(tags) for t in tags: tag = t[1] if not re.match('\d+', tag): db.session.add(TagRecord(tag=tag, pid=p.id)) db.session.add(Attention(name_hash=hash_name(u.name), pid=p.id)) db.session.commit() return { 'code': 0, 'date': p.id }
def delete(): u = require_token() obj_type = request.form.get('type') obj_id = get_num(request.form.get('id')) note = request.form.get('note') if note and len(note)>100: abort(422) obj = None if obj_type == 'pid': obj = Post.query.get(obj_id) elif obj_type == 'cid': obj = Comment.query.get(obj_id) if not obj: abort(404) if obj.name_hash == hash_name(u.name): if obj_type == 'pid': if len(obj.comments): abort(403) Attention.query.filter_by(pid=obj.id).delete() TagRecord.query.filter_by(pid=obj.id).delete() db.session.delete(obj) else: obj.deleted = True elif u.name in app.config.get('ADMINS'): obj.deleted = True db.session.add(Syslog( log_type='ADMIN DELETE', log_detail=f"{obj_type}={obj_id}\n{note}", name_hash=hash_name(u.name) )) if note.startswith('!ban'): db.session.add(Syslog( log_type='BANNED', log_detail=f"=> {obj_type}={obj_id}", name_hash=obj.name_hash )) else: abort(403) db.session.commit() return {'code': 0}
def __init__(self, config, hash_key=""): """ Initialize an EastwindPackage for install/dump config: path to config file """ self.config = EastwindSet(os.path.expanduser(config)) if hash_key == "": self.hash = utils.hash_name(self.config.name) else: self.hash = hash_key self.base_path = utils.app_path(os.path.join("package", self.hash, "")) self.config_manager = EastwindConfigManager(self.base_path) self.pkg_manager = EastwindPkgManager()
def extract(self, pkg_path): """ Extracting an EastwindPackage and turn it to a instance pkg_path: path to EastwindPackage, will expanded to user path """ self.pkg_name = os.path.basename(pkg_path) tar = tarfile.open(os.path.expanduser(pkg_path), "r:gz") hash_name = utils.hash_name(pkg_path) dest_dir = utils.app_path(os.path.join("package", hash_name)) tar.extractall(dest_dir) tar.close() config_file = os.path.join(dest_dir, "control") return EastwindPackage(config_file, hash_name)
def report(): u = require_token() pid = get_num(request.form.get('pid')) reason = request.form.get('reason', '') db.session.add(Syslog( log_type='REPORT', log_detail=f"pid={pid}\n{reason}", name_hash=hash_name(u.name) )) db.session.commit() return {'code': 0}
def get_attention(): u = require_token() ats = Attention.query.filter_by(name_hash=hash_name(u.name), disabled=False) posts = [Post.query.get(at.pid) for at in ats.all()] data = [ map_post(post, u.name, 10) for post in posts[::-1] if post and not post.deleted ] return { 'code': 0, 'count': len(data), 'data': data }
def edit_cw(): u = require_token() cw = request.form.get('cw') pid = get_num(request.form.get('pid')) cw = cw.strip() if cw else None if cw and len(cw)>32: abort(422) post = Post.query.get(pid) if not post: abort(404) if post.deleted: abort(451) if not (u.name in app.config.get('ADMINS') or hash_name(u.name) == post.name_hash): abort(403) post.cw = cw; db.session.commit() return {'code': 0}