本文整理汇总了Python中misago.core.shortcuts.get_int_or_404函数的典型用法代码示例。如果您正苦于以下问题:Python get_int_or_404函数的具体用法?Python get_int_or_404怎么用?Python get_int_or_404使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_int_or_404函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read
def read(self, request, thread_pk, pk):
thread = self.get_thread(request, get_int_or_404(thread_pk))
post = self.get_post(request, thread, get_int_or_404(pk)).model
request.user.lock()
return post_read_endpoint(request, thread.model, post)
示例2: post_editor
def post_editor(self, request, thread_pk, pk):
thread = self.get_thread(
request,
get_int_or_404(thread_pk),
read_aware=False,
subscription_aware=False
)
post = self.get_post(request, thread, get_int_or_404(pk)).model
allow_edit_post(request.user, post)
attachments = []
for attachment in post.attachment_set.order_by('-id'):
add_acl(request.user, attachment)
attachments.append(attachment)
attachments_json = AttachmentSerializer(
attachments, many=True, context={'user': request.user}).data
return Response({
'id': post.pk,
'api': post.get_api_url(),
'post': post.original,
'attachments': attachments_json,
'can_protect': bool(thread.category.acl['can_protect_posts']),
'is_protected': post.is_protected,
'poster': post.poster_name
})
示例3: generic
def generic(request):
queryset = get_user_model().objects
if request.query_params.get('followers'):
user_pk = get_int_or_404(request.query_params.get('followers'))
queryset = get_object_or_404(queryset, pk=user_pk).followed_by
elif request.query_params.get('follows'):
user_pk = get_int_or_404(request.query_params.get('follows'))
queryset = get_object_or_404(queryset, pk=user_pk).follows
if request.query_params.get('rank'):
rank_pk = get_int_or_404(request.query_params.get('rank'))
rank = get_object_or_404(Rank.objects, pk=rank_pk, is_tab=True)
queryset = queryset.filter(rank=rank)
if request.query_params.get('name'):
name_starts_with = request.query_params.get('name').strip().lower()
if name_starts_with:
queryset = queryset.filter(slug__startswith=name_starts_with)
else:
raise Http404()
queryset = queryset.select_related('rank', 'ban_cache', 'online_tracker')
paginator = Paginator()
users = paginator.paginate_queryset(queryset.order_by('slug'), request)
make_users_status_aware(users, request.user.acl)
return paginator.get_paginated_response(
UserSerializer(users, many=True).data)
示例4: forum_options
def forum_options(self, request, pk=None):
get_int_or_404(pk)
allow_self_only(request.user, pk, _("You can't change other users options."))
serializer = ForumOptionsSerializer(request.user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'detail': _("Your forum options have been changed.")})
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例5: forum_options
def forum_options(self, request, pk=None):
get_int_or_404(pk)
allow_self_only(request.user, pk, _("You can't change other users options."))
form = ForumOptionsForm(request.data, instance=request.user)
if form.is_valid():
form.save()
return Response({
'detail': _("Your forum options have been changed.")
})
else:
return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
示例6: search
def search(request, search_provider=None):
allowed_providers = searchproviders.get_allowed_providers(request)
if not request.user.acl_cache['can_search'] or not allowed_providers:
raise PermissionDenied(_("You don't have permission to search site."))
search_query = get_search_query(request)
response = []
for provider in allowed_providers:
provider_data = {
'id': provider.url,
'name': six.text_type(provider.name),
'icon': provider.icon,
'url': reverse('misago:search', kwargs={'search_provider': provider.url}),
'api': reverse('misago:api:search', kwargs={'search_provider': provider.url}),
'results': None,
'time': None,
}
if not search_provider or search_provider == provider.url:
start_time = time()
if search_provider == provider.url:
page = get_int_or_404(request.query_params.get('page', 1))
else:
page = 1
provider_data['results'] = provider.search(search_query, page)
provider_data['time'] = float('%.2f' % (time() - start_time))
response.append(provider_data)
return Response(response)
示例7: get_votes
def get_votes(self, request, thread_pk, pk):
poll_pk = get_int_or_404(pk)
try:
thread = self.get_thread(request, thread_pk)
if thread.poll.pk != poll_pk:
raise Http404()
except Poll.DoesNotExist:
raise Http404()
allow_see_poll_votes(request.user, thread.poll)
choices = []
voters = {}
for choice in thread.poll.choices:
choice['voters'] = []
voters[choice['hash']] = choice['voters']
choices.append(choice)
queryset = thread.poll.pollvote_set.values(
'voter_id', 'voter_name', 'voter_slug', 'voted_on', 'choice_hash')
for voter in queryset.order_by('voter_name').iterator():
voters[voter['choice_hash']].append(PollVoteSerializer(voter).data)
return Response(choices)
示例8: test_invalid_inputs
def test_invalid_inputs(self):
"""get_int_or_404 raises Http404 for invalid values"""
INVALID_VALUES = (
None,
'',
'bob',
'1bob',
'b0b',
'bob123',
'12.321',
'.4',
'5.',
)
for value in INVALID_VALUES:
with self.assertRaises(Http404):
get_int_or_404(value)
示例9: get_thread
def get_thread(self, request, pk, path_aware=False, read_aware=False, subscription_aware=False):
return self.thread(
request,
get_int_or_404(pk),
path_aware=path_aware,
read_aware=read_aware,
subscription_aware=subscription_aware,
)
示例10: get_thread
def get_thread(self, request, pk, read_aware=True, subscription_aware=True, select_for_update=False):
return self.thread(
request,
get_int_or_404(pk),
None,
read_aware,
subscription_aware,
select_for_update
)
示例11: posts
def posts(self, request, pk=None):
profile = self.get_user(pk)
page = get_int_or_404(request.query_params.get('page', 0))
if page == 1:
page = 0 # api allows explicit first page
feed = UserPosts(request, profile, page)
return Response(feed.get_frontend_context())
示例12: test_valid_inputs
def test_valid_inputs(self):
"""get_int_or_404 returns int for valid values"""
VALID_VALUES = (
('0', 0),
('123', 123),
('000123', 123),
('1', 1),
)
for value, result in VALID_VALUES:
self.assertEqual(get_int_or_404(value), result)
示例13: get
def get(self, request, list_type=None, **kwargs):
page = get_int_or_404(request.GET.get('page', 0))
category = self.get_category(request, **kwargs)
threads = self.get_threads(request, category, list_type, page)
frontend_context = self.get_frontend_context(request, category, threads)
request.frontend_context.update(frontend_context)
template_context = self.get_template_context(request, category, threads)
return render(request, self.template_name, template_context)
示例14: patch_top_category
def patch_top_category(request, thread, value):
category_pk = get_int_or_404(value)
root_category = get_object_or_404(
Category.objects.all_categories(include_root=True),
pk=category_pk
)
categories = list(Category.objects.all_categories().filter(
id__in=request.user.acl['visible_categories']
))
add_categories_to_items(root_category, categories, [thread])
return {'top_category': CategorySerializer(thread.top_category).data}
示例15: get_thread
def get_thread(self, user, thread_id):
thread = get_object_or_404(Thread.objects.select_related('category'),
id=get_int_or_404(thread_id),
category__tree_id=self.TREE_ID,
)
add_acl(user, thread.category)
add_acl(user, thread)
self.validate_thread_visible(user, thread)
return thread