Пример #1
0
def get_messages_from_flow_admin(request):
    """GET requests for admin to get messages from message flow"""
    admin = get_admin_from_request(request)
    if admin is None:
        return HttpResponse("Admin authentication failed", status=401)
    flow_id = request.GET.get("flow_id")
    if flow_id is None:
        return HttpResponse("Flow id not given", status=400)
    try:
        flow = MessageFlowAdmin.objects.get(id=flow_id)
    except:
        return HttpResponse("Flow with id does not exist", status=400)
    #if flow.admin!=admin:
    #    return HttpResponse("Message does not belong to this admin", status=401)
    messages = MessageHistoryAdmin.objects.filter(flow=flow).order_by("date_sent")
    data = []
    for message in messages:
        data.append({
            "sender": "self" if message.from_admin else "other",
            "admin": flow.admin.username,
            "vendor_name": flow.vendor.user.user.first_name,
            "message": message.message,
            "date_sent": message.date_sent,
        })
    flow.admin_read = True
    flow.save()
    return JsonResponse(data, safe=False)
Пример #2
0
def admin_message_to_vendor(request):
    """Admin sending message to vendor"""
    admin = get_admin_from_request(request)
    if admin is None:
        return HttpResponse("Admin authentication failed", status=401)
    flow_id = request.POST.get("flow_id")
    if flow_id is None:
        return HttpResponse("Flow id is not given", status=400)
    try:
        flow = MessageFlowAdmin.objects.get(id=flow_id)
    except:
        return HttpResponse("Flow does not exist", status=400)
    #if flow.admin != admin:
    #    return HttpResponse("Admin authentication failed", status=401)
    message_text = request.POST.get("message")
    if message_text is None:
        return HttpResponse("Message is not given", status=400)
    elif len(message_text) > 200:
        return HttpResponse("Message is too long (LIMIT=200 char)", status=400)
    message = MessageHistoryAdmin.objects.create(
            flow=flow,
            from_admin=True,
            message=message_text
        )
    message.save()
    flow.vendor_read = False
    flow.save()
    return HttpResponse("Message sent successfully!")
Пример #3
0
def get_admin_flows(request):
    """GET requests to get MessageFlows of Admin requestsing"""
    admin = get_admin_from_request(request)
    if admin is None:
        return HttpResponse("Admin authentication failed", status=401)
    flows = MessageFlowAdmin.objects.filter(admin=admin)
    info = []
    for flow in flows:
        if flow.product is None:
            context = "order"
            object_id = flow.order.pk
            product_name = flow.order.product.name
            customer = flow.order.customer.user.user.username
        else:
            context = "product"
            object_id = flow.product.pk
            product_name = flow.product.name
            customer = None
        info.append({
            "id": flow.pk,
            "notify": True if not flow.admin_read else False,
            "context": context,
            "object_id": object_id,
            "product": product_name,
            "customer": customer,
            "vendor_name": flow.vendor.user.user.first_name,
            "type": "admin"
        })
    return JsonResponse(info,safe=False)
Пример #4
0
def get_products_waiting_verification(request):
    """Gets all products waiting verification."""
    admin = get_admin_from_request(request)
    if (admin is None):
        return HttpResponse("Admin authentication failed", status=401)
    products = Product.objects.filter(is_verified=False)
    return JsonResponse(SearchHelper.prepare_products(products), safe=False)
Пример #5
0
def delete_comment(request):
    """Deletes the given comment when DELETE request is made."""
    admin = get_admin_from_request(request)
    if (admin is None):
        return HttpResponse("Admin authentication failed", status=401)
    try:
        comment_id = int(request.POST["comment_id"])
    except (KeyError, ValueError):
        return HttpResponse("Comment id (comment_id) not given or invalid",
                            status=400)
    comment = Comment.objects.filter(Q(id=comment_id))
    comment.delete()
    return HttpResponse("success")
Пример #6
0
def ban_user(request):
    """Bans given user."""
    admin = get_admin_from_request(request)
    if (admin is None):
        return HttpResponse("Admin authentication failed", status=401)
    try:
        username = request.POST["username"]
    except (KeyError, ValueError):
        return HttpResponse("User name (username) not given or invalid",
                            status=400)
    try:
        ruser = RegisteredUser.objects.get(user__username=username)
    except Exception:
        return HttpResponse("There is no such user.", status=400)
    ruser.is_banned = True
    ruser.save()
    return HttpResponse("success")
Пример #7
0
def verify_product(request):
    """Verifies product with given parameters when POST request is made."""
    admin = get_admin_from_request(request)
    if (admin is None):
        return HttpResponse("Admin authentication failed", status=401)
    try:
        product_id = request.POST["id"]
    except KeyError:
        return HttpResponse("Product id not specified", status=400)
    try:
        product = Product.objects.get(id=product_id)
    except Exception:
        return HttpResponse("There is no such product with the id", status=400)
    if product.is_verified == False:
        notif.insert_product_verified(product.vendor.user, product.name,
                                      product.id)
    product.is_verified = True
    product.save()
    return HttpResponse("Successfully verified product")
Пример #8
0
def delete_product(request):
    """Delete product with given parameters when DELETE request is made."""
    admin = get_admin_from_request(request)
    if (admin is None):
        return HttpResponse("Admin authentication failed", status=401)
    try:
        product_id = request.POST["id"]
    except KeyError:
        return HttpResponse("Product id not specified", status=400)
    try:
        product = Product.objects.get(id=product_id)
    except Exception:
        return HttpResponse("There is no such product with the id", status=400)
    images = Image.objects.filter(product=product)
    if len(images) > 0:
        files = [
            os.path.join("static/images", str(image.photo)) for image in images
        ]
        for f in files:
            os.remove(f)
    product.delete()
    return HttpResponse("success")