backend: add saving catalog
This commit is contained in:
@ -3,12 +3,19 @@ from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext as _
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.decorators.http import require_http_methods
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
from lazysignup.decorators import allow_lazy_user
|
||||
from account.decorators import login_required
|
||||
|
||||
from procat2.models import Catalog, Season, Region
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@login_required
|
||||
def catalogedit(request, id=0):
|
||||
@ -26,3 +33,36 @@ def catalogedit(request, id=0):
|
||||
def get_catalog(request, id):
|
||||
cat = get_object_or_404(Catalog, id=id)
|
||||
return JsonResponse(cat.data, safe=False)
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
@login_required
|
||||
@require_http_methods(["POST"])
|
||||
def save_catalog(request):
|
||||
body = request.body
|
||||
if not body or len(body) < 1:
|
||||
return HttpResponse('Bad request: no data', status=400)
|
||||
|
||||
data = json.loads(body.decode('utf-8'))
|
||||
|
||||
id = data.get('id')
|
||||
|
||||
if id:
|
||||
log.debug('saving existing catalog, id {}'.format(id))
|
||||
cat = get_object_or_404(Catalog, id=id, owner=request.user)
|
||||
else:
|
||||
season = Season.objects.get(id=data.get('season'))
|
||||
region = Region.objects.get(id=data.get('region'))
|
||||
cat = Catalog(owner=request.user, season=season, region=region)
|
||||
log.debug('saving new catalog, season {}, region {}'.format(season, region))
|
||||
|
||||
cat.data = data
|
||||
cat.update_metadata()
|
||||
cat.save()
|
||||
|
||||
if not id:
|
||||
# new catalog: update id in json and resave
|
||||
cat.data['id'] = cat.id
|
||||
cat.save()
|
||||
|
||||
return JsonResponse(cat.data, safe=False)
|
||||
|
||||
Reference in New Issue
Block a user