Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lostandfound #510

Merged
merged 10 commits into from
Dec 10, 2023
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

VAPID_PRIV_KEY = ""
FCM_SERVER_KEY = ""
MESSI_ACCESS_TOKEN = ""
MESSI_ACCESS_TOKEN = "Tolm_fRDkfoN5WMU4oUXWxNwmn1E0MmYlbeh1LA29cU="

# Change this to LOGGING to enable SQLite logging
NO_LOGGING = {
Expand Down
Empty file added lostandfound/__init__.py
Empty file.
86 changes: 86 additions & 0 deletions lostandfound/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from django.contrib import admin
from django.http.response import HttpResponse
from django.utils.html import format_html

# Register your models here.
from lostandfound.models import ProductFound
from django import forms
from users.models import UserProfile


class UserProfileAdmin(admin.ModelAdmin):
search_fields = ["ldap_id", "name", "roll_no"]
list_display = ["ldap_id", "name", "roll_no"]

def change_view(
self, request, object_id, form_url="", extra_context=None
) -> HttpResponse:
class CustomChangeForm(forms.ModelForm):
"""Modify the change form to only show the required fields."""

class Meta:
model = UserProfile
fields = ["ldap_id", "name", "roll_no"]

self.form = CustomChangeForm
return super().change_view(request, object_id, form_url, extra_context)


class ProductFoundAdmin(admin.ModelAdmin):
list_display = ["name", "product_image_display", "category", "found_at", "claimed"]
search_fields = [
"name",
"description",
"category",
"found_at",
"claimed",
"contact_details",
"time_of_creation",
"claimed_by",
]

autocomplete_fields = ["claimed_by"]
# inlines = [UserProfileInline]

def product_image_display(self, obj):
images = obj.product_image.split(",")
return format_html(
'<div style = "width :200px margin-left:50px"><img src="{}"style="max-height: 150px; /></div>'.format(
images[0]
)
)

def change_view(self, request, object_id, form_url="", extra_context=None):
# self.inlines = [ImageInline]
class CustomChangeForm(forms.ModelForm):
class Meta:
model = ProductFound
fields = [
"name",
"description",
"category",
"found_at",
"claimed",
"contact_details",
"claimed_by",
"product_image1",
"product_image2",
"product_image3",
]

self.form = CustomChangeForm
return super().change_view(request, object_id, form_url, extra_context)

def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)

class CSOAdminSite(admin.AdminSite):
site_header = "CSO Admin"
site_title = "CSO Admin Portal"
index_title = "Welcome to CSO Admin Portal"


cso_admin_site = CSOAdminSite(name="CSOAdmin")
cso_admin_site.register(ProductFound, ProductFoundAdmin)
cso_admin_site.register(UserProfile, UserProfileAdmin)
admin.site.register(ProductFound, ProductFoundAdmin)
6 changes: 6 additions & 0 deletions lostandfound/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class LostandfoundConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "lostandfound"
72 changes: 72 additions & 0 deletions lostandfound/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Generated by Django 3.2.16 on 2023-11-29 14:14

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):
initial = True

dependencies = [
("users", "0040_remove_userprofile_followed_communities"),
]

operations = [
migrations.CreateModel(
name="ProductFound",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("str_id", models.CharField(editable=False, max_length=58, null=True)),
("name", models.CharField(max_length=60)),
("description", models.TextField(blank=True, default="")),
("product_image", models.TextField(blank=True, null=True)),
(
"category",
models.CharField(
blank=True,
choices=[
("electronics", "Electronics"),
("stationery", "Stationery"),
("Other", "Other"),
],
max_length=60,
null=True,
),
),
("found_at", models.CharField(blank=True, default="", max_length=60)),
("claimed", models.BooleanField(blank=True, default=True, null=True)),
("contact_details", models.CharField(max_length=300)),
("time_of_creation", models.DateTimeField(auto_now_add=True)),
(
"claimed_by",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="claimed_products",
to="users.userprofile",
),
),
],
options={
"verbose_name": "Product",
"verbose_name_plural": "Products",
"ordering": ("-time_of_creation",),
},
),
migrations.AddIndex(
model_name="productfound",
index=models.Index(
fields=["time_of_creation"], name="lostandfoun_time_of_e282f9_idx"
),
),
]
35 changes: 35 additions & 0 deletions lostandfound/migrations/0002_auto_20231209_1246.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.16 on 2023-12-09 07:16

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("lostandfound", "0001_initial"),
]

