-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild_html_recipes.py
executable file
·59 lines (44 loc) · 2.1 KB
/
rebuild_html_recipes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import argparse
import os
import re
import django
from django.utils import timezone
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pywebcooking.settings')
django.setup()
from main.controllers import CRecipe
from main.models import Recipe
parser = argparse.ArgumentParser(description='Rebuild html for all recipes or for some recipes.')
parser.add_argument("--id", help="Id of the recipe to rebuild", type=int)
parser.add_argument("--slug", help="Slug of the recipe to rebuild")
parser.add_argument("--from-date", help="Rebuild html for recipes from the date specified.\nDate format: YYYY+MM-DD")
parser.add_argument("--to-date", help="Rebuild html for recipes to the date specified.\nDate format: YYYY+MM-DD")
args = parser.parse_args()
if args.id is not None and args.slug is not None:
print("ERROR: Parameters \"id\" and \"slug\" are mutually exclusive.")
exit(1)
recipes = Recipe.objects.all()
if args.id is not None:
recipes = recipes.filter(id=args.id)
elif args.slug is not None:
recipes = recipes.filter(slug=args.slug)
match_string = r"(\d{4})-(\d{2})-(\d{2})"
if args.from_date:
match = re.match(match_string, args.from_date)
if match:
recipes = recipes.filter(pub_date__gte=timezone.datetime(int(match.group(1)), int(match.group(2)),
int(match.group(3)),
tzinfo=timezone.get_current_timezone()))
if args.to_date:
match = re.match(match_string, args.to_date)
if match:
recipes = recipes.filter(pub_date__lte=timezone.datetime(int(match.group(1)), int(match.group(2)),
int(match.group(3)), 23, 59, 59,
tzinfo=timezone.get_current_timezone()))
if len(recipes) == 0:
print("ERROR: No recipe match to your query. Exciting...")
for recipe in recipes:
print("Processing recipe {0} ({1})...".format(recipe.title, recipe.id))
CRecipe.build_html_recipe(recipe)
print("")
print("DONE!")