views.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from django.views.decorators.http import require_POST, require_GET
  2. from jet.forms import AddBookmarkForm, RemoveBookmarkForm, ToggleApplicationPinForm, ModelLookupForm
  3. from jet.models import Bookmark
  4. from jet.utils import JsonResponse
  5. @require_POST
  6. def add_bookmark_view(request):
  7. result = {'error': False}
  8. form = AddBookmarkForm(request, request.POST)
  9. if form.is_valid():
  10. bookmark = form.save()
  11. result['id'] = bookmark.pk
  12. else:
  13. result['error'] = True
  14. return JsonResponse(result)
  15. @require_POST
  16. def remove_bookmark_view(request):
  17. result = {'error': False}
  18. try:
  19. instance = Bookmark.objects.get(pk=request.POST.get('id'))
  20. form = RemoveBookmarkForm(request, request.POST, instance=instance)
  21. if form.is_valid():
  22. form.save()
  23. else:
  24. result['error'] = True
  25. except Bookmark.DoesNotExist:
  26. result['error'] = True
  27. return JsonResponse(result)
  28. @require_POST
  29. def toggle_application_pin_view(request):
  30. result = {'error': False}
  31. form = ToggleApplicationPinForm(request, request.POST)
  32. if form.is_valid():
  33. pinned = form.save()
  34. result['pinned'] = pinned
  35. else:
  36. result['error'] = True
  37. return JsonResponse(result)
  38. @require_GET
  39. def model_lookup_view(request):
  40. result = {'error': False}
  41. form = ModelLookupForm(request.GET)
  42. if form.is_valid():
  43. items, total = form.lookup()
  44. result['items'] = items
  45. result['total'] = total
  46. else:
  47. result['error'] = True
  48. return JsonResponse(result)