views.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.update({
  12. 'id': bookmark.pk,
  13. 'title': bookmark.title,
  14. 'url': bookmark.url
  15. })
  16. else:
  17. result['error'] = True
  18. return JsonResponse(result)
  19. @require_POST
  20. def remove_bookmark_view(request):
  21. result = {'error': False}
  22. try:
  23. instance = Bookmark.objects.get(pk=request.POST.get('id'))
  24. form = RemoveBookmarkForm(request, request.POST, instance=instance)
  25. if form.is_valid():
  26. form.save()
  27. else:
  28. result['error'] = True
  29. except Bookmark.DoesNotExist:
  30. result['error'] = True
  31. return JsonResponse(result)
  32. @require_POST
  33. def toggle_application_pin_view(request):
  34. result = {'error': False}
  35. form = ToggleApplicationPinForm(request, request.POST)
  36. if form.is_valid():
  37. pinned = form.save()
  38. result['pinned'] = pinned
  39. else:
  40. result['error'] = True
  41. return JsonResponse(result)
  42. @require_GET
  43. def model_lookup_view(request):
  44. result = {'error': False}
  45. form = ModelLookupForm(request, request.GET)
  46. if form.is_valid():
  47. items, total = form.lookup()
  48. result['items'] = items
  49. result['total'] = total
  50. else:
  51. result['error'] = True
  52. return JsonResponse(result)