Skip to content

Commit

Permalink
Merge pull request #516 from Krishna-Baldwa/lnf&comm_issues
Browse files Browse the repository at this point in the history
fix image url for images in lost and found
  • Loading branch information
VIBR0X authored Dec 22, 2023
2 parents 0f0f00c + da377b0 commit 5c0553e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ venv.bak/

# mypy
.mypy_cache/

# Mac OS files
*.DS_Store
2 changes: 1 addition & 1 deletion community/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setUp(self):

def test_community_list(self):
"""Test if communities can be listed."""
url = "/api/events"
url = "/api/communities"
response = self.client1.get(url)
self.assertEqual(response.status_code, 200)
self.assertGreater(len(response.data), 0)
Expand Down
1 change: 1 addition & 0 deletions lostandfound/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Meta:
"product_image1",
"product_image2",
"product_image3",
"uploaded_by",
]

self.form = CustomChangeForm
Expand Down
41 changes: 41 additions & 0 deletions lostandfound/migrations/0003_auto_20231211_1548.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 3.2.16 on 2023-12-11 10:18

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


class Migration(migrations.Migration):

dependencies = [
('users', '0044_remove_userprofile_followed_communities'),
('lostandfound', '0002_auto_20231209_1246'),
]

operations = [
migrations.AddField(
model_name='productfound',
name='uploaded_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='uploaded_products', to='users.userprofile'),
),
migrations.AlterField(
model_name='productfound',
name='claimed',
field=models.BooleanField(blank=True, default=False, null=True),
),
migrations.AlterField(
model_name='productfound',
name='product_image1',
field=models.ImageField(upload_to=lostandfound.models.get_image_path),
),
migrations.AlterField(
model_name='productfound',
name='product_image2',
field=models.ImageField(blank=True, upload_to=lostandfound.models.get_image_path),
),
migrations.AlterField(
model_name='productfound',
name='product_image3',
field=models.ImageField(blank=True, upload_to=lostandfound.models.get_image_path),
),
]
27 changes: 20 additions & 7 deletions lostandfound/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
PDT_NAME_MAX_LENGTH = 60
CONTACT_MAX_LENGTH = 300

def get_image_path(instance, filename):
userid = str(instance.uploaded_by.id)
return './' + userid[0:2] + '/' + userid[2:4] + '/' + userid + '-' + filename + '.jpg'

class ProductFound(models.Model):
CATEGORY_CHOICES = (
Expand All @@ -20,22 +23,30 @@ class ProductFound(models.Model):
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)
product_image1 = models.ImageField(upload_to=get_image_path, null=False, blank=False)
product_image2 = models.ImageField(upload_to=get_image_path, null=False, blank=True)
product_image3 = models.ImageField(upload_to=get_image_path, 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)
claimed = models.BooleanField(default=False, 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)

uploaded_by = models.ForeignKey(
"users.UserProfile",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="uploaded_products",
)

claimed_by = models.ForeignKey(
"users.UserProfile",
on_delete=models.CASCADE,
Expand All @@ -55,8 +66,10 @@ def save(self, *args, **kwargs):

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

self.product_image = image_urls
self.str_id = get_url_friendly(self.name) + "-" + str(self.id)[:8]
Expand All @@ -72,4 +85,4 @@ class Meta:
"time_of_creation",
]
),
]
]

0 comments on commit 5c0553e

Please sign in to comment.