Show random recipes in meal planner
This commit is contained in:
parent
0ac23b4e3a
commit
69a6edee99
@ -7,6 +7,7 @@ from django.contrib import auth
|
||||
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
|
||||
|
||||
@ -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
|
||||
|
||||
|
@ -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"
|
||||
@ -445,10 +452,16 @@
|
||||
|
||||
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) => {
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user