Commit d1c23b9a authored by Floréal Cabanettes's avatar Floréal Cabanettes

Drop requirement of heavy dateparser python module + check post args on filter

parent 0585dbb9
......@@ -9,7 +9,6 @@ from main.config import RecipeConfig
from django_gravatar.helpers import get_gravatar_url
from .GenericView import GenericView
import locale
import dateparser
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import urllib.parse
......@@ -21,10 +20,12 @@ class RecipesView(View):
return a[0] + a[2]
def post(self, request, page=1):
post_params = ["filter-month", "filter-cat"]
args = dict(request.GET)
post_args = dict(request.POST)
del post_args["csrfmiddlewaretoken"]
args.update(post_args)
for post_param in post_params:
if post_param in post_args:
args[post_param] = post_args[post_param]
params = urllib.parse.urlencode(args, doseq=1)
url = reverse("recipes")
return HttpResponseRedirect(url + "?%s" % params)
......@@ -58,10 +59,8 @@ class RecipesView(View):
filter_cat = "all"
if "filter-month" in request.GET and request.GET["filter-month"] != "0":
filter_date = request.GET["filter-month"]
parsed_date = dateparser.parse(filter_date)
filter_date = filter_date.split(" ")
filter_date[1] = int(filter_date[1])
recipes = recipes.filter(pub_date__month=parsed_date.month, pub_date__year=parsed_date.year)
filter_date = list(map(int, filter_date.split("-")))
recipes = recipes.filter(pub_date__month=filter_date[0], pub_date__year=filter_date[1])
if "filter-cat" in request.GET and request.GET["filter-cat"] != "0":
filter_cat = request.GET["filter-cat"]
recipes = recipes.filter(category__name=filter_cat)
......
......@@ -38,7 +38,7 @@
<select class="form-control" id="sel-filter-month" name="filter-month">
<option value="0">{% trans "All dates" %}</option>
{% for date in all_dates %}
<option {% if filter_date.0 == date.1 and filter_date.1 == date.2 %}selected{% endif %}>{{ date.1 }} {{ date.2 }}</option>
<option value="{{ date.0 }}-{{ date.2 }}" {% if filter_date.0 == date.0 and filter_date.1 == date.2 %}selected{% endif %}>{{ date.1 }} {{ date.2 }}</option>
{% endfor %}
</select>
<select class="form-control" id="sel-filter-cats", name="filter-cat">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment