def subject_remove_from_parent(request, *args, **kw): context = request.context parent_row = context.subject_row if not request.has_perm(perm_global_manage_subjects): raise APIError(403, "forbidden") q = t_subjects_subjects.select().where(and_( t_subjects_subjects.c.subject_id == request.validated_params.body["subject_id"], t_subjects_subjects.c.part_of_id == parent_row["id"], )) r = DBSession.execute(q).fetchone() if r: q = t_subjects_subjects.delete().where(and_( t_subjects_subjects.c.subject_id == request.validated_params.body["subject_id"], t_subjects_subjects.c.part_of_id == parent_row["id"], )) update_connection().execute(q) return r_status.output({ "status": "ok" })
def test_evaluate_achievement_for_invite_users(self): achievement = create_achievement( achievement_name="invite_users_achievement", achievement_relevance="friends", achievement_maxlevel=10) user = create_user() achievement_date = Achievement.get_datetime_for_evaluation_type( achievement.evaluation_timezone, achievement.evaluation) create_achievement_user(user=user, achievement=achievement, achievement_date=achievement_date, level=1) update_connection().execute(t_values.delete()) create_variable("invite_users", variable_group="day") Value.increase_value(variable_name="invite_users", user=user, value=1, key=None, at_datetime=achievement_date) create_goals(achievement, goal_goal="1*level", goal_operator="geq", goal_group_by_key=False) clear_all_caches() level = Achievement.evaluate(user, achievement.id, achievement_date).get("level") print("level: ", level) Value.increase_value(variable_name="invite_users", user=user, value=8, key=None, at_datetime=achievement_date) level1 = Achievement.evaluate(user, achievement.id, achievement_date).get("level") print("level1 ", level1) Value.increase_value(variable_name="invite_users", user=user, value=5, key=None, at_datetime=achievement_date) level2 = Achievement.evaluate(user, achievement.id, achievement_date).get("level") print("level2: ", level2) self.assertEqual(level, 1) self.assertEqual(level1, 9) self.assertEqual(level2, 10)
def apns_feedback(apns, prefix): # Get feedback messages. uS = update_connection() for (token_hex, fail_time) in apns.feedback_server.items(): try: if not isinstance(token_hex, str): token_hex = token_hex.decode("utf8") token_hex = prefix + token_hex log.debug("APNS Feedback Entry: %s", token_hex + "_" + str(fail_time)) # do stuff with token_hex and fail_time q = t_subject_device.select().where( t_subject_device.c.push_id == token_hex) rows = uS.execute(q).fetchall() for device in rows: log.debug("APNSPushID found in Database: %s", token_hex) if fail_time > device["registered"]: log.debug("Fail-Time is before Registered-At") uS.execute(t_subject_device.delete().where( t_subject_device.c.device_id == device["device_id"], t_subject_device.c.subject_id == device["subject_id_id"], )) except: log.exception("Processing APNS Feedback failed for an entry.")
def set_messages_read(request): try: doc = request.json_body except: raise APIError(400, "invalid_json", "no valid json body") subject_id = int(request.matchdict["subject_id"]) if asbool(get_settings().get("enable_user_authentication", False)): may_read_messages = request.has_perm( perm_global_read_messages ) or request.has_perm(perm_own_read_messages) and str( request.subject.id) == str(subject_id) if not may_read_messages: raise APIError(403, "forbidden", "You may not read the messages of this subject.") if not exists_by_expr(t_subjects, t_subjects.c.id == subject_id): raise APIError(404, "set_messages_read.subject_not_found", "There is no subject with this id.") message_id = doc.get("message_id") q = select([t_subject_messages.c.id, t_subject_messages.c.created_at], from_obj=t_subject_messages).where( and_(t_subject_messages.c.id == message_id, t_subject_messages.c.subject_id == subject_id)) msg = DBSession.execute(q).fetchone() if not msg: raise APIError(404, "set_messages_read.message_not_found", "There is no message with this id.") uS = update_connection() uS.execute(t_subject_messages.update().values({ "is_read": True }).where( and_(t_subject_messages.c.subject_id == subject_id, t_subject_messages.c.created_at <= msg["created_at"]))) return {"status": "ok"}