operations = [
migrations.AlterModelOptions(
name="productfound",
options={
"ordering": ("-time_of_creation",),
"verbose_name": "ProductFound",
"verbose_name_plural": "ProductsFound",
},
),
migrations.AddField(
model_name="productfound",
name="product_image1",
field=models.ImageField(blank=True, upload_to="laf_images/"),
),
migrations.AddField(
model_name="productfound",
name="product_image2",
field=models.ImageField(blank=True, upload_to="laf_images/"),
),
migrations.AddField(
model_name="productfound",
name="product_image3",
field=models.ImageField(blank=True, upload_to="laf_images/"),
),
]
Empty file.
75 changes: 75 additions & 0 deletions lostandfound/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from django.db import models
from uuid import uuid4
from helpers.misc import get_url_friendly

PDT_NAME_MAX_LENGTH = 60
CONTACT_MAX_LENGTH = 300


class ProductFound(models.Model):
CATEGORY_CHOICES = (
("electronics", "Electronics"),
("stationery", "Stationery"),
("Other", "Other"),
)

id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
str_id = models.CharField(max_length=58, editable=False, null=True)
name = models.CharField(max_length=PDT_NAME_MAX_LENGTH, blank=False, null=False)
description = models.TextField(blank=True, default="", null=False)
product_image = models.TextField(
blank=True, null=True
) # Contains URLs of all three images.
product_image1 = models.ImageField(upload_to="laf_images/", null=False, blank=False)
product_image2 = models.ImageField(upload_to="laf_images/", null=False, blank=True)
product_image3 = models.ImageField(upload_to="laf_images/", null=False, blank=True)
category = models.CharField(
choices=CATEGORY_CHOICES, null=True, blank=True, max_length=PDT_NAME_MAX_LENGTH
)
found_at = models.CharField(
max_length=PDT_NAME_MAX_LENGTH, blank=True, null=False, default=""
)

claimed = models.BooleanField(default=True, blank=True, null=True)
contact_details = models.CharField(
max_length=CONTACT_MAX_LENGTH, blank=False, null=False
)
time_of_creation = models.DateTimeField(auto_now_add=True)

claimed_by = models.ForeignKey(
"users.UserProfile",
on_delete=models.CASCADE,
related_name="claimed_products",
null=True,
blank=True,
)

def __str__(self):
return self.name

def save(self, *args, **kwargs):
if self.claimed_by is not None:
self.claimed = True
else:
self.claimed = False

image_urls = ""
image_urls += self.product_image1.url + ","
image_urls += self.product_image2.url + ","
image_urls += self.product_image3.url

self.product_image = image_urls
self.str_id = get_url_friendly(self.name) + "-" + str(self.id)[:8]
super().save(*args, **kwargs)

class Meta:
verbose_name = "ProductFound"
verbose_name_plural = "ProductsFound"
ordering = ("-time_of_creation",)
indexes = [
models.Index(
fields=[
"time_of_creation",
]
),
]
38 changes: 38 additions & 0 deletions lostandfound/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from rest_framework import serializers
from lostandfound.models import ProductFound
from users.serializers import UserProfileSerializer


class ProductFoundSerializer(serializers.ModelSerializer):
product_image = serializers.SerializerMethodField()
claimed_by = serializers.SerializerMethodField()
claimed = serializers.SerializerMethodField()

class Meta:
model = ProductFound
fields = (
"id",
"name",
"description",
"product_image",
"category",
"found_at",
"claimed",
"contact_details",
"time_of_creation",
"claimed_by",
)

def get_product_image(self, obj):
return obj.product_image.split(",") if obj.product_image else None

def get_claimed_by(self, obj):
if obj.claimed_by:
return UserProfileSerializer(
obj.claimed_by, context={"extra": ["contact_no"]}
).data

return None

def get_claimed(self, obj):
return obj.claimed
2 changes: 2 additions & 0 deletions lostandfound/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

# Create your tests here.
17 changes: 17 additions & 0 deletions lostandfound/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.urls import path
from lostandfound.views import LostandFoundViewset
from lostandfound.admin import cso_admin_site


urlpatterns = [
path("cso_admin_login/", cso_admin_site.urls, name="cso_admin_login"),
path(
"lnf/products/", LostandFoundViewset.as_view({"get": "list"}), name="products"
),
path(
"lnf/products/<str:pk>/",
LostandFoundViewset.as_view({"get": "retrieve"}),
name="product",
),
# path('lnf/products/claim/', LostandFoundViewset.as_view({'post': 'claim'}), name='claim'),
]
Loading
Loading