123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from django.views.decorators.http import require_POST, require_GET
- from jet.forms import AddBookmarkForm, RemoveBookmarkForm, ToggleApplicationPinForm, ModelLookupForm
- from jet.models import Bookmark
- from jet.utils import JsonResponse
- @require_POST
- def add_bookmark_view(request):
- result = {'error': False}
- form = AddBookmarkForm(request, request.POST)
- if form.is_valid():
- bookmark = form.save()
- result['id'] = bookmark.pk
- else:
- result['error'] = True
- return JsonResponse(result)
- @require_POST
- def remove_bookmark_view(request):
- result = {'error': False}
- try:
- instance = Bookmark.objects.get(pk=request.POST.get('id'))
- form = RemoveBookmarkForm(request, request.POST, instance=instance)
- if form.is_valid():
- form.save()
- else:
- result['error'] = True
- except Bookmark.DoesNotExist:
- result['error'] = True
- return JsonResponse(result)
- @require_POST
- def toggle_application_pin_view(request):
- result = {'error': False}
- form = ToggleApplicationPinForm(request, request.POST)
- if form.is_valid():
- pinned = form.save()
- result['pinned'] = pinned
- else:
- result['error'] = True
- return JsonResponse(result)
- @require_GET
- def model_lookup_view(request):
- result = {'error': False}
- form = ModelLookupForm(request.GET)
- if form.is_valid():
- items, total = form.lookup()
- result['items'] = items
- result['total'] = total
- else:
- result['error'] = True
- return JsonResponse(result)
|