re added stuff

This commit is contained in:
vabene1111 2020-12-28 14:14:34 +01:00
parent 643dbbc294
commit 8b2833f353
5 changed files with 58 additions and 18 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-08-30 13:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0091_auto_20201226_1551'),
]
operations = [
migrations.AddField(
model_name='recipe',
name='servings',
field=models.IntegerField(default=1),
),
]

View File

@ -109,7 +109,7 @@
<div class="col col-md-3"> <div class="col col-md-3">
<div class="input-group d-print-none"> <div class="input-group d-print-none">
<input type="number" value="1" maxlength="3" class="form-control" <input type="number" value="1" maxlength="3" class="form-control" style="min-width: 2vw"
v-model="servings"/> v-model="servings"/>
<div class="input-group-append"> <div class="input-group-append">
<span class="input-group-text"><i class="fas fa-calculator"></i></span> <span class="input-group-text"><i class="fas fa-calculator"></i></span>
@ -516,7 +516,12 @@
let csrftoken = Cookies.get('csrftoken'); let csrftoken = Cookies.get('csrftoken');
Vue.http.headers.common['X-CSRFToken'] = csrftoken; Vue.http.headers.common['X-CSRFToken'] = csrftoken;
const recipe_servings = {{ recipe.servings }} {% if user_servings %}
const recipe_servings = {{ user_servings|floatformat:0 }}
{% else %}
const recipe_servings = {{ recipe.servings }}
{% endif %}
let app = new Vue({ let app = new Vue({
delimiters: ['[[', ']]'], delimiters: ['[[', ']]'],

View File

@ -246,6 +246,7 @@
</div> </div>
<script src="{% url 'javascript-catalog' %}"></script>
<script type="application/javascript"> <script type="application/javascript">
let csrftoken = Cookies.get('csrftoken'); let csrftoken = Cookies.get('csrftoken');
Vue.http.headers.common['X-CSRFToken'] = csrftoken; Vue.http.headers.common['X-CSRFToken'] = csrftoken;
@ -283,6 +284,8 @@
this.searchKeywords('') this.searchKeywords('')
this.searchUnits('') this.searchUnits('')
this.searchIngredients('') this.searchIngredients('')
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!'), 'danger')
}, },
methods: { methods: {
makeToast: function (title, message, variant = null) { makeToast: function (title, message, variant = null) {
@ -305,12 +308,12 @@
this.error = err.data this.error = err.data
this.loading = false this.loading = false
console.log(err) console.log(err)
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
}) })
}, },
importRecipe: function () { importRecipe: function () {
if (this.importing_recipe) { if (this.importing_recipe) {
this.makeToast('{% trans 'Error' %}', '{% trans 'Already importing the selected recipe, please wait!' %}', 'danger') this.makeToast(gettext('Error'), gettext('Already importing the selected recipe, please wait!'), 'danger')
return; return;
} }
this.importing_recipe = true this.importing_recipe = true
@ -319,7 +322,7 @@
window.location.href = response.data window.location.href = response.data
}).catch((err) => { }).catch((err) => {
console.log(err); console.log(err);
this.makeToast('{% trans 'Error' %}', '{% trans 'An error occurred while trying to import this recipe!' %}' + err.bodyText, 'danger') this.makeToast('{% trans 'Error' %}', gettext('An error occurred while trying to import this recipe!') + err.bodyText, 'danger')
}) })
}, },
deleteIngredient: function (i) { deleteIngredient: function (i) {
@ -363,7 +366,7 @@
this.keywords_loading = false this.keywords_loading = false
}).catch((err) => { }).catch((err) => {
console.log(err) console.log(err)
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') this.makeToast('{% trans 'Error' %}', gettext('There was an error loading a resource!') + err.bodyText, 'danger')
}) })
}, },
searchUnits: function (query) { searchUnits: function (query) {
@ -381,7 +384,7 @@
this.units_loading = false this.units_loading = false
}).catch((err) => { }).catch((err) => {
console.log(err) console.log(err)
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') this.makeToast('{% trans 'Error' %}', gettext('There was an error loading a resource!') + err.bodyText, 'danger')
}) })
}, },
searchIngredients: function (query) { searchIngredients: function (query) {
@ -400,7 +403,7 @@
this.ingredients_loading = false this.ingredients_loading = false
}).catch((err) => { }).catch((err) => {
console.log(err) console.log(err)
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') this.makeToast('{% trans 'Error' %}', gettext('There was an error loading a resource!') + err.bodyText, 'danger')
}) })
}, },
} }

View File

@ -46,13 +46,15 @@ def recipe_rating(recipe, user):
rating = recipe.cooklog_set.filter(created_by=user).aggregate(Avg('rating')) rating = recipe.cooklog_set.filter(created_by=user).aggregate(Avg('rating'))
if rating['rating__avg']: if rating['rating__avg']:
rating_stars = '' rating_stars = '<span style="display: inline-block;">'
for i in range(int(rating['rating__avg'])): for i in range(int(rating['rating__avg'])):
rating_stars = rating_stars + '<i class="fas fa-star fa-xs"></i>' rating_stars = rating_stars + '<i class="fas fa-star fa-xs"></i>'
if rating['rating__avg'] % 1 >= 0.5: if rating['rating__avg'] % 1 >= 0.5:
rating_stars = rating_stars + '<i class="fas fa-star-half-alt fa-xs"></i>' rating_stars = rating_stars + '<i class="fas fa-star-half-alt fa-xs"></i>'
rating_stars += '</span>'
return rating_stars return rating_stars
else: else:
return '' return ''

