diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py index 086df8e..924eff0 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.4 on 2019-01-09 11:35 +# Generated by Django 2.1.4 on 2019-01-13 16:56 from django.conf import settings import django.contrib.auth.models @@ -35,8 +35,8 @@ class Migration(migrations.Migration): ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), ], options={ - 'managed': True, 'db_table': 'user', + 'managed': True, }, managers=[ ('objects', django.contrib.auth.models.UserManager()), @@ -51,8 +51,8 @@ class Migration(migrations.Migration): ('diet_is_approved', models.BooleanField(default=False)), ], options={ - 'managed': True, 'db_table': 'diet', + 'managed': True, }, ), migrations.CreateModel( @@ -62,8 +62,8 @@ class Migration(migrations.Migration): ('diet', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')), ], options={ - 'managed': True, 'db_table': 'diet_prohibits_ingredient', + 'managed': True, }, ), migrations.CreateModel( @@ -75,8 +75,8 @@ class Migration(migrations.Migration): ('drink_is_approved', models.BooleanField(default=False)), ], options={ - 'managed': True, 'db_table': 'drink', + 'managed': True, }, ), migrations.CreateModel( @@ -86,8 +86,8 @@ class Migration(migrations.Migration): ('drink', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Drink')), ], options={ - 'managed': True, 'db_table': 'drink_has_ingredient', + 'managed': True, }, ), migrations.CreateModel( @@ -100,8 +100,8 @@ class Migration(migrations.Migration): ('food_is_approved', models.BooleanField(default=False)), ], options={ - 'managed': True, 'db_table': 'food', + 'managed': True, }, ), migrations.CreateModel( @@ -111,8 +111,8 @@ class Migration(migrations.Migration): ('food', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Food')), ], options={ - 'managed': True, 'db_table': 'food_has_ingredient', + 'managed': True, }, ), migrations.CreateModel( @@ -122,8 +122,8 @@ class Migration(migrations.Migration): ('ingredient_has_alcohol', models.BooleanField(blank=True, default=False, null=True)), ], options={ - 'managed': True, 'db_table': 'ingredient', + 'managed': True, }, ), migrations.CreateModel( @@ -133,8 +133,8 @@ class Migration(migrations.Migration): ('permission_description', models.CharField(max_length=700)), ], options={ - 'managed': True, 'db_table': 'permission', + 'managed': True, }, ), migrations.CreateModel( @@ -151,8 +151,8 @@ class Migration(migrations.Migration): ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], options={ - 'managed': True, 'db_table': 'restaurant', + 'managed': True, }, ), migrations.CreateModel( @@ -163,8 +163,8 @@ class Migration(migrations.Migration): ('role_description', models.CharField(max_length=700)), ], options={ - 'managed': True, 'db_table': 'role', + 'managed': True, }, ), migrations.CreateModel( @@ -175,8 +175,8 @@ class Migration(migrations.Migration): ('role', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Role')), ], options={ - 'managed': True, 'db_table': 'role_has_permission', + 'managed': True, }, ), migrations.CreateModel( @@ -187,8 +187,8 @@ class Migration(migrations.Migration): ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], options={ - 'managed': True, 'db_table': 'user_follows_diet', + 'managed': True, }, ), migrations.CreateModel( @@ -199,8 +199,8 @@ class Migration(migrations.Migration): ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], options={ - 'managed': True, 'db_table': 'user_prohibits_ingredient', + 'managed': True, }, ), migrations.CreateModel( @@ -208,15 +208,15 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('rating_grade', models.IntegerField()), - ('rating_date', models.DateField()), + ('rating_date', models.DateField(auto_now=True)), ('rating_text', models.CharField(blank=True, max_length=700, null=True)), ('rating_pοrtion_size', models.CharField(blank=True, max_length=6, null=True)), ('drink', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Drink')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], options={ - 'managed': True, 'db_table': 'user_rates_drink', + 'managed': True, }, ), migrations.CreateModel( @@ -224,15 +224,15 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('rating_grade', models.IntegerField()), - ('rating_date', models.DateField()), + ('rating_date', models.DateField(auto_now=True)), ('rating_text', models.CharField(blank=True, max_length=700, null=True)), ('rating_pοrtion_size', models.CharField(blank=True, max_length=6, null=True)), ('food', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Food')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], options={ - 'managed': True, 'db_table': 'user_rates_food', + 'managed': True, }, ), migrations.CreateModel( @@ -240,16 +240,16 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('rating_grade', models.IntegerField()), - ('rating_date', models.DateField()), + ('rating_date', models.DateField(auto_now=True)), ('rating_text', models.CharField(blank=True, max_length=700, null=True)), ('rating_accessibility', models.CharField(blank=True, max_length=8, null=True)), ('diet', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')), ('restaurant', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Restaurant')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], options={ - 'managed': True, 'db_table': 'user_rates_restaurant', + 'managed': True, }, ), migrations.AddField( diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0002_auto_20190110_2042.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0002_auto_20190110_2042.py deleted file mode 100644 index b00edf8..0000000 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0002_auto_20190110_2042.py +++ /dev/null @@ -1,25 +0,0 @@ -# Generated by Django 2.1.4 on 2019-01-10 20:42 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('hyrieus', '0001_initial'), - ] - - operations = [ - migrations.AlterField( - model_name='userratesrestaurant', - name='rating_date', - field=models.DateField(auto_now=True), - ), - migrations.AlterField( - model_name='userratesrestaurant', - name='user', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL), - ), - ] diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0003_auto_20190113_0009.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0003_auto_20190113_0009.py deleted file mode 100644 index 1751b7e..0000000 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0003_auto_20190113_0009.py +++ /dev/null @@ -1,35 +0,0 @@ -# Generated by Django 2.1.4 on 2019-01-13 00:09 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('hyrieus', '0002_auto_20190110_2042'), - ] - - operations = [ - migrations.AlterField( - model_name='userratesdrink', - name='rating_date', - field=models.DateField(auto_now=True), - ), - migrations.AlterField( - model_name='userratesdrink', - name='user', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL), - ), - migrations.AlterField( - model_name='userratesfood', - name='rating_date', - field=models.DateField(auto_now=True), - ), - migrations.AlterField( - model_name='userratesfood', - name='user', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL), - ), - ] diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py index 67225a7..4e85d0a 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py @@ -9,6 +9,7 @@ from flavoursAPI.hyrieus.views import (DietViewSet, DietProhibitsIngredientViewS UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets, FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet, FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView, + AddIngredientToFood, AddIngredientToDrink, ) router = routers.DefaultRouter() @@ -45,6 +46,8 @@ urlpatterns = [ path('prohibitIngredient//', ProhibitIngredient.as_view(), name='prohibitIngredient'), path('dietUserView/', DietUserView.as_view(), name='dietUserView'), path('ingredientUserView/', IngredientUserView.as_view(), name='ingredientUserView'), + path('addIngredientToFood/', AddIngredientToFood.as_view(), name='addIngredientToFood'), + path('addIngredientToDrink/', AddIngredientToDrink.as_view(), name='addIngredientToDrink'), path('rest-auth/', include('rest_auth.urls')), url(r'^rest-auth/registration/', include('rest_auth.registration.urls')) ] diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py index 5d99206..923ca35 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py @@ -202,7 +202,8 @@ class RestaurantViewSet(viewsets.ModelViewSet): restaurants = restaurants.distinct(); if filterType is not None: - restaurants = restaurants.filter(restaurant_category=filterType) + filterType = filterType.split(",") + restaurants = restaurants.filter(restaurant_category__in=filterType) if not user.role.role_id == 1: restaurants = restaurants.filter(restaurant_is_approved=True) @@ -553,9 +554,12 @@ class ProfileUserView(APIView): userDiets = UserFollowsDiet.objects.all().filter(user=profile).prefetch_related('diet') userDiets = Diet.objects.filter(diet_id__in = userDiets.values('diet')) \ .values('diet_id', 'diet_name') - profileInfo['diets'] = userDiets + userIngredients = UserProhibitsIngredient.objects.all().filter(user=profile) + userIngredients = [UserProhibitsIngredientSerializer(ingredient).data for ingredient in userIngredients] + profileInfo['ingredients'] = userIngredients + ownsRestaurants = Restaurant.objects.filter(user=profile).values('restaurant_id', 'restaurant_name') profileInfo['owns'] = ownsRestaurants @@ -798,4 +802,66 @@ class IngredientUserView(APIView): 'prohibits': userProhibitsIngredient } - return Response(data) \ No newline at end of file + return Response(data) + +class AddIngredientToFood(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def post(self, request, format=None): + requestData = request.data.copy() + + food = request.data['food'] + ingredient = requestData['ingredient_name'] + + try: + Ingredient.objects.get(ingredient_name=ingredient) + except Ingredient.DoesNotExist: + newIngredient = {'ingredient_name': ingredient} + if 'ingredient_has_alcohol' in requestData: + ingredient_has_alcohol = requestData.pop('ingredient_has_alcohol') + newIngredient['ingredient_has_alcohol'] = False if ingredient_has_alcohol == "false" else True + serializer = IngredientSerializer(data = newIngredient) + if serializer.is_valid(): + serializer.save() + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + serializer = FoodHasIngredientSerializer(data = requestData) + if serializer.is_valid(): + food = serializer.save() + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + return Response(serializer.data) + +class AddIngredientToDrink(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def post(self, request, format=None): + requestData = request.data.copy() + + drink = request.data['drink'] + ingredient = requestData['ingredient_name'] + + try: + Ingredient.objects.get(ingredient_name=ingredient) + except Ingredient.DoesNotExist: + newIngredient = {'ingredient_name': ingredient} + if 'ingredient_has_alcohol' in requestData: + ingredient_has_alcohol = requestData.pop('ingredient_has_alcohol') + newIngredient['ingredient_has_alcohol'] = False if ingredient_has_alcohol == "false" else True + serializer = IngredientSerializer(data = newIngredient) + if serializer.is_valid(): + serializer.save() + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + serializer = DrinkHasIngredientSerializer(data = requestData) + if serializer.is_valid(): + food = serializer.save() + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + return Response(serializer.data) \ No newline at end of file