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)
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
import os.path
|
||||
import re
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
from django.db import models
|
||||
import datetime
|
||||
import os.path
|
||||
import re
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Season(models.Model):
|
||||
@ -106,3 +111,41 @@ class Catalog(models.Model):
|
||||
'build_progress': self.build_progress,
|
||||
'pdf': self.pdf_url(),
|
||||
}
|
||||
|
||||
def update_metadata(self):
|
||||
"""Update meta properties from what's in 'data'"""
|
||||
if not self.data or len(self.data) < 1:
|
||||
log.warning('no data in update_metadata()')
|
||||
return
|
||||
|
||||
data = self.data
|
||||
|
||||
self.name = data.get('name', '(No name)')
|
||||
|
||||
self.season = Season.objects.get(id=data.get('season'))
|
||||
self.region = Region.objects.get(id=data.get('region'))
|
||||
|
||||
self.public = data.get('public', False)
|
||||
self.master = data.get('master', False)
|
||||
self.show_prices = data.get('show_prices', False)
|
||||
|
||||
# NOTE 'is_utility' in the json data is not exposed to the
|
||||
# django model or placed in a separate postgres column.
|
||||
|
||||
# reset until pdf is made
|
||||
self.build_progress = 0
|
||||
|
||||
# calculate some properties
|
||||
sections = 0
|
||||
pages = 0
|
||||
materials = 0
|
||||
for section in data.get('sections'):
|
||||
sections += 1
|
||||
for page in section.get('pages'):
|
||||
pages += 1
|
||||
for block in page:
|
||||
materials += len(block.get('ids', []))
|
||||
|
||||
self.sections = sections
|
||||
self.pages = pages
|
||||
self.materials = materials
|
||||
|
||||
@ -21,7 +21,7 @@ from lazysignup.views import convert
|
||||
|
||||
from dashboard.views import dashboard
|
||||
from cataloglist.views import cataloglist, my_catalogs, public_catalogs
|
||||
from catalogedit.views import catalogedit, get_catalog
|
||||
from catalogedit.views import catalogedit, get_catalog, save_catalog
|
||||
|
||||
from .forms import UserCreationForm
|
||||
from .views import login_guest, lazy_convert_done
|
||||
@ -38,7 +38,8 @@ urlpatterns = [
|
||||
|
||||
path('catalog/new', catalogedit, name='catalogedit'),
|
||||
path('catalog/edit/<int:id>', catalogedit, name='catalogedit'),
|
||||
path('api/v1/catalogs/id/<int:id>', get_catalog, name='catalog'),
|
||||
path('api/v1/catalogs/id/<int:id>', get_catalog, name='get_catalog'),
|
||||
path('api/v1/catalogs/save', save_catalog, name='save_catalog'),
|
||||
|
||||
path('admin/', admin.site.urls),
|
||||
path("account/", include("account.urls")),
|
||||
|
||||
Reference in New Issue
Block a user