View File

@ -7,7 +7,7 @@ from django.contrib.auth import update_session_auth_hash, authenticate
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.password_validation import validate_password from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.models import Q from django.db.models import Q, Avg
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.utils import timezone from django.utils import timezone
@ -83,7 +83,8 @@ def recipe_view(request, pk, share=None):
if request.method == "POST": if request.method == "POST":
if not request.user.is_authenticated: if not request.user.is_authenticated:
messages.add_message(request, messages.ERROR, _('You do not have the required permissions to perform this action!')) messages.add_message(request, messages.ERROR,
_('You do not have the required permissions to perform this action!'))
return HttpResponseRedirect(reverse('view_recipe', kwargs={'pk': recipe.pk, 'share': share})) return HttpResponseRedirect(reverse('view_recipe', kwargs={'pk': recipe.pk, 'share': share}))
comment_form = CommentForm(request.POST, prefix='comment') comment_form = CommentForm(request.POST, prefix='comment')
@ -110,13 +111,17 @@ def recipe_view(request, pk, share=None):
comment_form = CommentForm() comment_form = CommentForm()
bookmark_form = RecipeBookEntryForm() bookmark_form = RecipeBookEntryForm()
user_servings = CookLog.objects.filter(recipe=recipe, created_by=request.user,
servings__gt=0).all().aggregate(Avg('servings'))['servings__avg']
if request.user.is_authenticated: if request.user.is_authenticated:
if not ViewLog.objects.filter(recipe=recipe).filter(created_by=request.user).filter(created_at__gt=(timezone.now() - timezone.timedelta(minutes=5))).exists(): if not ViewLog.objects.filter(recipe=recipe).filter(created_by=request.user).filter(
created_at__gt=(timezone.now() - timezone.timedelta(minutes=5))).exists():
ViewLog.objects.create(recipe=recipe, created_by=request.user) ViewLog.objects.create(recipe=recipe, created_by=request.user)
return render(request, 'recipe_view.html', return render(request, 'recipe_view.html',
{'recipe': recipe, 'comments': comments, 'comment_form': comment_form, {'recipe': recipe, 'comments': comments, 'comment_form': comment_form,
'bookmark_form': bookmark_form, 'share': share}) 'bookmark_form': bookmark_form, 'share': share, 'user_servings': user_servings})
@group_required('user') @group_required('user')
@ -158,7 +163,8 @@ def meal_plan_entry(request, pk):
messages.add_message(request, messages.ERROR, _('You do not have the required permissions to view this page!')) messages.add_message(request, messages.ERROR, _('You do not have the required permissions to view this page!'))
return HttpResponseRedirect(reverse_lazy('index')) return HttpResponseRedirect(reverse_lazy('index'))
same_day_plan = MealPlan.objects.filter(date=plan.date).exclude(pk=plan.pk).filter(Q(created_by=request.user) | Q(shared=request.user)).order_by('meal_type').all() same_day_plan = MealPlan.objects.filter(date=plan.date).exclude(pk=plan.pk).filter(
Q(created_by=request.user) | Q(shared=request.user)).order_by('meal_type').all()
return render(request, 'meal_plan_entry.html', {'plan': plan, 'same_day_plan': same_day_plan}) return render(request, 'meal_plan_entry.html', {'plan': plan, 'same_day_plan': same_day_plan})
@ -231,7 +237,9 @@ def user_settings(request):
if (api_token := Token.objects.filter(user=request.user).first()) is None: if (api_token := Token.objects.filter(user=request.user).first()) is None:
api_token = Token.objects.create(user=request.user) api_token = Token.objects.create(user=request.user)
return render(request, 'settings.html', {'preference_form': preference_form, 'user_name_form': user_name_form, 'password_form': password_form, 'api_token': api_token}) return render(request, 'settings.html',
{'preference_form': preference_form, 'user_name_form': user_name_form, 'password_form': password_form,
'api_token': api_token})
@group_required('guest') @group_required('guest')
@ -243,16 +251,20 @@ def history(request):
@group_required('admin') @group_required('admin')
def system(request): def system(request):
postgres = False if (settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2' or settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql') else True postgres = False if (settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2' or
settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql') else True
secret_key = False if os.getenv('SECRET_KEY') else True secret_key = False if os.getenv('SECRET_KEY') else True
return render(request, 'system.html', {'gunicorn_media': settings.GUNICORN_MEDIA, 'debug': settings.DEBUG, 'postgres': postgres, 'version': VERSION_NUMBER, 'ref': BUILD_REF, 'secret_key': secret_key}) return render(request, 'system.html',
{'gunicorn_media': settings.GUNICORN_MEDIA, 'debug': settings.DEBUG, 'postgres': postgres,
'version': VERSION_NUMBER, 'ref': BUILD_REF, 'secret_key': secret_key})
def setup(request): def setup(request):
if User.objects.count() > 0 or 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS: if User.objects.count() > 0 or 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS:
messages.add_message(request, messages.ERROR, _('The setup page can only be used to create the first user! If you have forgotten your superuser credentials please consult the django documentation on how to reset passwords.')) messages.add_message(request, messages.ERROR, _(
'The setup page can only be used to create the first user! If you have forgotten your superuser credentials please consult the django documentation on how to reset passwords.'))
return HttpResponseRedirect(reverse('login')) return HttpResponseRedirect(reverse('login'))
if request.method == 'POST': if request.method == 'POST':