admin.py 2.17 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
from django.contrib import admin
from outils.common_imports import *
from outils.for_view_imports import *

from .models import Shelf, Shelfs
from members.models import CagetteUser
from inventory.models import CagetteInventory

@never_cache
def index(request):
    template = loader.get_template('shelfs/admin.html')
    context = {'title': 'Gestion des rayons',
                'ADMINS': settings.ADMIN_IDS}
    response = HttpResponse(template.render(context, request))
    return response

def create(request):
    result = {}
    is_connected_user = CagetteUser.are_credentials_ok(request)
    if is_connected_user is True:
        try:
            result = Shelf().create(request.POST)
        except Exception as e:
            result['error'] = str(e)
    else:
        result['error'] = "Forbidden"
    return JsonResponse({'res': result})

def update(request):
    result = {}
    is_connected_user = CagetteUser.are_credentials_ok(request)
    if is_connected_user is True:
        try:
            result = Shelf().update(request.POST)
        except Exception as e:
            result['error'] = str(e)
    else:
        result['error'] = "Forbidden"
    return JsonResponse({'res': result})

def delete(request):
    result = {}
    is_connected_user = CagetteUser.are_credentials_ok(request)
    if is_connected_user is True:
        try:
            result = Shelf().delete(request.POST)
        except Exception as e:
            result['error'] = str(e)
    else:
        result['error'] = "Forbidden"
    return JsonResponse({'res': result})

def add_products(request):
    import json
    result = {}
    is_connected_user = CagetteUser.are_credentials_ok(request)
    if is_connected_user is True:
        try:
            id = int(request.POST.get('shelf_id'))
            barcodes = json.loads(request.POST.get('bc'))
61 62 63 64 65
            m = Shelf(id)
            result = m.add_products_by_barcodes(barcodes)

            # Update shelf last product added date 
            result["update_last_product_added_date"] = m.update_last_product_added_date()
Administrator committed
66 67 68 69 70
        except Exception as e:
            result['error'] = str(e)
    else:
        result['error'] = "Forbidden"
    return JsonResponse({'res': result})