本文整理汇总了Python中app.needs.Need类的典型用法代码示例。如果您正苦于以下问题:Python Need类的具体用法?Python Need怎么用?Python Need使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Need类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_staff
def remove_staff(self, course, user, data):
need = Need("staff")
if not course.can(user, need, course):
raise need.exception()
if data['staff_member'] in course.staff:
course.staff.remove(data['staff_member'])
course.put()
示例2: reject_invitation
def reject_invitation(self, group, user, data):
# can only reject an invitation if you are in the invited_members
need = Need('invitation')
if not group.can(user, need, group):
raise need.exception()
group.invited_members.remove(user.key)
group.put()
示例3: index
def index(self):
"""
Index HTTP method. Should be called from GET when no key is provided.
Processes cursor and num_page URL arguments for pagination support.
"""
query = self.model.query()
need = Need('index')
result = self.model.can(session['user'], need, query=query)
if not result:
return need.api_response()
args = self.parse_args(True)
query = filter_query(result, args, self.model)
created_prop = getattr(self.model, 'created', None)
if not query.orders and created_prop:
logging.info("Adding default ordering by creation time.")
query = query.order(-created_prop)
page = int(request.args.get('page', 1))
num_page = request.args.get('num_page', None)
query_results = paginate(query, page, num_page)
add_statistics = request.args.get('stats', False)
if add_statistics:
query_results['statistics'] = self.statistics()
return create_api_response(200, "success", query_results)
示例4: post
def post(self, user, data):
need = Need('create')
if not self.model.can(user, need, None):
raise need.exception()
job_type, filters = data['job_type'], data['filters']
if not isinstance(filters, list):
raise BadValueError('filters must be a list of triples')
for filter in filters:
if len(filter) != 3:
raise BadValueError('filters must be a list of triples')
if job_type not in analytics.available_jobs:
raise BadValueError('job must be of the following types: %s' %
', '.join(list(analytics.available_jobs.keys())))
job = analytics.get_job(job_type, user, filters)
job.start()
return (201, 'success', {
'key': job.job_dump.key.id()
})
示例5: remove_member
def remove_member(self, group, user, data):
# can only remove a member if you are a member
need = Need('member')
if not group.can(user, need, group):
raise need.exception()
if data['member'] in group.members:
group.members.remove(data['member'])
elif data['member'] in group.invited_members:
group.invited_members.remove(data['member'])
if len(group.members) == 0:
group.key.delete()
description = "Deleted group"
else:
group.put()
description = "Changed group"
audit_log_message = models.AuditLog(
event_type='Group.remove_member',
user=user.key,
obj=group.key,
description=description
)
audit_log_message.put()
示例6: put
def put(self, key):
"""
The PUT HTTP method
"""
obj = self.model.get_by_id(key)
if not obj:
return create_api_response(404, "{resource} {key} not found".format(
resource=self.name, key=key))
need = Need('get')
if not obj.can(session['user'], need, obj):
return need.api_response()
need = Need('put')
if not obj.can(session['user'], need, obj):
return need.api_response()
blank_val = object()
changed = False
for key, value in self.parse_args(False).iteritems():
old_val = getattr(obj, key, blank_val)
if old_val == blank_val:
return create_api_response(
400, "{} is not a valid field.".format(key))
setattr(obj, key, value)
changed = True
if changed:
obj.put()
return create_api_response(200, "", obj)
示例7: index
def index(self, user, data):
"""
Index HTTP method. Should be called from GET when no key is provided.
Processes cursor and num_page URL arguments for pagination support.
:param user: (object) caller
:param data: (dictionary)
:return: results for query
"""
query = self.model.query()
need = Need('index')
result = self.model.can(user, need, query=query)
if not result:
raise need.exception()
query = filter_query(result, data, self.model)
created_prop = getattr(self.model, 'created', None)
if not query.orders and created_prop:
logging.info('Adding default ordering by creation time.')
query = query.order(-created_prop, self.model.key)
page = int(request.args.get('page', 1))
# default page length is 100
num_page = int(request.args.get('num_page', 100))
query_results = paginate(query, page, num_page)
add_statistics = request.args.get('stats', False)
if add_statistics:
query_results['statistics'] = self.statistics()
return query_results
示例8: get_students
def get_students(self, course, user, data):
query = models.Participant.query(
models.Participant.course == course.key)
need = Need('staff')
if not models.Participant.can(user, need, course, query):
raise need.exception()
return list(query.fetch())
示例9: score
def score(self, obj, user, data):
"""
Sets composition score
:param obj: (object) target
:param user: (object) caller
:param data: (dictionary) data
:return: (int) score
"""
need = Need('grade')
if not obj.can(user, need, obj):
raise need.exception()
score = models.Score(
score=data['score'],
message=data['message'],
grader=user.key)
grade = score.put()
submission = obj.submission.get()
# Create or updated based on existing scores.
if data['source'] == 'composition':
# Only keep any autograded scores.
submission.score = [autograde for autograde in submission.score \
if score.autograder]
submission.score.append(score)
else:
submission.score.append(score)
submission.put()
return score
示例10: add_staff
def add_staff(self, course, user, data):
need = Need('staff')
if not course.can(user, need, course):
raise need.exception()
user = models.User.get_or_insert(data['email'])
if user not in course.staff:
models.Participant.add_role(user, course, STAFF_ROLE)
示例11: get_staff
def get_staff(self, course, user, data):
need = Need('staff')
if not course.can(user, need, course):
raise need.exception()
query = models.Participant.query(
models.Participant.course == course.key,
models.Participant.role == 'staff')
return list(query.fetch())
示例12: remove_staff
def remove_staff(self, course, user, data):
need = Need('staff')
if not course.can(user, need, course):
raise need.exception()
removed_user = models.User.lookup(data['email'])
if removed_user:
models.Participant.remove_role(removed_user, course, STAFF_ROLE)
示例13: current
def current(self, obj, user, data):
need = Need('get')
if not obj.can(user, need, obj):
raise need.exception()
if not obj.current_version:
raise BadValueError(
'Invalid version resource. Contact an administrator.')
return obj.current_version
示例14: invite
def invite(self, group, user, data):
need = Need('invite')
if not group.can(user, need, group):
return need.exception()
error = group.invite(data['email'])
if error:
raise BadValueError(error)
示例15: get_instance
def get_instance(self, key, user):
obj = self.model.get_by_id(key)
if not obj:
raise BadKeyError(key)
need = Need('get')
if not obj.can(user, need, obj):
raise need.exception()
return obj