本文整理汇总了Python中api_db_utils.APIDB类的典型用法代码示例。如果您正苦于以下问题:Python APIDB类的具体用法?Python APIDB怎么用?Python APIDB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了APIDB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: trainee_profile
def trainee_profile(req):
"""
``GET`` @ |ta| + ``/user/current``
``PUT`` @ |ta| + ``/user/current``
- Profile of the current user.
- Updates the profile of the user.
- |ul|
"""
out = ['id', 'name', 'nickname', 'gender', 'picture', 'avatar', 'birthday', 'country', 'city', 'language',
'email', 'phone', 'active_club', 'sensors']
if req.method == "GET":
return sanitize_json(req.user, out, except_on_missing=False)
elif req.method == "PUT":
j_req = json_from_request(req,
optional_props=['name', 'nickname', 'gender', 'picture', 'avatar', 'birthday',
'country', 'city', 'language',
'email', 'phone', 'activeClub', 'sensors'])
if 'active_club' in j_req:
membership = APIDB.get_user_club_role(req.user, Key(urlsafe=j_req['active_club']))
if membership != "MEMBER":
raise BadRequest("It seems that you want to activate a club that you are not member of")
update, user = APIDB.update_user(req.user, **j_req)
s_token = GCAuth.auth_user_token(user)
deferred.defer(sync_user, user, s_token)
return sanitize_json(user, out, except_on_missing=False)
示例2: coach_participations_performances
def coach_participations_performances(req, uskey_participation):
"""
``GET`` @ |ca| + ``/participations/<uskey_participation>/performances``
Perfomances of a participation. |uroleOT|
"""
participation = req.model
user = participation.user.get()
performances = APIDB.get_performances_from_participation(participation)
res_list = []
session = participation.session.get()
subscription = APIDB.get_course_subscription(session.course, user)
for performance in performances:
res_obj = performance.to_dict()
activity = performance.activity.get()
res_obj['activity'] = sanitize_json(activity, ['id', 'name'])
res_obj['indicators'] = performance.indicators
res_list.append(sanitize_json(res_obj,
['activity', 'record_date', 'completeness', 'indicators', 'max_completeness',
'level']))
d_participation = participation.to_dict()
d_participation['max_completeness'] = participation.max_completeness
d_participation['indicators'] = participation.indicators
return dict(user=sanitize_json(user, ['id', 'name', 'picture', 'sensors'], except_on_missing=False),
participation=sanitize_json(d_participation,
['id', 'max_completeness', 'completeness', 'time', 'indicators']),
subscription=sanitize_json(subscription, ['id', 'profile_level','disabled_exercises']),
session=sanitize_json(session, ['id', 'name', 'max_level']), performances=res_list)
示例3: coach_course_subscription_details
def coach_course_subscription_details(req, uskey_subscription):
"""
``GET`` @ |ca| + ``/subscriptions/<uskey_subscription>``
GET a subscription. |uroleOT|
"""
subscription = req.model
user = subscription.member.get()
res = sanitize_json(subscription.to_dict(), ['id', 'start_date', 'profile_level', 'observations', 'disabled_exercises','max_level','profile'])
res['user'] = sanitize_json(user, ['id', 'name', 'picture'])
sessions = APIDB.get_course_sessions(subscription.course.get())
res_list = []
added_activities = []
for session in sessions:
activities = APIDB.get_session_exercises(session)
for activity in activities:
if activity.id not in added_activities:
added_activities.append(activity.id)
j_activity = activity.to_dict()
level_count = len(activity.levels)
j_activity['level_count'] = level_count
# this is already a json, see docs in the model
res_list.append(j_activity)
res['activities'] = sanitize_list(res_list,allowed=['id', 'name', 'level_count'])
return res
示例4: coach_session_participations
def coach_session_participations(req, uskey_session):
"""
``GET`` @ |ca| + ``/sessions/<uskey_session>/participations``
Participation on a session. |uroleOT|
"""
session = req.model
user = req.user
j_req = json_from_paginated_request(req)
# if j_req['paginated']:
participations, total = APIDB.get_session_participations(session, paginated=True, size=j_req['size'],
page=j_req['page'])
# else:
# participations = APIDB.get_session_participations(session, paginated=True, size=j_req['size'],
# page=j_req['page'])
res_list = []
for participation in participations:
res = participation.to_dict()
res['user'] = sanitize_json(participation.user.get(), ['name', 'id', 'avatar', 'picture', 'sensors'],
except_on_missing=False)
subscription = APIDB.get_course_subscription(session.course, participation.user)
res['subscription_id'] = subscription.id
res['current_level'] = subscription.profile_level
res['level'] = participation.level
res['level_up'] = subscription.increase_level
res['indicators'] = participation.indicators
res['max_completeness'] = participation.max_completeness
res['participation_count'] = participation.participation_count
res_list.append(sanitize_json(res, ['id', 'user', 'level_up', 'current_level', 'indicators',
'max_completeness', 'completeness', 'participation_count', 'time',
'level', 'subscription_id']))
return dict(results=res_list, total=total)
示例5: trainee_club_session_list_ongoing
def trainee_club_session_list_ongoing(req, uskey_club):
"""
``GET`` @ |ta| + ``/clubs/<uskey_course>/sessions``
List of the session of a club. |uroleM|
"""
# TODO: test
club = req.model
# j_req = json_from_request(req)
# session_type = j_req['type']
sessions = APIDB.get_sessions_im_subscribed(req.user, club, paginated=False)
res_list = []
for session in sessions:
if session.status == "ONGOING":
res_obj = session.to_dict()
res_obj['status'] = session.status
res_obj['participated'] = APIDB.user_participated_in_session(req.user, session)
res_obj['participation_count'] = APIDB.user_participation_details(req.user, session, count_only=True)
res_obj['max_score'] = APIDB.session_completeness(req.user, session)
course = session.course.get()
res_obj['course_id'] = course.id
res_obj['course_name'] = course.name
# no edist here, since the data on the type are already removed
return sanitize_json(res_obj, hidden=['course', 'list_exercises', 'profile', 'activities', 'on_before',
'on_after', 'meta_data'])
return dict()
示例6: coach_course_create
def coach_course_create(req, uskey_club):
"""
``POST`` @ |ca| + ``/clubs/<uskey_club>/courses``
Create a course for the club. |uroleOT|
"""
j_req = json_from_request(req, mandatory_props=["name", "description", "courseType", "maxLevel"],
optional_props=["startDate", "endDate", "duration", "profile"])
try:
if "startDate" in j_req:
j_req['startDate'] = datetime.datetime.fromtimestamp(long(j_req['startDate']) / 1000)
except Exception as e:
raise BadParameters("startDate is not a correct date")
# j_req['startDate'] = datetime.datetime.now()
try:
if "startDate" in j_req:
j_req['endDate'] = datetime.datetime.fromtimestamp(long(j_req['endDate']) / 1000)
except Exception as e:
raise BadParameters("endDate is not a correct date")
if "duration" in j_req:
j_req['duration'] = int(j_req['duration'])
course = APIDB.create_course(req.model, **j_req)
APIDB.add_trainer_to_course(req.user, course)
req.model = course
return coach_course_detail(req, uskey_club)
示例7: coach_course_delete
def coach_course_delete(req, uskey_course):
"""
``DELETE`` @ |ca| + ``/courses/<uskey_course>``
Delete the course. |uroleOT|
"""
APIDB.delete_course(req.model)
return HttpEmpty()
示例8: coach_club_delete
def coach_club_delete(req, uskey_club):
"""
``DELETE`` @ |ca| + ``/clubs/<uskey_club>``
Delete a club. |uroleOT|
"""
club = req.model
APIDB.delete_club(club)
return HttpEmpty()
示例9: coach_course_session_delete
def coach_course_session_delete(req, uskey_session):
"""
``DELETE`` @ |ca| + ``/course/<uskey_course>/sessions``
Deletes the sessions . |uroleOT|
"""
session = req.model
APIDB.delete_session(session)
return HttpEmpty()
示例10: coach_course_subscription_update_level
def coach_course_subscription_update_level(req, uskey_subscription):
"""
``PUT`` @ |ca| + ``/subscriptions/<uskey_subscription>/level``
Updates the level of a subscription. |uroleOT|
"""
subscription = req.model
j_req = json_from_request(req, mandatory_props=['profileLevel'], optional_props=['increaseLevel'])
APIDB.update_subscription(subscription, **j_req)
return HttpEmpty()
示例11: coach_club_update
def coach_club_update(req, uskey_club):
"""
``PUT`` @ |ca| + ``/clubs/<uskey_club>``
Update a club. |uroleOT|
"""
j_req = json_from_request(req, optional_props=["name", "description", "url", "isOpen", "tags"])
club = req.model
APIDB.update_club(req.model, **j_req)
req.model = club
return coach_club_details(req, None)
示例12: coach_details_update
def coach_details_update(req, uskey_detail):
"""
``POST`` @ |ca| + ``/details/<uskey_detail>``
Updates a detail. |uroleOT|
"""
detail = req.model
j_req = json_from_request(req, optional_props=['name', 'd etailType', 'description'])
APIDB.update_detail(detail, **j_req)
req.model = detail
return coach_details_detail(req, uskey_detail)
示例13: coach_club_create
def coach_club_create(req):
"""
``POST`` @ |ca| + ``/clubs``
Create a club.
"""
j_req = json_from_request(req, mandatory_props=["name", "url", "isOpen"], optional_props=['tags', "description"])
club = APIDB.create_club(**j_req)
APIDB.add_owner_to_club(req.user, club)
# users the rendering of club details, add 201 code
req.model = club
return HttpCreated(coach_club_details(req, None))
示例14: coach_course_session_list
def coach_course_session_list(req, uskey_course):
"""
``GET`` @ |ca| + ``/course/<uskey_course>/sessions``
List of the sessions of a course. |uroleOT|
"""
course = req.model
j_req = json_from_paginated_request(req, (('status', None), ('type', None),
('from', None),
('to', None)))
# date_to_js_timestamp(datetime.datetime.now())))
page = int(j_req['page'])
size = int(j_req['size'])
status = j_req['status']
try:
date_from = datetime.datetime.fromtimestamp(long(j_req['from']) / 1000)
except Exception as e:
date_from = None
try:
date_to = datetime.datetime.fromtimestamp(long(j_req['to']) / 1000)
except Exception as e:
date_to = None
# raise BadParameters("Problems with the data format %s" % e.message)
session_type = j_req['type']
sessions, total = APIDB.get_course_sessions(course, date_from=date_from, date_to=date_to, session_type=session_type,
status=status,
paginated=True, page=page, size=size)
res_list = []
for session in sessions:
res_obj = session.to_dict()
res_obj['status'] = session.status
# res_obj['participated'] = APIDB.user_participated_in_session(req.user, session)
session_participations = APIDB.get_session_participations(session, count_only=True)
course_subscribers = APIDB.get_course_subscribers(course, count_only=True)
res_obj['participation_count'] = session_participations
if course_subscribers:
res_obj['participation_percent'] = 100 * (float(session_participations) / float(course_subscribers))
else:
res_obj['participation_percent'] = 0
# res_obj['actnoivity_count'] = session.activity_count
# TODO: check the 'url' here
allowed = ['id', 'name', 'status', 'participation_count',
'session_type', 'participation_percent', 'max_level', 'profile']
course_type = session.course.get().course_type
if course_type == "SCHEDULED":
allowed += ["start_date", "end_date"]
elif course_type == "PROGRAM":
allowed += ["week_no", "day_no"]
res_list.append(sanitize_json(res_obj, allowed=allowed))
return dict(total=total, results=res_list)
示例15: coach_course_subscription_delete
def coach_course_subscription_delete(req, uskey_subscription):
"""
``DELETE`` @ |ca| + ``/subscriptions/<uskey_subscription>``
Deletes a subscription. |uroleOT|
"""
subscription = req.model
club = subscription.course.get().club.get()
if not APIDB.get_user_club_role(req.user, club) in ["TRAINER", "OWNER"]:
raise AuthenticationError("User is not a OWNER or TRAINER")
APIDB.deactivate(subscription)
return HttpEmpty()