Skip to content

Commit

Permalink
Refactor: Delete categories and set to boolean fields
Browse files Browse the repository at this point in the history
  • Loading branch information
saeyeonn committed May 26, 2024
1 parent 5ba2f4d commit 38d4a68
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 53 deletions.
Binary file modified pure_plate/db.sqlite3
Binary file not shown.
8 changes: 0 additions & 8 deletions pure_plate/restaurant/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from django.db import models
from django.db.models import Avg

# class Category(models.Model):
# category_id = models.AutoField(primary_key=True)
# category_name = models.CharField(max_length=50, db_index=True)

# class Meta:
# verbose_name_plural = "categories"

class Restaurant(models.Model):
name = models.CharField(max_length=255, db_index=True)
address = models.CharField(max_length=255)
Expand All @@ -18,7 +11,6 @@ class Restaurant(models.Model):
phone = models.CharField(max_length=255, db_index=True)
review_count = models.IntegerField(default=0)
avg_rating = models.DecimalField(max_digits=3, decimal_places=2, default=0.00)
# categories = models.ManyToManyField(Category, related_name='restaurants')
vegan=models.BooleanField(default=False)
halal=models.BooleanField(default=False)
gluten_free=models.BooleanField(default=False)
Expand Down
4 changes: 1 addition & 3 deletions pure_plate/restaurant/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from . import views



urlpatterns = [
path('restaurants/',views.restaurants_in_categories_view, name='restaurants-in-categories')
path('list/',views.restaurants_list, name='restaurants-list')
]
61 changes: 19 additions & 42 deletions pure_plate/restaurant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,26 @@
from django.db.models import Count
from .models import Restaurant

#/api/restaurants_in_categories?categories=Italian,Chinese,Mexican

def restaurants_in_categories_view(request):
def restaurants_list(request):

try:
category_names = request.GET.get('categories', '')
if not category_names:
return JsonResponse({
'status': 200,
'message': 'Category is not selected.',
'data': []
})

category_names_list = category_names.split(',')

filter_conditions = {}
if 'vegan' in category_names_list:
filter_conditions['vegan'] = True
if 'halal' in category_names_list:
filter_conditions['halal'] = True
if 'glutenfree' in category_names_list:
filter_conditions['gluten_free'] = True
if 'lactofree' in category_names_list:
filter_conditions['lacto_free'] = True

restaurants = Restaurant.objects.filter(**filter_conditions)


restaurants = Restaurant.objects.all()

data = [{
'restaurantId': restaurant.id,
'restaurantName': restaurant.name,
'restaurantAddress': restaurant.address,
'restaurantLatitude': str(restaurant.latitude),
'restaurantLongitude': str(restaurant.longitude),
'restaurantTime': restaurant.time,
'restaurantPhoto': restaurant.photo,
'restaurantPhone': restaurant.phone,
'restaurantReviewCount': restaurant.review_count,
'restaurantRating': str(restaurant.avg_rating),
'restaurantVegan': restaurant.vegan,
'restaurantHalal': restaurant.halal,
'restaurantGlutenFree': restaurant.gluten_free,
'restaurantLactoFree': restaurant.lacto_free
'Id': restaurant.id,
'Name': restaurant.name,
'Address': restaurant.address,
'Latitude': str(restaurant.latitude),
'Longitude': str(restaurant.longitude),
'Time': restaurant.time,
'Photo': restaurant.photo,
'Phone': restaurant.phone,
'ReviewCount': restaurant.review_count,
'Rating': str(restaurant.avg_rating),
'Vegan': restaurant.vegan,
'Halal': restaurant.halal,
'GlutenFree': restaurant.gluten_free,
'LactoFree': restaurant.lacto_free
} for restaurant in restaurants]


Expand All @@ -54,10 +31,10 @@ def restaurants_in_categories_view(request):
'data': data
}

return JsonResponse(response)
return JsonResponse(response, status=200)

except Exception as e:
return JsonResponse({'status': 400, 'message': str(e), 'data': []})
return JsonResponse({'status': 400, 'message': str(e), 'data': []}, status=400)



0 comments on commit 38d4a68

Please sign in to comment.