Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified olx/db.sqlite3
Binary file not shown.
1 change: 1 addition & 0 deletions olx/olx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'products',
'rest_framework'
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions olx/olx/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path("", include('products.urls')),
path("api/", include('products.api.urls')),
]

if settings.DEBUG is True:
Expand Down
14 changes: 14 additions & 0 deletions olx/products/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db.models import fields
from rest_framework import serializers
from products.models import Product, Owner, Image

class ProductSerializer(serializers.ModelSerializer):
Images = serializers.StringRelatedField(many=True)
class Meta:
model = Product
fields = "__all__"

class OwnerSerializer(serializers.ModelSerializer):
class Meta:
model = Owner
fields = "__all__"
6 changes: 6 additions & 0 deletions olx/products/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from products.api.views import product_list_view

urlpatterns = [
path("products/", product_list_view, name="product-list")
]
27 changes: 27 additions & 0 deletions olx/products/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.relations import method_overridden
from rest_framework.response import Response
from rest_framework.serializers import Serializer

from products.models import Product
from products.api.serializers import ProductSerializer

@api_view(["GET", "POST"])
def product_list_view(request):
if request.method == "GET":
products = Product.objects.filter(sold=False)
serializer_context = {
'request': request,
}

serializer = ProductSerializer(products, many=True, context=serializer_context)
return Response(serializer.data)
elif request.method == "POST":
# To Do: Add serializer for image
serializer = ProductSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
68 changes: 68 additions & 0 deletions olx/products/migrations/0002_auto_20210703_1331.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Generated by Django 3.2.4 on 2021-07-03 13:31

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('products', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='owner',
name='profilePicture',
field=models.ImageField(blank=True, null=True, upload_to=''),
),
migrations.AddField(
model_name='owner',
name='rating',
field=models.FloatField(blank=True, default=0.0),
),
migrations.AddField(
model_name='owner',
name='rollNo',
field=models.CharField(default=200101001, max_length=9),
preserve_default=False,
),
migrations.AddField(
model_name='product',
name='company',
field=models.CharField(blank=True, max_length=120),
),
migrations.AddField(
model_name='product',
name='dateCreated',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='product',
name='lastModified',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='product',
name='quantity',
field=models.IntegerField(default=1),
preserve_default=False,
),
migrations.AddField(
model_name='product',
name='sold',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='image',
name='images',
field=models.ImageField(blank=True, null=True, upload_to='images'),
),
migrations.AlterField(
model_name='image',
name='product',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='Images', to='products.product'),
),
]
4 changes: 3 additions & 1 deletion olx/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ class Image(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE,related_name="Images")
images = models.ImageField(blank=True, null=True, upload_to=f'images')
def __str__(self):
return self.product.name + " Image"
return self.images.name

@property
def ImageURL(self):
try:
url = self.image.url
if url=='':
url='images/default.jpeg'
except:
url = ''
return url
Expand Down
6 changes: 5 additions & 1 deletion olx/products/templates/product_details.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% extends "base.html" %}
{% block body_block %}
{% for img in prod.Images.all %}
<img src= "{{ img.images.url }}" />
{% if prod.Images.all.0.images.url != '' %}
<img src= "{{ img.images.url }}" />
{% else %}
<img src= "/uploads/images/default.jpeg" />
{% endif %}
{% endfor %}
<p>{{ prod.price }}</p>
<p>{{ prod.name }}</p>
Expand Down
6 changes: 5 additions & 1 deletion olx/products/templates/product_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{% block body_block %}
{% for prod in product_list %}
<a href = "{% url "products:details" prod.pk %}">
<img src= "{{ prod.Images.all.0.images.url }}" />
{% if prod.Images.all.0.images.url != '' %}
<img src= "{{ prod.Images.all.0.images.url }}" />
{% else %}
<img src= "uploads/images/default.jpeg" />
{% endif %}
<p>{{ prod.price }}</p>
<p>{{ prod.name }}</p>
<p>{{ prod.company }}</p>
Expand Down
Binary file removed olx/uploads/images/Screenshot_2021-06-03_220904.png
Binary file not shown.
Binary file not shown.
Binary file added olx/uploads/images/angry.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added olx/uploads/images/bird.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added olx/uploads/images/camera.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added olx/uploads/images/default.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.