def create_variable(name, increase_permission="admin"): variable = Variable() variable.name = name variable.increase_permission = increase_permission DBSession.add(variable) DBSession.flush() return variable
def increase_multi_values(request): try: doc = request.json_body except: raise APIError(400, "invalid_json", "no valid json body") ret = {} for user_id, values in doc.items(): user = User.get_user(user_id) if not user: raise APIError(404, "user_not_found", "user %s not found" % (user_id, )) for variable_name, values_and_keys in values.items(): for value_and_key in values_and_keys: variable = Variable.get_variable_by_name(variable_name) if asbool(get_settings().get("enable_user_authentication", False)): if not Variable.may_increase(variable, request, user_id): raise APIError( 403, "forbidden", "You may not increase the variable %s for user %s." % (variable_name, user_id)) if not variable: raise APIError(404, "variable_not_found", "variable %s not found" % (variable_name, )) if not 'value' in value_and_key: raise APIError(400, "variable_not_found", "illegal value for %s" % (variable_name, )) value = value_and_key['value'] key = value_and_key.get('key', '') Value.increase_value(variable_name, user, value, key) output = _get_progress(achievements_for_user=user, requesting_user=request.user) output = copy.deepcopy(output) to_delete = list() for i in range(len(output["achievements"])): if len(output["achievements"][i]["new_levels"]) > 0: if "levels" in output["achievements"][i]: del output["achievements"][i]["levels"] if "priority" in output["achievements"][i]: del output["achievements"][i]["priority"] if "goals" in output["achievements"][i]: del output["achievements"][i]["goals"] else: to_delete.append(i) for i in sorted(to_delete, reverse=True): del output["achievements"][i] if len(output["achievements"]) > 0: ret[user_id] = output return ret
def create_variables(): invite_users = Variable(name="invite_users", increase_permission="own") DBSession.add(invite_users) cycling = Variable(name="cycling", increase_permission="own") DBSession.add(cycling) DBSession.flush()
def add_Variable(request): variable = Variable() variable.name = request.POST["variable_name"] variable.group = request.POST["variable_group"] DBSession.add(variable) DBSession.flush() return {"variable": "ok"}
def create_variable( variable_name=undefined, variable_group=undefined, ): variable = Variable() variable.name = variable_name variable.group = variable_group DBSession.add(variable) DBSession.flush() return variable
def increase_value(request): """increase a value for the user""" user_id = int(request.matchdict["user_id"]) try: value = float(request.POST["value"]) except: try: doc = request.json_body value = doc["value"] except: raise APIError(400, "invalid_value", "Invalid value provided") key = request.matchdict["key"] if ( "key" in request.matchdict and request.matchdict["key"] is not None) else "" variable_name = request.matchdict["variable_name"] user = User.get_user(user_id) if not user: raise APIError(404, "user_not_found", "user not found") variable = Variable.get_variable_by_name(variable_name) if not variable: raise APIError(404, "variable_not_found", "variable not found") if asbool(get_settings().get("enable_user_authentication", False)): if not Variable.may_increase(variable, request, user_id): raise APIError(403, "forbidden", "You may not increase the variable for this user.") Value.increase_value(variable_name, user, value, key) output = _get_progress(achievements_for_user=user, requesting_user=request.user) output = copy.deepcopy(output) to_delete = list() for i in range(len(output["achievements"])): if len(output["achievements"][i]["new_levels"]) > 0: if "levels" in output["achievements"][i]: del output["achievements"][i]["levels"] if "priority" in output["achievements"][i]: del output["achievements"][i]["priority"] if "goals" in output["achievements"][i]: del output["achievements"][i]["goals"] else: to_delete.append(i) for i in sorted(to_delete, reverse=True): del output["achievements"][i] return output
def goal(request): params = request.GET dir_name = os.path.dirname( os.path.abspath(__file__)) + "\\csv_uploads\\file.csv" if request.method == 'POST': user_id = params["user_id"] variable = request.POST["variable"] goal_name = request.POST["goal_name"] goal_goal = request.POST["goal_goal"] achievement_id = params["id"] goal_condition = request.POST["goal_condition"] Variable.add_variable(variable) Goal.add_goal(goal_name, goal_condition, goal_goal, achievement_id) Value.increase(achievement_id, variable, user_id, dir_name) return HTTPFound(request.route_url('leaderboard', _query=params)) else: with open(dir_name) as f: keys = f.readline().rstrip().split(";") return {'keys': keys, 'params': params}
def populate_demo(DBSession): from gengine.app.model import ( Achievement, AchievementCategory, Variable, Subject, Language, TranslationVariable, Translation, Reward, AchievementProperty, AchievementAchievementProperty, AchievementReward, AuthUser, AuthRole, AuthRolePermission, SubjectType, t_auth_roles_permissions ) def add_translation_variable(name): t = TranslationVariable(name=name) DBSession.add(t) return t def add_translation(variable, lang, text): tr = Translation(translationvariable=variable, text=text, language=lang) DBSession.add(tr) return tr with transaction.manager: subjecttype_country = SubjectType(name="Country") DBSession.add(subjecttype_country) subjecttype_region = SubjectType(name="Region") subjecttype_region.part_of_types.append(subjecttype_country) DBSession.add(subjecttype_region) subjecttype_city = SubjectType(name="City") subjecttype_city.part_of_types.append(subjecttype_region) DBSession.add(subjecttype_city) subjecttype_position = SubjectType(name="Position") DBSession.add(subjecttype_position) subjecttype_team = SubjectType(name="Team") DBSession.add(subjecttype_team) subjecttype_user = DBSession.query(SubjectType).filter_by(name="User").first() if not subjecttype_user: subjecttype_user = SubjectType(name="User") DBSession.add(subjecttype_user) subjecttype_user.part_of_types.append(subjecttype_city) subjecttype_user.part_of_types.append(subjecttype_team) subjecttype_user.part_of_types.append(subjecttype_position) DBSession.add(subjecttype_user) subject_germany = Subject(type=subjecttype_country, name="Germany") DBSession.add(subject_germany) subject_france = Subject(type=subjecttype_country, name="France") DBSession.add(subject_france) subject_india = Subject(type=subjecttype_country, name="India") DBSession.add(subject_india) subject_germany_north = Subject(type=subjecttype_region, name="Germany-North") DBSession.add(subject_germany_north) subject_germany_west = Subject(type=subjecttype_region, name="Germany-West") DBSession.add(subject_germany_west) subject_germany_east = Subject(type=subjecttype_region, name="Germany-East") DBSession.add(subject_germany_east) subject_germany_south = Subject(type=subjecttype_region, name="Germany-South") DBSession.add(subject_germany_south) subject_paderborn = Subject(type=subjecttype_city, name="Paderborn") DBSession.add(subject_paderborn) subject_bielefeld = Subject(type=subjecttype_city, name="Bielefeld") DBSession.add(subject_bielefeld) subject_detmold = Subject(type=subjecttype_city, name="Detmold") DBSession.add(subject_detmold) subject_berlin = Subject(type=subjecttype_city, name="Berlin") DBSession.add(subject_berlin) subject_sales = Subject(type=subjecttype_team, name="Sales") DBSession.add(subject_sales) subject_tech = Subject(type=subjecttype_team, name="Tech") DBSession.add(subject_tech) subject_junior_developer = Subject(type=subjecttype_position, name="Junior Developer") DBSession.add(subject_junior_developer) subject_senior_developer = Subject(type=subjecttype_position, name="Senior Developer") DBSession.add(subject_senior_developer) subject_manager = Subject(type=subjecttype_position, name="Manager") DBSession.add(subject_manager) subject_promoter = Subject(type=subjecttype_position, name="Promoter") DBSession.add(subject_promoter) DBSession.flush() lang_de = Language(name="de") lang_en = Language(name="en") DBSession.add(lang_de) DBSession.add(lang_en) var_invited_users = Variable(name="invite_users") DBSession.add(var_invited_users) var_invited_users = Variable(name="participate", group="none") DBSession.add(var_invited_users) achievementcategory_community = AchievementCategory(name="community") DBSession.add(achievementcategory_community) achievement_invite = Achievement(name='invite_users', evaluation="immediately", maxtimes=20, achievementcategory=achievementcategory_community, condition='{"term": {"type": "literal", "variable": "invite_users"}}', goal="5*level", operator="geq", player_subjecttype=subjecttype_user ) DBSession.add(achievement_invite) achievementcategory_sports = AchievementCategory(name="sports") DBSession.add(achievementcategory_sports) achievement_fittest = Achievement(name='fittest', relevance="friends", maxlevel=100, achievementcategory=achievementcategory_sports, condition='{"term": {"key": ["5","7","9"], "type": "literal", "key_operator": "IN", "variable": "participate"}}', evaluation="weekly", goal="5*level", player_subjecttype=subjecttype_user ) DBSession.add(achievement_fittest) property_name = AchievementProperty(name='name') DBSession.add(property_name) property_xp = AchievementProperty(name='xp') DBSession.add(property_xp) property_icon = AchievementProperty(name='icon') DBSession.add(property_icon) reward_badge = Reward(name='badge', rewarded_subjecttype=subjecttype_user) DBSession.add(reward_badge) reward_image = Reward(name='backgroud_image', rewarded_subjecttype=subjecttype_user) DBSession.add(reward_image) transvar_invite_name = add_translation_variable(name="invite_users_achievement_name") add_translation(transvar_invite_name, lang_en, 'Invite ${5*level} Users') add_translation(transvar_invite_name, lang_de, 'Lade ${5*level} Freunde ein') DBSession.add(AchievementAchievementProperty(achievement=achievement_invite, property=property_name, value_translation=transvar_invite_name)) DBSession.add(AchievementAchievementProperty(achievement=achievement_invite, property=property_xp, value='${100 * level}')) DBSession.add(AchievementAchievementProperty(achievement=achievement_invite, property=property_icon, value="https://www.gamification-software.com/img/running.png")) DBSession.add(AchievementReward(achievement=achievement_invite, reward=reward_badge, value="https://www.gamification-software.com/img/trophy.png", from_level=5)) DBSession.add(AchievementReward(achievement=achievement_invite, reward=reward_image, value="https://www.gamification-software.com/img/video-controller-336657_1920.jpg", from_level=5)) transvar_fittest_name = add_translation_variable(name="fittest_achievement_name") add_translation(transvar_fittest_name, lang_en, 'Do the most sport activities among your friends') add_translation(transvar_fittest_name, lang_de, 'Mache unter deinen Freunden am meisten Sportaktivitäten') DBSession.add(AchievementAchievementProperty(achievement=achievement_fittest, property=property_name, value_translation=transvar_fittest_name)) DBSession.add(AchievementAchievementProperty(achievement=achievement_fittest, property=property_xp, value='${50 + (200 * level)}')) DBSession.add(AchievementAchievementProperty(achievement=achievement_fittest, property=property_icon, value="https://www.gamification-software.com/img/colorwheel.png")) DBSession.add(AchievementReward(achievement=achievement_fittest, reward=reward_badge, value="https://www.gamification-software.com/img/easel.png", from_level=1)) DBSession.add(AchievementReward(achievement=achievement_fittest, reward=reward_image, value="https://www.gamification-software.com/img/game-characters-622654.jpg", from_level=1)) DBSession.flush() user1 = Subject(lat=10, lng=50, timezone="Europe/Berlin", name="Fritz", type=subjecttype_user) user2 = Subject(lat=10, lng=50, timezone="US/Eastern", name="Ludwig", type=subjecttype_user) user3 = Subject(lat=10, lng=50, name="Helene", type=subjecttype_user) user1.friends.append(user2) user1.friends.append(user3) user2.friends.append(user1) user2.friends.append(user3) user3.friends.append(user1) user3.friends.append(user2) user1.part_of_subjects.append(subject_bielefeld) user1.part_of_subjects.append(subject_sales) user1.part_of_subjects.append(subject_manager) user2.part_of_subjects.append(subject_bielefeld) user2.part_of_subjects.append(subject_sales) user2.part_of_subjects.append(subject_promoter) user3.part_of_subjects.append(subject_paderborn) user3.part_of_subjects.append(subject_sales) user3.part_of_subjects.append(subject_promoter) DBSession.add(user2) DBSession.add(user3) DBSession.flush() try: auth_user = DBSession.query(AuthUser).filter_by(email="*****@*****.**").first() if not auth_user: auth_user = AuthUser(subject=user1, email="*****@*****.**", password="******", active=True) DBSession.add(auth_user) auth_role = DBSession.query(AuthRole).filter_by(name="Global Admin").first() if not auth_role: auth_role = AuthRole(name="Global Admin") DBSession.add(auth_role) DBSession.flush() for perm in yield_all_perms(): if not exists_by_expr(t_auth_roles_permissions, and_( t_auth_roles_permissions.c.auth_role_id == auth_role.id, t_auth_roles_permissions.c.name == perm[0] )): DBSession.add(AuthRolePermission(role=auth_role, name=perm[0])) auth_user.roles.append(auth_role) DBSession.add(auth_user) except ImportError as e: print("[auth] feature not installed - not importing auth demo data")
def increase_value(request): """increase a value for the subject""" subject_id = int(request.matchdict["subject_id"]) try: value = float(request.POST["value"]) except: try: doc = request.json_body value = doc["value"] except: raise APIError(400, "invalid_value", "Invalid value provided") key = request.matchdict["key"] if ( "key" in request.matchdict and request.matchdict["key"] is not None) else "" variable_name = request.matchdict["variable_name"] subject = Subject.get_subject(subject_id) if not subject: raise APIError(404, "subject_not_found", "subject not found") variable = Variable.get_variable_by_name(variable_name) if not variable: raise APIError(404, "variable_not_found", "variable not found") if asbool(get_settings().get("enable_user_authentication", False)): if not AuthUser.may_increase(variable, request, subject_id): raise APIError( 403, "forbidden", "You may not increase the variable for this subject.") Value.increase_value(variable_name, subject["id"], value, key, at_datetime=dt_now()) try: achievement_history = int(request.GET["achievement_history"]) except: achievement_history = 2 output = _get_progress(achievements_for_subject=subject, requesting_subject=request.subject, achievement_history=achievement_history) output = copy.deepcopy(output) to_delete = list() for i in range(len(output["achievements"])): if len(output["achievements"][i]["new_levels"]) > 0: if "levels" in output["achievements"][i]: del output["achievements"][i]["levels"] if "priority" in output["achievements"][i]: del output["achievements"][i]["priority"] if "goals" in output["achievements"][i]: del output["achievements"][i]["goals"] else: to_delete.append(i) for i in sorted(to_delete, reverse=True): del output["achievements"][i] return output
def populate_demo(DBSession): from gengine.app.model import ( Achievement, AchievementCategory, Goal, Variable, User, Language, TranslationVariable, Translation, GoalProperty, GoalGoalProperty, Reward, AchievementProperty, AchievementAchievementProperty, AchievementReward, AuthUser, AuthRole, AuthRolePermission ) def add_translation_variable(name): t = TranslationVariable(name=name) DBSession.add(t) return t def add_translation(variable, lang, text): tr = Translation(translationvariable=variable, text=text, language=lang) DBSession.add(tr) return tr with transaction.manager: lang_de = Language(name="de") lang_en = Language(name="en") DBSession.add(lang_de) DBSession.add(lang_en) var_invited_users = Variable(name="invite_users") DBSession.add(var_invited_users) var_invited_users = Variable(name="participate", group="none") DBSession.add(var_invited_users) goal_property_name = GoalProperty(name='name') DBSession.add(goal_property_name) achievementcategory_community = AchievementCategory(name="community") DBSession.add(achievementcategory_community) achievement_invite = Achievement(name='invite_users', evaluation="immediately", maxtimes=20, achievementcategory=achievementcategory_community) DBSession.add(achievement_invite) transvar_invite = add_translation_variable(name="invite_users_goal_name") add_translation(transvar_invite, lang_en, 'Invite ${5*level} Users') add_translation(transvar_invite, lang_de, 'Lade ${5*level} Freunde ein') achievement_invite_goal1 = Goal(name_translation=transvar_invite, condition='{"term": {"type": "literal", "variable": "invite_users"}}', goal="5*level", operator="geq", achievement=achievement_invite) DBSession.add(achievement_invite_goal1) DBSession.add(GoalGoalProperty(goal=achievement_invite_goal1, property=goal_property_name, value_translation=transvar_invite)) achievementcategory_sports = AchievementCategory(name="sports") DBSession.add(achievementcategory_sports) achievement_fittest = Achievement(name='fittest', relevance="friends", maxlevel=100, achievementcategory=achievementcategory_sports) DBSession.add(achievement_fittest) transvar_fittest = add_translation_variable(name="fittest_goal_name") add_translation(transvar_fittest, lang_en, 'Do the most sport activities among your friends') add_translation(transvar_fittest, lang_de, 'Mache unter deinen Freunden am meisten Sportaktivitäten') achievement_fittest_goal1 = Goal(name_translation=transvar_fittest, condition='{"term": {"key": ["5","7","9"], "type": "literal", "key_operator": "IN", "variable": "participate"}}', evaluation="weekly", goal="5*level", achievement=achievement_fittest ) DBSession.add(achievement_fittest_goal1) DBSession.add(GoalGoalProperty(goal=achievement_fittest_goal1, property=goal_property_name, value_translation=transvar_fittest)) property_name = AchievementProperty(name='name') DBSession.add(property_name) property_xp = AchievementProperty(name='xp') DBSession.add(property_xp) property_icon = AchievementProperty(name='icon') DBSession.add(property_icon) reward_badge = Reward(name='badge') DBSession.add(reward_badge) reward_image = Reward(name='backgroud_image') DBSession.add(reward_image) transvar_invite_name = add_translation_variable(name="invite_achievement_name") add_translation(transvar_invite_name, lang_en, 'The Community!') add_translation(transvar_invite_name, lang_de, 'Die Community!') DBSession.add(AchievementAchievementProperty(achievement=achievement_invite, property=property_name, value_translation=transvar_invite_name)) DBSession.add(AchievementAchievementProperty(achievement=achievement_invite, property=property_xp, value='${100 * level}')) DBSession.add(AchievementAchievementProperty(achievement=achievement_invite, property=property_icon, value="https://www.gamification-software.com/img/running.png")) DBSession.add(AchievementReward(achievement=achievement_invite, reward=reward_badge, value="https://www.gamification-software.com/img/trophy.png", from_level=5)) DBSession.add(AchievementReward(achievement=achievement_invite, reward=reward_image, value="https://www.gamification-software.com/img/video-controller-336657_1920.jpg", from_level=5)) transvar_fittest_name = add_translation_variable(name="fittest_achievement_name") add_translation(transvar_fittest_name, lang_en, 'The Fittest!') add_translation(transvar_fittest_name, lang_de, 'Der Fitteste!') DBSession.add(AchievementAchievementProperty(achievement=achievement_fittest, property=property_name, value_translation=transvar_fittest_name)) DBSession.add(AchievementAchievementProperty(achievement=achievement_fittest, property=property_xp, value='${50 + (200 * level)}')) DBSession.add(AchievementAchievementProperty(achievement=achievement_fittest, property=property_icon, value="https://www.gamification-software.com/img/colorwheel.png")) DBSession.add(AchievementReward(achievement=achievement_fittest, reward=reward_badge, value="https://www.gamification-software.com/img/easel.png", from_level=1)) DBSession.add(AchievementReward(achievement=achievement_fittest, reward=reward_image, value="https://www.gamification-software.com/img/game-characters-622654.jpg", from_level=1)) user1 = User(id=1,lat=10,lng=50,timezone="Europe/Berlin") user2 = User(id=2,lat=10,lng=50,timezone="US/Eastern") user3 = User(id=3,lat=10,lng=50) user1.friends.append(user2) user1.friends.append(user3) user2.friends.append(user1) user2.friends.append(user3) user3.friends.append(user1) user3.friends.append(user2) DBSession.add(user1) DBSession.add(user2) DBSession.add(user3) DBSession.flush() try: auth_user = AuthUser(user_id=user1.id,email="*****@*****.**",password="******",active=True) DBSession.add(auth_user) auth_role = AuthRole(name="Global Admin") DBSession.add(auth_role) DBSession.add(AuthRolePermission(role=auth_role, name=perm_global_access_admin_ui)) DBSession.add(AuthRolePermission(role=auth_role, name=perm_global_delete_user)) DBSession.add(AuthRolePermission(role=auth_role, name=perm_global_increase_value)) DBSession.add(AuthRolePermission(role=auth_role, name=perm_global_update_user_infos)) auth_user.roles.append(auth_role) DBSession.add(auth_user) except ImportError as e: print("[auth] feature not installed - not importing auth demo data")