Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
DeyanVNikolov committed Feb 21, 2024
1 parent ce76132 commit 6d8b221
Show file tree
Hide file tree
Showing 45 changed files with 7,482 additions and 4,514 deletions.
3 changes: 2 additions & 1 deletion website/adventora/templates/hotels.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ <h1 style="margin: 10px auto;" class="title is-size-5 has-text-weight-bold">{{ h
</div>
<div class="card-body has-img">
<figure class="image is-4by3">
<img src="/static/cover/hotel/{{ hotel.id }}/{{ hotel.id }}-0.png" style="object-fit: cover" alt="Placeholder image">

<img src="/static/cover/hotel/{{ hotel.id }}/{{ hotel.main_img }}" style="object-fit: cover" alt="Placeholder image">
</figure>
</div>
<div class="card-footer">
Expand Down
13 changes: 13 additions & 0 deletions website/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ def stranded_abroad(request):
def hotels(request):
hotels = Hotel.objects.all().exclude(approved=False)

for hotel in hotels:
# select ONE random image from hotel.photos
photos = str(hotel.photos).split('|PHOTO|')
if len(photos) > 0:
# select first image, if not os.exists, select second, if not os.exists, select third, if not os.exists, select fourth, if not os.exists, select fifth
img = None
for photo in photos:
if os.path.isfile(f'static/cover/hotel/{hotel.id}/{photo}'):
img = photo
break
if img and os.path.isfile(f'static/cover/hotel/{hotel.id}/{img}'):
hotel.main_img = img

context = {
'hotels': hotels
}
Expand Down
18 changes: 18 additions & 0 deletions website/hotel/migrations/0025_hotel_photos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.4 on 2024-02-21 20:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('hotel', '0024_remove_hotel_short_code'),
]

operations = [
migrations.AddField(
model_name='hotel',
name='photos',
field=models.CharField(blank=True, max_length=50000, null=True),
),
]
1 change: 1 addition & 0 deletions website/hotel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Hotel(models.Model):
mol_name = models.CharField(max_length=100, null=True, blank=True)
stars = models.IntegerField(null=True)
approved = models.BooleanField(default=False, null=False)
photos = models.CharField(max_length=50000, null=True, blank=True)

def __str__(self):
return self.name
Expand Down
24 changes: 16 additions & 8 deletions website/hotel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,10 @@ def photos(request):
hotel_images = []
BASE_DIR = Path(__file__).resolve().parent.parent

image_dir = f'{BASE_DIR}/static/cover/hotel/{hotel.id}'
if os.path.isdir(image_dir):
for filename in os.listdir(image_dir):
hotel_images.append(filename)
images = str(hotel.photos).split("|PHOTO|")
for image in images:
if os.path.exists(f"{BASE_DIR}/static/cover/hotel/{hotel.id}/{image}"):
hotel_images.append(image)

if request.method == "POST":
photos = request.FILES.getlist('photos')
Expand All @@ -714,13 +714,14 @@ def photos(request):
if not os.path.exists(f"{BASE_DIR}/static/cover/hotel/{hotel.id}"):
os.makedirs(f"{BASE_DIR}/static/cover/hotel/{hotel.id}")
# start the index from the number of files in the folder, if there are 3 files, start from 3
index = len(os.listdir(f"{BASE_DIR}/static/cover/hotel/{hotel.id}"))
for photo in photos:
with open(f"{BASE_DIR}/static/cover/hotel/{hotel.id}/{hotel.id}-{index}.png", 'wb+') as destination:
name = uuid.uuid4()
with open(f"{BASE_DIR}/static/cover/hotel/{hotel.id}/{str(name)}.png", 'wb+') as destination:
for chunk in photo.chunks():
destination.write(chunk)
index += 1
hotel.photos = str(hotel.photos) + f"|PHOTO|{str(name)}.png"

hotel.save()
messages.success(request, _('Successfully uploaded photos'))
return redirect('photos')

Expand Down Expand Up @@ -753,10 +754,17 @@ def deletephoto(request, hotel_id, photo_id):
messages.warning(request, _('You do not have permission to view this page'))
return redirect('dashboard')

BASE_DIR = Path(__file__).resolve().parent.parent
photos = str(hotel.photos).split("|PHOTO|")
if photo_id not in photos:
messages.warning(request, _('Photo not found'))
return redirect('photos')

photo_path = f"{BASE_DIR}/static/cover/hotel/{hotel.id}/{photo_id}"

if os.path.exists(photo_path):
os.remove(photo_path)
hotel.photos = str(hotel.photos).replace(f"|PHOTO|{photo_id}", "")
hotel.save()
messages.success(request, _('Successfully deleted photo'))
return redirect('photos')
else:
Expand Down
Binary file added website/locale/bg/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 6d8b221

Please sign in to comment.