def sync(request): if request.method == "POST": if not has_group_permission(request.user, ['admin']): messages.add_message( request, messages.ERROR, _('You do not have the required permissions to view this page!' )) return HttpResponseRedirect(reverse('data_sync')) form = SyncForm(request.POST) if form.is_valid(): new_path = Sync() new_path.path = form.cleaned_data['path'] new_path.storage = form.cleaned_data['storage'] new_path.last_checked = datetime.now() new_path.save() return redirect('data_sync') else: form = SyncForm() monitored_paths = SyncTable(Sync.objects.all()) RequestConfig(request, paginate={ 'per_page': 25 }).configure(monitored_paths) return render(request, 'batch/monitor.html', { 'form': form, 'monitored_paths': monitored_paths })
def search(request): if has_group_permission(request.user, ('guest',)): if request.user.userpreference.search_style == UserPreference.NEW: return search_v2(request) f = RecipeFilter(request.GET, queryset=Recipe.objects.filter(space=request.user.userpreference.space).all().order_by( Lower('name').asc()), space=request.space) if request.user.userpreference.search_style == UserPreference.LARGE: table = RecipeTable(f.qs) else: table = RecipeTableSmall(f.qs) RequestConfig(request, paginate={'per_page': 25}).configure(table) if request.GET == {} and request.user.userpreference.show_recent: qs = Recipe.objects.filter(viewlog__created_by=request.user).filter( space=request.user.userpreference.space).order_by('-viewlog__created_at').all() recent_list = [] for r in qs: if r not in recent_list: recent_list.append(r) if len(recent_list) >= 5: break last_viewed = RecipeTable(recent_list) else: last_viewed = None return render(request, 'index.html', {'recipes': table, 'filter': f, 'last_viewed': last_viewed}) else: if request.user.is_authenticated: return HttpResponseRedirect(reverse('view_no_group')) else: return HttpResponseRedirect(reverse('account_login') + '?next=' + request.path)
def old_search(request): if has_group_permission(request.user, ('guest',)): params = dict(request.GET) params['internal'] = None f = RecipeFilter(params, queryset=Recipe.objects.filter(space=request.user.userpreference.space).all().order_by('name'), space=request.space) return f.qs
def recipe_view(request, pk, share=None): with scopes_disabled(): recipe = get_object_or_404(Recipe, pk=pk) if not request.user.is_authenticated and not share_link_valid(recipe, share): messages.add_message(request, messages.ERROR, _('You do not have the required permissions to view this page!')) return HttpResponseRedirect(reverse('account_login') + '?next=' + request.path) if not (has_group_permission(request.user, ('guest',)) and recipe.space == request.space) and not share_link_valid(recipe, share): messages.add_message(request, messages.ERROR, _('You do not have the required permissions to view this page!')) return HttpResponseRedirect(reverse('index')) comments = Comment.objects.filter(recipe__space=request.space, recipe=recipe) if request.method == "POST": if not request.user.is_authenticated: 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})) comment_form = CommentForm(request.POST, prefix='comment') if comment_form.is_valid(): comment = Comment() comment.recipe = recipe comment.text = comment_form.cleaned_data['text'] comment.created_by = request.user comment.save() messages.add_message(request, messages.SUCCESS, _('Comment saved!')) comment_form = CommentForm() user_servings = None if request.user.is_authenticated: user_servings = CookLog.objects.filter( recipe=recipe, created_by=request.user, servings__gt=0, space=request.space, ).all().aggregate(Avg('servings'))['servings__avg'] if not user_servings: user_servings = 0 if request.user.is_authenticated: if not ViewLog.objects.filter(recipe=recipe, created_by=request.user, created_at__gt=(timezone.now() - timezone.timedelta(minutes=5)), space=request.space).exists(): ViewLog.objects.create(recipe=recipe, created_by=request.user, space=request.space) return render(request, 'recipe_view.html', {'recipe': recipe, 'comments': comments, 'comment_form': comment_form, 'share': share, 'user_servings': user_servings})
def sync(request): if request.space.max_recipes != 0 and Recipe.objects.filter( space=request.space ).count( ) >= request.space.max_recipes: # TODO move to central helper function messages.add_message( request, messages.WARNING, _('You have reached the maximum number of recipes for your space.') ) return HttpResponseRedirect(reverse('index')) if request.space.max_users != 0 and UserPreference.objects.filter( space=request.space).count() > request.space.max_users: messages.add_message( request, messages.WARNING, _('You have more users than allowed in your space.')) return HttpResponseRedirect(reverse('index')) if request.space.demo or settings.HOSTED: messages.add_message( request, messages.ERROR, _('This feature is not yet available in the hosted version of tandoor!' )) return redirect('index') if request.method == "POST": if not has_group_permission(request.user, ['admin']): messages.add_message( request, messages.ERROR, _('You do not have the required permissions to view this page!' )) return HttpResponseRedirect(reverse('data_sync')) form = SyncForm(request.POST, space=request.space) if form.is_valid(): new_path = Sync() new_path.path = form.cleaned_data['path'] new_path.storage = form.cleaned_data['storage'] new_path.last_checked = datetime.now() new_path.space = request.space new_path.save() return redirect('data_sync') else: form = SyncForm(space=request.space) monitored_paths = SyncTable(Sync.objects.filter(space=request.space).all()) RequestConfig(request, paginate={ 'per_page': 25 }).configure(monitored_paths) return render(request, 'batch/monitor.html', { 'form': form, 'monitored_paths': monitored_paths })
def sync(request): limit, msg = above_space_limit(request.space) if limit: messages.add_message(request, messages.WARNING, msg) return HttpResponseRedirect(reverse('index')) if request.space.demo or settings.HOSTED: messages.add_message( request, messages.ERROR, _('This feature is not yet available in the hosted version of tandoor!' )) return redirect('index') if request.method == "POST": if not has_group_permission(request.user, ['admin']): messages.add_message( request, messages.ERROR, _('You do not have the required permissions to view this page!' )) return HttpResponseRedirect(reverse('data_sync')) form = SyncForm(request.POST, space=request.space) if form.is_valid(): new_path = Sync() new_path.path = form.cleaned_data['path'] new_path.storage = form.cleaned_data['storage'] new_path.last_checked = datetime.now() new_path.space = request.space new_path.save() return redirect('data_sync') else: form = SyncForm(space=request.space) monitored_paths = SyncTable(Sync.objects.filter(space=request.space).all()) RequestConfig(request, paginate={ 'per_page': 25 }).configure(monitored_paths) return render(request, 'batch/monitor.html', { 'form': form, 'monitored_paths': monitored_paths })
def recipe_view(request, pk, share=None): recipe = get_object_or_404(Recipe, pk=pk) if not has_group_permission(request.user, ('guest',)) and not share_link_valid(recipe, share): messages.add_message( request, messages.ERROR, _('You do not have the required permissions to view this page!') ) return HttpResponseRedirect(reverse('view_no_group') + '?next=' + request.path) comments = Comment.objects.filter(recipe=recipe) if request.method == "POST": if not request.user.is_authenticated: messages.add_message( request, messages.ERROR, _('You do not have the required permissions to perform this action!') # noqa: E501 ) return HttpResponseRedirect( reverse( 'view_recipe', kwargs={'pk': recipe.pk, 'share': share} ) ) comment_form = CommentForm(request.POST, prefix='comment') if comment_form.is_valid(): comment = Comment() comment.recipe = recipe comment.text = comment_form.cleaned_data['text'] comment.created_by = request.user comment.save() messages.add_message( request, messages.SUCCESS, _('Comment saved!') ) bookmark_form = RecipeBookEntryForm(request.POST, prefix='bookmark') if bookmark_form.is_valid(): bookmark = RecipeBookEntry() bookmark.recipe = recipe bookmark.book = bookmark_form.cleaned_data['book'] try: bookmark.save() except IntegrityError as e: if 'UNIQUE constraint' in str(e.args): messages.add_message( request, messages.ERROR, _('This recipe is already linked to the book!') ) else: messages.add_message( request, messages.SUCCESS, _('Bookmark saved!') ) comment_form = CommentForm() user_servings = None if request.user.is_authenticated: user_servings = CookLog.objects.filter( recipe=recipe, created_by=request.user, servings__gt=0 ).all().aggregate(Avg('servings'))['servings__avg'] if not user_servings: user_servings = 0 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(): ViewLog.objects.create(recipe=recipe, created_by=request.user) return render(request, 'recipe_view.html', {'recipe': recipe, 'comments': comments, 'comment_form': comment_form, 'share': share, 'user_servings': user_servings})