Skip to content

Commit

Permalink
Merge pull request #321 from RyanNoelk/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
RyanNoelk authored Jan 24, 2018
2 parents 72594ec + 02e25a9 commit c9a7dd8
Show file tree
Hide file tree
Showing 52 changed files with 1,256 additions and 800 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static-files/
database/
frontend/build/
frontend/jest/
site-media/
openeats.sql

servies/static-files/
servies/side-media/
Expand Down
25 changes: 25 additions & 0 deletions api/v1/recipe/migrations/0012_auto_20180106_1113.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2018-01-06 17:13
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('recipe', '0011_auto_20171114_1543'),
]

operations = [
migrations.AddField(
model_name='recipe',
name='public',
field=models.BooleanField(default=True),
),
migrations.AlterField(
model_name='recipe',
name='info',
field=models.TextField(blank=True, help_text='enter information about the recipe', verbose_name='info'),
),
]
1 change: 1 addition & 0 deletions api/v1/recipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Recipe(models.Model):
rating = models.IntegerField(_('rating'), help_text="rating of the meal", default=0)
pub_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
public = models.BooleanField(default=True)

class Meta:
ordering = ['-pub_date', 'title']
Expand Down
2 changes: 1 addition & 1 deletion api/v1/recipe/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Create a router and register our viewsets with it.
router = DefaultRouter(schema_title='Recipes')
router.register(r'mini-browse', views.MiniBrowseViewSet)
router.register(r'recipes', views.RecipeViewSet)
router.register(r'recipes', views.RecipeViewSet, base_name='recipes')
router.register(r'rating', views.RatingViewSet, base_name='rating')

urlpatterns = [
Expand Down
53 changes: 42 additions & 11 deletions api/v1/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,33 @@ class RecipeViewSet(viewsets.ModelViewSet):
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Recipe.objects.all()
serializer_class = serializers.RecipeSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter)
filter_fields = ('course__slug', 'cuisine__slug', 'course', 'cuisine', 'title', 'rating')
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
search_fields = ('title', 'tags__title', 'ingredient_groups__ingredients__title')
ordering_fields = ('pub_date', 'title', 'rating', )

def get_queryset(self):
query = Recipe.objects
filter_set = {}

# If user is anonymous, restrict recipes to public.
if not self.request.user.is_authenticated:
filter_set['public'] = True

if 'cuisine__slug' in self.request.query_params:
filter_set['cuisine__in'] = Cuisine.objects.filter(
slug__in=self.request.query_params.get('cuisine__slug').split(',')
)

if 'course__slug' in self.request.query_params:
filter_set['course__in'] = Course.objects.filter(
slug__in=self.request.query_params.get('course__slug').split(',')
)
if 'rating' in self.request.query_params:
filter_set['rating__in'] = self.request.query_params.get('rating').split(',')

