diff --git a/olx/db.sqlite3 b/olx/db.sqlite3 index b03b53b..62c9786 100644 Binary files a/olx/db.sqlite3 and b/olx/db.sqlite3 differ diff --git a/olx/olx/settings.py b/olx/olx/settings.py index 4419bdd..484bab5 100644 --- a/olx/olx/settings.py +++ b/olx/olx/settings.py @@ -39,6 +39,7 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'products', + 'rest_framework' ] MIDDLEWARE = [ diff --git a/olx/olx/urls.py b/olx/olx/urls.py index d6048b2..3f829f6 100644 --- a/olx/olx/urls.py +++ b/olx/olx/urls.py @@ -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: diff --git a/olx/products/api/serializers.py b/olx/products/api/serializers.py new file mode 100644 index 0000000..4f5b3b5 --- /dev/null +++ b/olx/products/api/serializers.py @@ -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__" \ No newline at end of file diff --git a/olx/products/api/urls.py b/olx/products/api/urls.py new file mode 100644 index 0000000..9731923 --- /dev/null +++ b/olx/products/api/urls.py @@ -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") +] \ No newline at end of file diff --git a/olx/products/api/views.py b/olx/products/api/views.py new file mode 100644 index 0000000..8997c6b --- /dev/null +++ b/olx/products/api/views.py @@ -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) diff --git a/olx/products/migrations/0002_auto_20210703_1331.py b/olx/products/migrations/0002_auto_20210703_1331.py new file mode 100644 index 0000000..ef854e2 --- /dev/null +++ b/olx/products/migrations/0002_auto_20210703_1331.py @@ -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'), + ), + ] diff --git a/olx/products/models.py b/olx/products/models.py index 149d403..6f72457 100644 --- a/olx/products/models.py +++ b/olx/products/models.py @@ -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 diff --git a/olx/products/templates/product_details.html b/olx/products/templates/product_details.html index 2afc080..8e85292 100644 --- a/olx/products/templates/product_details.html +++ b/olx/products/templates/product_details.html @@ -1,7 +1,11 @@ {% extends "base.html" %} {% block body_block %} {% for img in prod.Images.all %} - + {% if prod.Images.all.0.images.url != '' %} + + {% else %} + + {% endif %} {% endfor %}

{{ prod.price }}

{{ prod.name }}

diff --git a/olx/products/templates/product_list.html b/olx/products/templates/product_list.html index 7ee9243..481bb1e 100644 --- a/olx/products/templates/product_list.html +++ b/olx/products/templates/product_list.html @@ -2,7 +2,11 @@ {% block body_block %} {% for prod in product_list %} - + {% if prod.Images.all.0.images.url != '' %} + + {% else %} + + {% endif %}

{{ prod.price }}

{{ prod.name }}

{{ prod.company }}

diff --git a/olx/uploads/images/Screenshot_2021-06-03_220904.png b/olx/uploads/images/Screenshot_2021-06-03_220904.png deleted file mode 100644 index 32ffc73..0000000 Binary files a/olx/uploads/images/Screenshot_2021-06-03_220904.png and /dev/null differ diff --git a/olx/uploads/images/Screenshot_from_2021-05-17_22-58-35.png b/olx/uploads/images/Screenshot_from_2021-05-17_22-58-35.png deleted file mode 100644 index 2256f9d..0000000 Binary files a/olx/uploads/images/Screenshot_from_2021-05-17_22-58-35.png and /dev/null differ diff --git a/olx/uploads/images/angry.jpg b/olx/uploads/images/angry.jpg new file mode 100644 index 0000000..e40bec2 Binary files /dev/null and b/olx/uploads/images/angry.jpg differ diff --git a/olx/uploads/images/bird.jpg b/olx/uploads/images/bird.jpg new file mode 100644 index 0000000..392ea5e Binary files /dev/null and b/olx/uploads/images/bird.jpg differ diff --git a/olx/uploads/images/camera.jpeg b/olx/uploads/images/camera.jpeg new file mode 100644 index 0000000..05a5814 Binary files /dev/null and b/olx/uploads/images/camera.jpeg differ diff --git a/olx/uploads/images/default.jpeg b/olx/uploads/images/default.jpeg new file mode 100644 index 0000000..4a989e0 Binary files /dev/null and b/olx/uploads/images/default.jpeg differ