Example #1
0
def product_add(request):
    form = ProductAddForm(request.POST, request.FILES)
    print(request.FILES)
    if form.is_valid():
        form.save()
        return JsonResponse({utils.KEY_STATUS: utils.STATUS_OKAY})
    else:
        return form_validation_error(form)
Example #2
0
def process_order_feedback(request, order_id):
    form = OrderFeedbackForm(request.POST)
    response = dict()
    response[utils.KEY_STATUS] = utils.STATUS_OKAY
    if form.is_valid():
        form.save_feedback(order_id)
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #3
0
def process_feedback(request):  # AJAX
    response = dict()
    form = FeedbackForm(request.POST)
    if form.is_valid():
        response[utils.KEY_STATUS] = utils.STATUS_OKAY
        customer = Customer.objects.get(user=request.user)
        form.save_feedback(customer)
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #4
0
def update_customer_profile(request):  # AJAX
    form = UpdateProfileForm(request.POST)
    response = dict()
    response[utils.KEY_STATUS] = utils.STATUS_OKAY
    if form.is_valid():
        customer = Customer.objects.get(
            pk=request.session[utils.SESSION_CUSTOMER_ID])  # type: Customer
        customer.user.first_name = form.cleaned_data['name']
        customer.phone = form.cleaned_data['phone']
        customer.save()
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #5
0
def process_registration(request):  # AJAX
    response = dict()
    form = RegistrationForm(request.POST)
    if form.is_valid():
        customer = Customer.create(email=form.cleaned_data.get("email"),
                                   phone=str(form.cleaned_data.get("phone")),
                                   password=str(
                                       form.cleaned_data.get("password")),
                                   firstname=form.cleaned_data.get("name"))
        response[utils.KEY_STATUS] = utils.STATUS_OKAY
        response[utils.KEY_NAME] = customer.user.first_name
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #6
0
def check_account_exists(request):  # AJAX
    """
    Request will be originated from login phase one.
    """
    response = dict()
    form = EnterEmailForm(request.POST)
    if form.is_valid():
        response[utils.KEY_STATUS] = utils.STATUS_OKAY
        response[utils.KEY_EMAIL] = form.cleaned_data['email']
        if Customer.objects.filter(email=form.cleaned_data['email']).exists():
            response[utils.KEY_EMAIL_REGISTERED] = True
        else:
            response[utils.KEY_EMAIL_REGISTERED] = False
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #7
0
def process_product_feedback(request, product_id: str):
    response = dict()
    response[utils.KEY_STATUS] = utils.STATUS_OKAY
    form = ProductFeedbackForm(request.POST)
    if form.is_valid():
        try:
            form.save_feedback(request.session[utils.SESSION_CUSTOMER_ID],
                               int(product_id))
        except IntegrityError:
            return JsonResponse({
                utils.KEY_STATUS:
                utils.STATUS_ERROR,
                utils.KEY_ERROR_MESSAGE:
                utils.ERROR_ALREADY_EXISTS
            })
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #8
0
def edit_address(request):  # AJAX
    form = AddressForm(request.POST)
    response = dict()
    response[utils.KEY_STATUS] = utils.STATUS_OKAY
    if form.is_valid():
        address_ = Address.objects.filter(
            name=form.cleaned_data['name'],
            customer_id=request.session[utils.SESSION_CUSTOMER_ID])
        if len(address_) > 0:  # TODO Convert to model method
            address = address_.first()  # type: Address
            address.address = form.cleaned_data['address']
            address.phone = form.cleaned_data['phone']
            address.landmark = form.cleaned_data['landmark']
            address.city = form.cleaned_data['city']
            address.state = form.cleaned_data['state']
            address.save()
            response[utils.KEY_ADDRESS_NAME] = address.name
        else:
            response[utils.KEY_STATUS] = utils.STATUS_ERROR
            response[utils.KEY_ERROR_MESSAGE] = utils.ERROR_ADDRESS_NOT_FOUND
        return JsonResponse(response)
    else:
        return form_validation_error(form)
Example #9
0
def process_address_add(request):  # AJAX
    response = dict()
    form = AddressForm(request.POST)
    response[utils.KEY_STATUS] = utils.STATUS_OKAY
    if form.is_valid():
        # TODO refactor it into a method in model
        address = Address(
            name=form.cleaned_data['name'],
            address=form.cleaned_data['address'],
            landmark=form.cleaned_data['landmark'],
            city=form.cleaned_data['city'],
            pincode=form.cleaned_data['pincode'],
            phone=form.cleaned_data['phone'],
            customer_id=request.session[utils.SESSION_CUSTOMER_ID])
        response[utils.KEY_ADDRESS_NAME] = address.name
        try:
            address.save()
        except IntegrityError:
            response[utils.KEY_STATUS] = utils.STATUS_ERROR
            response[utils.KEY_ERROR_MESSAGE] = utils.ERROR_ALREADY_EXISTS
            response[utils.KEY_FORM] = form.as_table()
        return JsonResponse(response)
    else:
        return form_validation_error(form)