Skip to content

Commit

Permalink
upload form for products
Browse files Browse the repository at this point in the history
  • Loading branch information
dinesh committed Apr 21, 2024
1 parent be32039 commit 233a0ce
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 4 deletions.
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified main/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file modified main/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file modified main/__pycache__/views.cpython-312.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Product(models.Model):
user_name = models.ForeignKey(users,on_delete=models.CASCADE)
product_id = models.CharField(max_length=30,primary_key=True)
product_name = models.TextField()
product_description = models.TextField()
product_description = models.TextField(default=None,null=True)
product_price = models.IntegerField()
current_bid = models.IntegerField()
def __str__(self) -> str:
Expand Down
3 changes: 3 additions & 0 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
path('auctions/',views.auctions,name="auctions"),
path('auction/<str:auction_name>',views.auction,name="auction"),
path('user/',views.user,name="user"),
path('user/upload',views.product_upload,name="product_upload"),
path('user/my_products',views.my_products,name="my_products"),
path('user/upload/done',views.product_upload_done,name="product_uploaded"),
path('user/<str:user_name>',views.user_inspect,name="user_inspect"),
path('user/login/',views.user_login),
path('user/register/',views.user_register),
Expand Down
62 changes: 61 additions & 1 deletion main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,64 @@ def error(request):
context = {
'message' : 'user not found'
}
return HttpResponse(template.render(context,request))
return HttpResponse(template.render(context,request))

def product_upload(request):
template = loader.get_template('upload.html')
context = {
}
return HttpResponse(template.render(context,request))

import random
import string

def product_id_generator(size=10, chars=string.ascii_uppercase + string.digits + string.ascii_lowercase):
product_id = ''.join(random.choice(chars) for _ in range(size))
try:
product = models.Product.objects.get(product_id=product_id)
except:
product = None
if product == None:
product_id = product_id
return product_id
else:
product_id_generator()

def product_upload_done(request):
if request.method == 'POST':
user_name = models.users.objects.get(user_name=settings.USER_NAME)
product_name = request.POST.get('product_name')
product_description = request.POST.get('product_description')
product_price = request.POST.get('product_price')
product_image = request.FILES.get('product_image')
product_id = product_id_generator()

product = models.Product(
user_name = user_name,
product_id = product_id,
product_name = product_name,
product_description = product_description,
product_price = product_price,
current_bid = product_price,
)
product_images = models.Product_images(
product_id = product,
product_images = product_image,
)
product.save()
product_images.save()
return redirect('/user/my_products')
else:
return HttpResponse('error')

def my_products(request):
template = loader.get_template('my_products.html')
products = models.Product.objects.filter(user_name=settings.USER_NAME).all()
total_set = {}
for product in products:
image = models.Product_images.objects.filter(product_id=product.product_id).first()
total_set[product] = image
context = {
'data' : total_set,
}
return HttpResponse(template.render(context,request))
Binary file added media/product_images/icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added static/js/description.js
Empty file.
17 changes: 17 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,21 @@ button:hover {
.user_bid_activity a:hover {
text-transform: capitalize;
padding: 2px;
}

.product_upload input {
padding: 10px;
margin: 5px;
}
.product_upload input[type="submit"] {
margin: 2px;
text-align: center;
color: #fff;
background-color:#000;
border-radius: 20px;
}
.product_upload input[type="submit"]:hover {
background-color: #fff;
color: #252525;
box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.2);
}
3 changes: 2 additions & 1 deletion templates/auction.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3>{{product_id}}</h3>
<h1>User_name</h1>
<a href="#"><h3>{{data.user_name}}</h3></a>
</div>
<button type="button" class="info_button">Description</button>
<button type="button" class="info_button" id="description">Description</button>
</div>
<div class="bid_box">
<div class="current_bid_details">
Expand All @@ -45,5 +45,6 @@ <h4>{{data.for_price}}</h4>
{% endfor %}
</div>
{% endblock content %}
<script src={% static "js/description.js" %}>
</body>
</html>
18 changes: 18 additions & 0 deletions templates/my_products.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block title %}
Auction-site
{% endblock title %}

{% block content %}
<div class="product_results">
{% for product, images in data.items %}
<a href="/auction/{{product.product_id}}">
<div class="product">
<img src='/media/{{images.product_images}}'>
<h3>{{product.current_bid}}</h3>
</div>
</a>
{% endfor %}
</div>
{% endblock content %}
</body>
21 changes: 21 additions & 0 deletions templates/upload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block title %}
Auction-site
{% endblock title %}

{% block content %}
<div class="container-center">
<div class="container">
<h1>upload your product</h1>
<form class="product_upload" method="POST" action="/user/upload/done" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="product_image" id="image" placeholder="image" required><br>
<input type="text" name="product_name" placeholder="name for product" required><br>
<input type="text" name="product_description" placeholder="Description" required><br>
<input type="text" name="product_price" placeholder="price" required><br>
<input type="submit" value="Upload">
</form>
</div>
</div>
{% endblock content %}
</body>
3 changes: 2 additions & 1 deletion templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ <h2>{{user.full_name}}</h2>
<div class="main">
<ul>
<a href="/"><li>Home</li></a><br>
<a href=""><li>My Products</li></a>
<a href="/user/my_products"><li>My Products</li></a>
<a href="/user/upload"><li>upload</li>
</ul>
</div>
{% endblock content %}
Expand Down

0 comments on commit 233a0ce

Please sign in to comment.