Skip to content

Commit

Permalink
Merge pull request #213 from tourn/random-recipes2
Browse files Browse the repository at this point in the history
Show random recipes in meal planner
  • Loading branch information
vabene1111 authored Nov 4, 2020
2 parents 209924e + 69a6ede commit 8d58254
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cookbook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib.auth.models import User, Group
from django.utils.translation import gettext as _
from django.db import models
from django_random_queryset import RandomManager

from recipes.settings import COMMENT_PREF_DEFAULT

Expand Down Expand Up @@ -198,6 +199,8 @@ class Recipe(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

objects = RandomManager()

def __str__(self):
return self.name

Expand Down
17 changes: 15 additions & 2 deletions cookbook/templates/meal_plan.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@
<div class="card-body">
<div class="row">
<div class="col-md-12">
<input type="text" class="form-control" v-model="recipe_query" @keyup="getRecipes"
placeholder="{% trans 'Search Recipe' %}" style="margin-bottom: 8px">
<div class="input-group mb-3">
<input type="text" class="form-control" v-model="recipe_query" @keyup="getRecipes"
placeholder="{% trans 'Search Recipe' %}">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" @click="getRandomRecipes">
<i class="fas fa-dice"></i>
</button>
</div>
</div>
</div>
</div>
<draggable class="list-group" :list="recipes"
Expand Down Expand Up @@ -445,10 +452,16 @@ <h5 class="modal-title">{% trans 'Meal Plan Help' %}</h5>

this.updateUserNames()
},
getRandomRecipes: function () {
this.$set(this, 'recipe_query', '');
this.getRecipes();
},
getRecipes: function () {
let url = "{% url 'api:recipe-list' %}?limit=5"
if (this.recipe_query !== '') {
url += '&query=' + this.recipe_query;
} else {
url += '&random=True'
}

this.$http.get(url).then((response) => {
Expand Down
8 changes: 7 additions & 1 deletion cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ class RecipeViewSet(viewsets.ModelViewSet, StandardFilterMixin):
permission_classes = [CustomIsShare | CustomIsGuest] # TODO split read and write permission for meal plan guest

def get_queryset(self):
queryset = Recipe.objects.all()
internal = self.request.query_params.get('internal', None)
if internal:
self.queryset = self.queryset.filter(internal=True)
queryset = queryset.filter(internal=True)
random = self.request.query_params.get('random', False)
if random:
queryset = queryset.random(5)

self.queryset = queryset

return super(RecipeViewSet, self).get_queryset()

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ icalendar==4.0.6
pyyaml==5.3.1
uritemplate==3.0.1
beautifulsoup4==4.9.2
microdata==0.7.1
microdata==0.7.1
django-random-queryset==0.1.3

0 comments on commit 8d58254

Please sign in to comment.