def unassign_achievement(): user = get_user_from_request() if not user.is_admin: return errors.no_access() json = request.get_json() if "users" not in json or "achievement" not in json: return errors.wrong_payload("users", "achievement") if len(json["users"]) == 0: return errors.wrong_payload("users") achievement = Achievement.get_or_none( Achievement.id == json["achievement"]) if achievement is None: return errors.wrong_payload("achievement") assign_errors = [] for u in json["users"]: user_to_unassign = User.get_or_none(User.id == u) if user_to_unassign is None: assign_errors.append(f"Cannot unassign achievement from user {u}") else: assign = AchievementUser.get_or_none(achievement=achievement, user=user_to_unassign) assign.delete_instance() return jsonify({"success": 1, "errors": assign_errors})
def achievement(user): achievement = Achievement.create(title="test achievement", image=None) AchievementUser.create(achievement=achievement, user=user, comment="some comment") from src.model import db db.db_wrapper.database.close() return achievement
def user(username): """Получить подробную информацию о пользователе""" user = User.get_or_none(User.username == username) if user is None: return errors.not_found() user_dict = user.to_json() user_dict = Vote.add_votes_info(user_dict, 1, get_user_from_request()) user_dict = Achievement.add_achievements(user_dict) return jsonify({"success": 1, "user": user_dict})
def get_achievements(): """Получить список наград""" achievements = Achievement.select() achievements = [a.to_json() for a in achievements] for a in achievements: users = (User.select(User.id, User.username).join( AchievementUser, peewee.JOIN.LEFT_OUTER).where( AchievementUser.achievement == a["id"])) a["users"] = [u.to_json() for u in users] return jsonify({"success": 1, "achievements": achievements})
def assign_achievement(): user = get_user_from_request() if not user.is_admin: return errors.no_access() json = request.get_json() if "users" not in json or "achievement" not in json: return errors.wrong_payload("users", "achievement") if len(json["users"]) == 0: return errors.wrong_payload("users") achievement = Achievement.get_or_none( Achievement.id == json["achievement"]) if achievement is None: return errors.wrong_payload("achievement") assign_errors = [] for u in json["users"]: user_to_assign = User.get_or_none(User.id == u) if user_to_assign is None: assign_errors.append(f"Cannot assign achievement to user {u}") else: AchievementUser.create( achievement=achievement, user=user_to_assign, comment=json.get("comment", None), ) Notification.create( user=user_to_assign, created_date=datetime.datetime.now(), text=f"Новая награда: {achievement.title}", object_type="achievement", object_id=achievement.id, ) return jsonify({"success": 1, "errors": assign_errors})
def convert(): create_app() pattern = r".*?getDisplayName\(\) == '(?P<username>.*?)'}(?P<meta>.*?)<img.*src=\"(?P<image>.*?)\" title=['\"](?P<title>.*)['\"]" # noqa achievements = [] image_base_path = "/var/www/old.kolenka" creator = User.get_or_none(User.username == "Xitilon") lines = old_ach.split("\n") for line in lines: m = re.search(pattern, line) if m: username = m.group("username") user = User.get_or_none(User.username == username) if user is None: print(f"Cannot find user {username}") continue image = m.group("image") title = m.group("title") meta = m.group("meta") created_achivement = None for a in achievements: if a.title == title: created_achivement = a break if created_achivement is None: img = content.upload_image(creator.id, 2020, 3, image_base_path + image) created_achivement = Achievement.create(title=title, image=img) achievements.append(created_achivement) else: img = created_achivement.image AchievementUser.create( achievement=created_achivement, user=user, comment=meta )
def add_achievement(): user = get_user_from_request() if not user.is_admin: return errors.no_access() json = request.get_json() if "title" not in json or "image" not in json: return errors.wrong_payload("title", "image") if len(json["title"]) == 0: return errors.wrong_payload("title") content = Content.get_or_none(Content.id == json["image"]) if content: if not content.is_image: return errors.achievement_is_not_image() elif not content.is_small_image: return errors.achievement_too_large() achievement = Achievement.create(title=json["title"], image=content) return jsonify({"success": 1, "achievement": achievement.to_json()})