return query.filter(**filter_set)

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
Expand Down Expand Up @@ -69,18 +90,24 @@ class MiniBrowseViewSet(viewsets.mixins.ListModelMixin,
serializer_class = serializers.MiniBrowseSerializer

def list(self, request, *args, **kwargs):
# If user is anonymous, restrict recipes to public.
if self.request.user.is_authenticated:
qs = Recipe.objects.all()
else:
qs = Recipe.objects.filter(public=True)

# Get the limit from the request and the count from the DB.
# Compare to make sure you aren't accessing more than possible.
limit = int(request.query_params.get('limit'))
count = Recipe.objects.count()
count = qs.count()
if limit > count:
limit = count

# Get all ids from the DB.
my_ids = Recipe.objects.values_list('id', flat=True)
my_ids = qs.values_list('id', flat=True)
# Select a random sample from the DB.
rand_ids = random.sample(my_ids, limit)
# set teh queryset to that random sample.
# set the queryset to that random sample.
self.queryset = Recipe.objects.filter(id__in=rand_ids)

return super(MiniBrowseViewSet, self).list(request, *args, **kwargs)
Expand All @@ -91,20 +118,24 @@ class RatingViewSet(viewsets.ReadOnlyModelViewSet):

def get_queryset(self):
query = Recipe.objects

filter_set = {}

# If user is anonymous, restrict recipes to public.
if not self.request.user.is_authenticated:
filter_set['public'] = True

if 'cuisine' in self.request.query_params:
try:
filter_set['cuisine'] = Cuisine.objects.get(
slug=self.request.query_params.get('cuisine')
filter_set['cuisine__in'] = Cuisine.objects.filter(
slug__in=self.request.query_params.get('cuisine').split(',')
)
except:
return []

if 'course' in self.request.query_params:
try:
filter_set['course'] = Course.objects.get(
slug=self.request.query_params.get('course')
filter_set['course__in'] = Course.objects.filter(
slug__in=self.request.query_params.get('course').split(',')
)
except:
return []
Expand Down
24 changes: 16 additions & 8 deletions api/v1/recipe_groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,22 @@ class CuisineCountViewSet(viewsets.ModelViewSet):

def get_queryset(self):
query = Recipe.objects

filter_set = {}

# If user is anonymous, restrict recipes to public.
if not self.request.user.is_authenticated:
filter_set['public'] = True

if 'course' in self.request.query_params:
try:
filter_set['course'] = Course.objects.get(
slug=self.request.query_params.get('course')
filter_set['course__in'] = Course.objects.filter(
slug__in=self.request.query_params.get('course').split(',')
)
except:
return []

if 'rating' in self.request.query_params:
filter_set['rating'] = self.request.query_params.get('rating')
filter_set['rating__in'] = self.request.query_params.get('rating').split(',')

if 'search' in self.request.query_params:
query = get_search_results(
Expand Down Expand Up @@ -94,18 +98,22 @@ class CourseCountViewSet(viewsets.ModelViewSet):

def get_queryset(self):
query = Recipe.objects

filter_set = {}

# If user is anonymous, restrict recipes to public.
if not self.request.user.is_authenticated:
filter_set['public'] = True

if 'cuisine' in self.request.query_params:
try:
filter_set['cuisine'] = Cuisine.objects.get(
slug=self.request.query_params.get('cuisine')
filter_set['cuisine__in'] = Cuisine.objects.filter(
slug__in=self.request.query_params.get('cuisine').split(',')
)
except:
return []

if 'rating' in self.request.query_params:
filter_set['rating'] = self.request.query_params.get('rating')
filter_set['rating__in'] = self.request.query_params.get('rating').split(',')

if 'search' in self.request.query_params:
query = get_search_results(
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '2.1'
services:
api:
build: api/
Expand Down
9 changes: 7 additions & 2 deletions docker-prod.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '2.1'
services:
api:
image: openeats/api
Expand All @@ -8,7 +8,8 @@ services:
- static-files:/code/static-files
- site-media:/code/site-media
depends_on:
- db
db:
condition: service_healthy
env_file:
env_prod.list
node:
Expand All @@ -20,6 +21,10 @@ services:
env_prod.list
db:
image: mariadb
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 20
env_file:
env_prod.list
nginx:
Expand Down
9 changes: 7 additions & 2 deletions docker-stage.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '2.1'
services:
api:
build: api/
Expand All @@ -8,7 +8,8 @@ services:
- static-files:/code/static-files
- site-media:/code/site-media
depends_on:
- db
db:
condition: service_healthy
env_file:
env_stg.list
node:
Expand All @@ -22,6 +23,10 @@ services:
env_stg.list
db:
image: mariadb
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 20
env_file:
env_stg.list
nginx:
Expand Down
File renamed without changes.
5 changes: 1 addition & 4 deletions docs/Running_the_App.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

The recommended way to run this app is with docker. You can install docker [here](https://www.docker.com/products/overview). If you are not familiar with docker you can read more about it on [their website](https://www.docker.com/what-docker).

If you are looking to run the app without docker, see the instructions [here](Running_without_Docker.md)

### Running the app with docker for production

If you are looking to run this in production, there is no need to clone the repo.
Expand All @@ -12,7 +10,7 @@ First, create two files:
- docker-prod.yml - This file can be found in the in the root directory of the repo.
- env_prod.list - The settings file [sample_env_file_for_docker.list](sample_env_file_for_docker.list) can be used as an example.

The `docker-prod.yml` contains the list of images and commands to run the app. It come with an nginx reverse proxy that by default will run on port 80. You will most likely want to change the port that nginx runs on as well as use a fix tag for the image. By default all are set to latest.
The `docker-prod.yml` contains the list of images and commands to run the app. It comes with an nginx reverse proxy that by default will run on port 80. You will most likely want to change the port that nginx runs on as well as use a fix tag for the image. By default, all are set to latest.

#### Configure the environment file
Most of the settings in your `env_prod.list` can stay the same as `env_stg.list` that is in this repo. There are a few config settings that need to be changed for most configurations. See [Setting_up_env_file.md](Setting_up_env_file.md) for a complete description of the environment variables.
Expand Down Expand Up @@ -59,5 +57,4 @@ If you want to add some test data you can load a few recipes and some news data.
./manage.py loaddata news_data.json
./manage.py loaddata recipe_data.json
./manage.py loaddata ing_data.json
./manage.py loaddata direction_data.json
```
18 changes: 18 additions & 0 deletions docs/Taking_Backups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Backing up Your Data

The following commands can be used to take backups of your data. These commands are automatically run when you upgrade to a newer version as well.


#### Backing up recipes images:

Replace `/dir/on/local/system/` with the location where you would like your images.
```sh
docker cp openeats_api_1:/code/site-media/ /dir/on/local/system/
```

#### Backing up database:

Places a sql dump of the database on your current working directory.
```sh
docker exec openeats_db_1 sh -c 'exec mysqldump openeats -uroot -p"$MYSQL_ROOT_PASSWORD"' > openeats.sql
```
Loading

0 comments on commit c9a7dd8

Please sign in to comment.