def set_items_count(request): params, error = GetParams(request, 'POST', ('item_id', 'basket_id', 'count')) if error: return error try: basket = models.Basket.objects.get(id=params.basket_id) except models.Basket.DoesNotExist: return ApiResponse({'status': 'Basket has not been found.'}, 404) try: item = content.models.WorkItem.objects.get(id=params.item_id) except content.models.WorkItem.DoesNotExist: return ApiResponse({'status': 'Item has not been found.'}, 404) try: item_group = basket.item_groups.get(item=item, basket=basket) except models.ItemGroup.DoesNotExist: return ApiResponse( {'status': 'Item has not been found in the basket.'}, 404) item_group.count = int(params.count) item_group.save() return ApiResponse({'status': 'item has been updated'})
def add_item(request): params, error = GetParams(request, 'POST', ('item_id', )) if error: return error basket_id = request.POST.get('basket_id') if basket_id: try: basket = models.Basket.objects.get(id=basket_id) except models.Basket.DoesNotExist: return ApiResponse({'status': 'Basket has not been found.'}, 404) else: basket = models.Basket.objects.create() basket.save() try: item = content.models.WorkItem.objects.get(id=params.item_id) except content.models.WorkItem.DoesNotExist: return ApiResponse({'status': 'Item has not been found.'}, 404) try: item_group = basket.item_groups.get(item=item, basket=basket) item_group.count += 1 except models.ItemGroup.DoesNotExist: item_group = models.ItemGroup.objects.create(item=item, count=1, basket=basket) item_group.save() return ApiResponse({'basket_id': str(basket.id)})
def make_order(request): params, error = GetParams( request, 'POST', ('name', 'phone', 'email', 'message', 'basket_id')) if error: return error try: basket = models.Basket.objects.get(id=params.basket_id) except models.Basket.DoesNotExist: return ApiResponse({'status': 'Basket has not been found.'}, 404) item_groups = basket.item_groups.all() total_price = 0 for item_group in item_groups: total_price += int(item_group.item.prise) * item_group.count serializer = serializers.ItemGroupSerializer(item_groups, many=True) order = models.Order.objects.create(name=params.name, email=params.email, phone=params.phone, message=params.message, data=json.dumps(serializer.data), total_price=total_price, basket=basket) order.save() order.make_pretty_data() for item_group in basket.item_groups.all()[::-1]: item_group.delete() return ApiResponse({'status': 'order has been saved.'})
def content_provider(request, model_name): if model_name in MODEL_NAMES: cls = eval('models.' + model_name) serializer_cls = eval('serializers.' + model_name + 'Serializer') objects = cls.objects.all() serializer = serializer_cls(objects, many=True) return ApiResponse(serializer.data) return ApiResponse({'status': 'model has not been found'}, 404)
def items(request): params, error = GetParams(request, 'GET', ('basket_id', )) if error: return error try: basket = models.Basket.objects.get(id=params.basket_id) except models.Basket.DoesNotExist: return ApiResponse({'status': 'Basket has not been found.'}, 404) serializer = serializers.ItemGroupSerializer( basket.item_groups.all().order_by('dt'), many=True) return ApiResponse(serializer.data)
def get_state(request): if request.method != 'GET': return ApiResponse({ 'status': 'HTTP method must be GET only' }, 403) workItems = models.WorkItem.objects.all().order_by('sort_index') workItemSerializer = serializers.WorkItemSerializer(workItems, many=True) categories = [c.name for c in models.WorkItemCategory.objects.all()] contactInfoList = models.ContactInformation.objects.all() if len(contactInfoList) > 0: contactInfo = contactInfoList[0] else: contactInfo = models.ContactInformation.objects.create() contactInfoSerializer = serializers.ContactInfoSerializer(contactInfo) sliderItems = models.SliderImage.objects.filter(shown=True) sliderItemsSerializer = serializers.SliderItemSerializer(sliderItems, many=True) serviceItems = models.ServiceItem.objects.all() if len(serviceItems) == 0: data_initialization.init_services() serviceItems = models.ServiceItem.objects.all() serviceItemsData = [] for item in serviceItems: try: serviceItemsData.append(json.loads(item.json_data)) except: pass videos = models.Video.objects.all().order_by('sort_index') videosSerializer = serializers.VideoSerializer(videos, many=True) images = models.GalleryImage.objects.all().order_by('sort_index') gallerySerializer = serializers.GallerSeriyalizer(images, many=True) return ApiResponse({ 'workItems': workItemSerializer.data, 'categories': categories, 'contactInfo': contactInfoSerializer.data, 'sliderItems': sliderItemsSerializer.data, 'serviceItems': serviceItemsData, 'videos': videosSerializer.data, 'gallery': gallerySerializer.data, })
def add_request(request): params, error = GetParams(request, 'POST', ( 'name', 'email', 'phone', 'message', )) if error: return error if 5: request = models.Request.objects.create(name=params.name, email=params.email, phone=params.phone, message=params.message, dt=timezone.now()) request.save() return ApiResponse({'status': 'saved'}) else: pass return ApiResponse({'status': 'error with creating a request'}, 500)