Browse Source

Finalization, Add API support for ingredient addition in foods and drinks

master
Apostolos Fanakis 6 years ago
parent
commit
8d2d83b437
  1. 48
      UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py
  2. 25
      UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0002_auto_20190110_2042.py
  3. 35
      UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0003_auto_20190113_0009.py
  4. 3
      UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py
  5. 70
      UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py

48
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 from django.conf import settings
import django.contrib.auth.models 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')), ('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={ options={
'managed': True,
'db_table': 'user', 'db_table': 'user',
'managed': True,
}, },
managers=[ managers=[
('objects', django.contrib.auth.models.UserManager()), ('objects', django.contrib.auth.models.UserManager()),
@ -51,8 +51,8 @@ class Migration(migrations.Migration):
('diet_is_approved', models.BooleanField(default=False)), ('diet_is_approved', models.BooleanField(default=False)),
], ],
options={ options={
'managed': True,
'db_table': 'diet', 'db_table': 'diet',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -62,8 +62,8 @@ class Migration(migrations.Migration):
('diet', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')), ('diet', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')),
], ],
options={ options={
'managed': True,
'db_table': 'diet_prohibits_ingredient', 'db_table': 'diet_prohibits_ingredient',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -75,8 +75,8 @@ class Migration(migrations.Migration):
('drink_is_approved', models.BooleanField(default=False)), ('drink_is_approved', models.BooleanField(default=False)),
], ],
options={ options={
'managed': True,
'db_table': 'drink', 'db_table': 'drink',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -86,8 +86,8 @@ class Migration(migrations.Migration):
('drink', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Drink')), ('drink', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Drink')),
], ],
options={ options={
'managed': True,
'db_table': 'drink_has_ingredient', 'db_table': 'drink_has_ingredient',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -100,8 +100,8 @@ class Migration(migrations.Migration):
('food_is_approved', models.BooleanField(default=False)), ('food_is_approved', models.BooleanField(default=False)),
], ],
options={ options={
'managed': True,
'db_table': 'food', 'db_table': 'food',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -111,8 +111,8 @@ class Migration(migrations.Migration):
('food', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Food')), ('food', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Food')),
], ],
options={ options={
'managed': True,
'db_table': 'food_has_ingredient', 'db_table': 'food_has_ingredient',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -122,8 +122,8 @@ class Migration(migrations.Migration):
('ingredient_has_alcohol', models.BooleanField(blank=True, default=False, null=True)), ('ingredient_has_alcohol', models.BooleanField(blank=True, default=False, null=True)),
], ],
options={ options={
'managed': True,
'db_table': 'ingredient', 'db_table': 'ingredient',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -133,8 +133,8 @@ class Migration(migrations.Migration):
('permission_description', models.CharField(max_length=700)), ('permission_description', models.CharField(max_length=700)),
], ],
options={ options={
'managed': True,
'db_table': 'permission', 'db_table': 'permission',
'managed': True,
}, },
), ),
migrations.CreateModel( 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)), ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
], ],
options={ options={
'managed': True,
'db_table': 'restaurant', 'db_table': 'restaurant',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -163,8 +163,8 @@ class Migration(migrations.Migration):
('role_description', models.CharField(max_length=700)), ('role_description', models.CharField(max_length=700)),
], ],
options={ options={
'managed': True,
'db_table': 'role', 'db_table': 'role',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -175,8 +175,8 @@ class Migration(migrations.Migration):
('role', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Role')), ('role', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Role')),
], ],
options={ options={
'managed': True,
'db_table': 'role_has_permission', 'db_table': 'role_has_permission',
'managed': True,
}, },
), ),
migrations.CreateModel( 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)), ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
], ],
options={ options={
'managed': True,
'db_table': 'user_follows_diet', 'db_table': 'user_follows_diet',
'managed': True,
}, },
), ),
migrations.CreateModel( 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)), ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
], ],
options={ options={
'managed': True,
'db_table': 'user_prohibits_ingredient', 'db_table': 'user_prohibits_ingredient',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -208,15 +208,15 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating_grade', models.IntegerField()), ('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_text', models.CharField(blank=True, max_length=700, null=True)),
('rating_pοrtion_size', models.CharField(blank=True, max_length=6, 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')), ('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={ options={
'managed': True,
'db_table': 'user_rates_drink', 'db_table': 'user_rates_drink',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -224,15 +224,15 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating_grade', models.IntegerField()), ('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_text', models.CharField(blank=True, max_length=700, null=True)),
('rating_pοrtion_size', models.CharField(blank=True, max_length=6, 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')), ('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={ options={
'managed': True,
'db_table': 'user_rates_food', 'db_table': 'user_rates_food',
'managed': True,
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -240,16 +240,16 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating_grade', models.IntegerField()), ('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_text', models.CharField(blank=True, max_length=700, null=True)),
('rating_accessibility', models.CharField(blank=True, max_length=8, 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')), ('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')), ('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={ options={
'managed': True,
'db_table': 'user_rates_restaurant', 'db_table': 'user_rates_restaurant',
'managed': True,
}, },
), ),
migrations.AddField( migrations.AddField(

25
UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0002_auto_20190110_2042.py

@ -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),
),
]

35
UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0003_auto_20190113_0009.py

@ -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),
),
]

3
UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py

@ -9,6 +9,7 @@ from flavoursAPI.hyrieus.views import (DietViewSet, DietProhibitsIngredientViewS
UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets, UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets,
FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet, FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet,
FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView, FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView,
AddIngredientToFood, AddIngredientToDrink,
) )
router = routers.DefaultRouter() router = routers.DefaultRouter()
@ -45,6 +46,8 @@ urlpatterns = [
path('prohibitIngredient/<ingredient>/', ProhibitIngredient.as_view(), name='prohibitIngredient'), path('prohibitIngredient/<ingredient>/', ProhibitIngredient.as_view(), name='prohibitIngredient'),
path('dietUserView/', DietUserView.as_view(), name='dietUserView'), path('dietUserView/', DietUserView.as_view(), name='dietUserView'),
path('ingredientUserView/', IngredientUserView.as_view(), name='ingredientUserView'), 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')), path('rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')) url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
] ]

70
UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py

@ -202,7 +202,8 @@ class RestaurantViewSet(viewsets.ModelViewSet):
restaurants = restaurants.distinct(); restaurants = restaurants.distinct();
if filterType is not None: 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: if not user.role.role_id == 1:
restaurants = restaurants.filter(restaurant_is_approved=True) 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 = UserFollowsDiet.objects.all().filter(user=profile).prefetch_related('diet')
userDiets = Diet.objects.filter(diet_id__in = userDiets.values('diet')) \ userDiets = Diet.objects.filter(diet_id__in = userDiets.values('diet')) \
.values('diet_id', 'diet_name') .values('diet_id', 'diet_name')
profileInfo['diets'] = userDiets 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') ownsRestaurants = Restaurant.objects.filter(user=profile).values('restaurant_id', 'restaurant_name')
profileInfo['owns'] = ownsRestaurants profileInfo['owns'] = ownsRestaurants
@ -799,3 +803,65 @@ class IngredientUserView(APIView):
} }
return Response(data) 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)
Loading…
Cancel
Save