sandwitches 2.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. sandwitches/__init__.py +6 -0
  2. sandwitches/admin.py +69 -0
  3. sandwitches/api.py +207 -0
  4. sandwitches/asgi.py +16 -0
  5. sandwitches/feeds.py +23 -0
  6. sandwitches/forms.py +196 -0
  7. sandwitches/locale/nl/LC_MESSAGES/django.mo +0 -0
  8. sandwitches/locale/nl/LC_MESSAGES/django.po +1010 -0
  9. sandwitches/migrations/0001_initial.py +328 -0
  10. sandwitches/migrations/0002_historicalrecipe_servings_recipe_servings.py +27 -0
  11. sandwitches/migrations/0003_setting.py +35 -0
  12. sandwitches/migrations/0004_alter_setting_ai_api_key_and_more.py +37 -0
  13. sandwitches/migrations/0005_rating_comment.py +17 -0
  14. sandwitches/migrations/0006_historicalrecipe_is_highlighted_and_more.py +22 -0
  15. sandwitches/migrations/__init__.py +0 -0
  16. sandwitches/models.py +218 -0
  17. sandwitches/settings.py +220 -0
  18. sandwitches/storage.py +114 -0
  19. sandwitches/tasks.py +115 -0
  20. sandwitches/templates/admin/admin_base.html +118 -0
  21. sandwitches/templates/admin/confirm_delete.html +23 -0
  22. sandwitches/templates/admin/dashboard.html +262 -0
  23. sandwitches/templates/admin/rating_list.html +38 -0
  24. sandwitches/templates/admin/recipe_form.html +184 -0
  25. sandwitches/templates/admin/recipe_list.html +64 -0
  26. sandwitches/templates/admin/tag_form.html +30 -0
  27. sandwitches/templates/admin/tag_list.html +37 -0
  28. sandwitches/templates/admin/task_detail.html +91 -0
  29. sandwitches/templates/admin/task_list.html +41 -0
  30. sandwitches/templates/admin/user_form.html +37 -0
  31. sandwitches/templates/admin/user_list.html +60 -0
  32. sandwitches/templates/base.html +94 -0
  33. sandwitches/templates/base_beer.html +57 -0
  34. sandwitches/templates/components/carousel_scripts.html +59 -0
  35. sandwitches/templates/components/favorites_search_form.html +85 -0
  36. sandwitches/templates/components/footer.html +14 -0
  37. sandwitches/templates/components/ingredients_scripts.html +50 -0
  38. sandwitches/templates/components/ingredients_section.html +11 -0
  39. sandwitches/templates/components/instructions_section.html +9 -0
  40. sandwitches/templates/components/language_dialog.html +26 -0
  41. sandwitches/templates/components/navbar.html +27 -0
  42. sandwitches/templates/components/rating_section.html +66 -0
  43. sandwitches/templates/components/recipe_header.html +32 -0
  44. sandwitches/templates/components/search_form.html +106 -0
  45. sandwitches/templates/components/search_scripts.html +98 -0
  46. sandwitches/templates/components/side_menu.html +35 -0
  47. sandwitches/templates/components/user_menu.html +10 -0
  48. sandwitches/templates/detail.html +178 -0
  49. sandwitches/templates/favorites.html +42 -0
  50. sandwitches/templates/index.html +76 -0
  51. sandwitches/templates/login.html +57 -0
  52. sandwitches/templates/partials/recipe_list.html +87 -0
  53. sandwitches/templates/recipe_form.html +119 -0
  54. sandwitches/templates/setup.html +105 -0
  55. sandwitches/templates/signup.html +133 -0
  56. sandwitches/templatetags/__init__.py +0 -0
  57. sandwitches/templatetags/custom_filters.py +15 -0
  58. sandwitches/templatetags/markdown_extras.py +17 -0
  59. sandwitches/urls.py +109 -0
  60. sandwitches/utils.py +222 -0
  61. sandwitches/views.py +647 -0
  62. sandwitches/wsgi.py +16 -0
  63. sandwitches-2.2.0.dist-info/METADATA +104 -0
  64. sandwitches-2.2.0.dist-info/RECORD +65 -0
  65. sandwitches-2.2.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,6 @@
1
+ """Top-level package for sandwitches."""
2
+
3
+ from importlib.metadata import version
4
+
5
+ __version__ = version("sandwitches") # Matches the 'name' in pyproject.toml
6
+ __author__ = """Martyn van Dijke"""
sandwitches/admin.py ADDED
@@ -0,0 +1,69 @@
1
+ from django.contrib import admin
2
+ from django.contrib.auth import get_user_model
3
+ from django.contrib.auth.admin import UserAdmin
4
+ from .models import Recipe, Tag, Rating, Setting
5
+ from django.utils.html import format_html
6
+ from import_export import resources
7
+ from import_export.admin import ImportExportModelAdmin
8
+ from solo.admin import SingletonModelAdmin
9
+ from .forms import SettingForm
10
+
11
+
12
+ class RecipeResource(resources.ModelResource):
13
+ class Meta:
14
+ model = Recipe
15
+
16
+
17
+ class TagResource(resources.ModelResource):
18
+ class Meta:
19
+ model = Tag
20
+
21
+
22
+ class RatingResource(resources.ModelResource):
23
+ class Meta:
24
+ model = Rating
25
+
26
+
27
+ User = get_user_model()
28
+
29
+
30
+ @admin.register(Setting)
31
+ class SettingAdmin(SingletonModelAdmin):
32
+ form = SettingForm
33
+
34
+
35
+ @admin.register(User)
36
+ class CustomUserAdmin(UserAdmin):
37
+ fieldsets = UserAdmin.fieldsets + (
38
+ (None, {"fields": ("avatar", "bio", "language", "favorites")}),
39
+ )
40
+
41
+
42
+ @admin.register(Recipe)
43
+ class RecipeAdmin(ImportExportModelAdmin):
44
+ resource_classes = [RecipeResource]
45
+ list_display = ("title", "uploaded_by", "created_at", "is_highlighted", "show_url")
46
+ list_editable = ("is_highlighted",)
47
+ readonly_fields = ("created_at", "updated_at")
48
+
49
+ def save_model(self, request, obj, form, change):
50
+ # set uploaded_by automatically when creating in admin
51
+ if not change and not obj.uploaded_by:
52
+ obj.uploaded_by = request.user
53
+ super().save_model(request, obj, form, change)
54
+
55
+ def show_url(self, obj):
56
+ url = obj.get_absolute_url()
57
+ return format_html("<a href='{url}'>{url}</a>", url=url)
58
+
59
+ show_url.short_description = "Recipe Link" # ty:ignore[unresolved-attribute]
60
+
61
+
62
+ @admin.register(Tag)
63
+ class TagAdmin(ImportExportModelAdmin):
64
+ resource_classes = [TagResource]
65
+
66
+
67
+ @admin.register(Rating)
68
+ class RatingAdmin(ImportExportModelAdmin):
69
+ resource_classes = [RatingResource]
sandwitches/api.py ADDED
@@ -0,0 +1,207 @@
1
+ import logging
2
+ from ninja import NinjaAPI
3
+ from .models import Recipe, Tag, Setting, Rating
4
+ from django.contrib.auth import get_user_model
5
+ from .utils import (
6
+ parse_ingredient_line,
7
+ scale_ingredient,
8
+ format_scaled_ingredient,
9
+ ) # Import utility functions
10
+
11
+ from ninja import ModelSchema
12
+ from ninja import Schema
13
+ from django.shortcuts import get_object_or_404
14
+ from datetime import date
15
+ import random
16
+ from typing import List, Optional # Import typing hints
17
+
18
+ from ninja.security import django_auth
19
+
20
+ from . import __version__
21
+
22
+ # Get the custom User model
23
+ User = get_user_model()
24
+
25
+ api = NinjaAPI(version=__version__)
26
+
27
+
28
+ class UserPublicSchema(ModelSchema):
29
+ class Meta:
30
+ model = User
31
+ fields = ["username", "first_name", "last_name", "avatar"]
32
+
33
+
34
+ class RecipeSchema(ModelSchema):
35
+ favorited_by: List[UserPublicSchema] = []
36
+
37
+ class Meta:
38
+ model = Recipe
39
+ fields = "__all__"
40
+
41
+
42
+ class TagSchema(ModelSchema):
43
+ class Meta:
44
+ model = Tag
45
+ fields = "__all__"
46
+
47
+
48
+ class UserSchema(ModelSchema):
49
+ class Meta:
50
+ model = User
51
+ exclude = ["password", "last_login", "user_permissions"]
52
+
53
+
54
+ class SettingSchema(ModelSchema):
55
+ class Meta:
56
+ model = Setting
57
+ fields = "__all__"
58
+
59
+
60
+ class RatingSchema(ModelSchema):
61
+ user: UserPublicSchema
62
+
63
+ class Meta:
64
+ model = Rating
65
+ fields = "__all__"
66
+
67
+
68
+ class Error(Schema):
69
+ message: str
70
+
71
+
72
+ class RatingResponseSchema(Schema):
73
+ average: float
74
+ count: int
75
+
76
+
77
+ class ScaledIngredient(Schema): # New Schema for scaled ingredients
78
+ original_line: str
79
+ scaled_line: str
80
+ quantity: Optional[float]
81
+ unit: Optional[str]
82
+ name: Optional[str]
83
+
84
+
85
+ @api.get("ping")
86
+ def ping(request):
87
+ return {"status": "ok", "message": "pong"}
88
+
89
+
90
+ @api.get("v1/settings", response=SettingSchema)
91
+ def get_settings(request):
92
+ return Setting.objects.get() # ty:ignore[unresolved-attribute]
93
+
94
+
95
+ @api.post("v1/settings", auth=django_auth, response={200: SettingSchema, 403: Error})
96
+ def update_settings(request, payload: SettingSchema):
97
+ if not request.user.is_staff:
98
+ return 403, {"message": "You are not authorized to perform this action"}
99
+ settings = Setting.objects.get() # ty:ignore[unresolved-attribute]
100
+ for attr, value in payload.dict().items():
101
+ setattr(settings, attr, value)
102
+ settings.save()
103
+ return settings
104
+
105
+
106
+ @api.get("v1/me", response={200: UserSchema, 403: Error})
107
+ def me(request):
108
+ if not request.user.is_authenticated:
109
+ return 403, {"message": "Please sign in first"}
110
+ return request.user
111
+
112
+
113
+ @api.get("v1/users", auth=django_auth, response=list[UserSchema])
114
+ def users(request):
115
+ return User.objects.all()
116
+
117
+
118
+ @api.get("v1/recipes", response=list[RecipeSchema])
119
+ def get_recipes(request):
120
+ return Recipe.objects.all().prefetch_related("favorited_by") # ty:ignore[unresolved-attribute]
121
+
122
+
123
+ @api.get("v1/recipes/{recipe_id}", response=RecipeSchema)
124
+ def get_recipe(request, recipe_id: int):
125
+ recipe = get_object_or_404(
126
+ Recipe.objects.prefetch_related("favorited_by"), # ty:ignore[unresolved-attribute]
127
+ id=recipe_id,
128
+ )
129
+ return recipe
130
+
131
+
132
+ @api.get("v1/recipes/{recipe_id}/scale-ingredients", response=List[ScaledIngredient])
133
+ def scale_recipe_ingredients(request, recipe_id: int, target_servings: int):
134
+ recipe = get_object_or_404(Recipe, id=recipe_id)
135
+
136
+ # Ensure target_servings is at least 1
137
+ target_servings = max(1, target_servings)
138
+
139
+ current_servings = recipe.servings
140
+ if not current_servings or current_servings <= 0:
141
+ current_servings = 1
142
+
143
+ ingredient_lines = [
144
+ line.strip() for line in (recipe.ingredients or "").split("\n") if line.strip()
145
+ ]
146
+
147
+ scaled_ingredients_output = []
148
+ for line in ingredient_lines:
149
+ try:
150
+ parsed = parse_ingredient_line(line)
151
+ scaled = scale_ingredient(parsed, current_servings, target_servings)
152
+ formatted_line = format_scaled_ingredient(scaled)
153
+
154
+ scaled_ingredients_output.append(
155
+ ScaledIngredient(
156
+ original_line=line,
157
+ scaled_line=formatted_line,
158
+ quantity=scaled.get("quantity"),
159
+ unit=scaled.get("unit"),
160
+ name=scaled.get("name"),
161
+ )
162
+ )
163
+ except Exception as e:
164
+ # Fallback for lines that fail to parse/scale
165
+ scaled_ingredients_output.append(
166
+ ScaledIngredient(
167
+ original_line=line,
168
+ scaled_line=line,
169
+ quantity=None,
170
+ unit=None,
171
+ name=line,
172
+ )
173
+ )
174
+ logging.warning(f"Failed to scale ingredient line '{line}': {e}")
175
+
176
+ return scaled_ingredients_output
177
+
178
+
179
+ @api.get("v1/recipe-of-the-day", response=RecipeSchema)
180
+ def get_recipe_of_the_day(request):
181
+ recipes = list(Recipe.objects.all().prefetch_related("favorited_by")) # ty:ignore[unresolved-attribute]
182
+ if not recipes:
183
+ return None
184
+ today = date.today()
185
+ random.seed(today.toordinal())
186
+ recipe = random.choice(recipes)
187
+ return recipe
188
+
189
+
190
+ @api.get("v1/recipes/{recipe_id}/rating", response=RatingResponseSchema)
191
+ def get_recipe_rating(request, recipe_id: int):
192
+ recipe = get_object_or_404(Recipe, id=recipe_id)
193
+ return {
194
+ "average": recipe.average_rating(),
195
+ "count": recipe.rating_count(),
196
+ }
197
+
198
+
199
+ @api.get("v1/tags", response=list[TagSchema])
200
+ def get_tags(request):
201
+ return Tag.objects.all() # ty:ignore[unresolved-attribute]
202
+
203
+
204
+ @api.get("v1/tags/{tag_id}", response=TagSchema)
205
+ def get_tag(request, tag_id: int):
206
+ tag = get_object_or_404(Tag, id=tag_id)
207
+ return tag
sandwitches/asgi.py ADDED
@@ -0,0 +1,16 @@
1
+ """
2
+ ASGI config for sandwitches project.
3
+
4
+ It exposes the ASGI callable as a module-level variable named ``application``.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
8
+ """
9
+
10
+ import os
11
+
12
+ from django.core.asgi import get_asgi_application
13
+
14
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sandwitches.settings")
15
+
16
+ application = get_asgi_application()
sandwitches/feeds.py ADDED
@@ -0,0 +1,23 @@
1
+ from django.contrib.syndication.views import Feed
2
+ from django.urls import reverse_lazy
3
+ from .models import Recipe
4
+
5
+
6
+ class LatestRecipesFeed(Feed):
7
+ title = "Sandwitches - Latest Recipes"
8
+ link = reverse_lazy(
9
+ "index"
10
+ ) # This should point to the homepage or a list of recipes
11
+ description = "Updates on the newest recipes added to Sandwitches."
12
+
13
+ def items(self):
14
+ return Recipe.objects.order_by("-created_at")[:5] # ty:ignore[unresolved-attribute]
15
+
16
+ def item_title(self, item):
17
+ return item.title
18
+
19
+ def item_description(self, item):
20
+ return item.description
21
+
22
+ def item_link(self, item):
23
+ return item.get_absolute_url()
sandwitches/forms.py ADDED
@@ -0,0 +1,196 @@
1
+ from django import forms
2
+ from django.conf import settings
3
+ from django.contrib.auth import get_user_model
4
+ from django.contrib.auth.forms import UserCreationForm
5
+ from django.utils.translation import gettext_lazy as _
6
+ from .models import Recipe, Tag, Setting
7
+
8
+ User = get_user_model()
9
+
10
+
11
+ class BaseUserFormMixin:
12
+ """Mixin to handle common password validation and user field processing."""
13
+
14
+ def clean_passwords(self, cleaned_data):
15
+ p1 = cleaned_data.get("password1")
16
+ p2 = cleaned_data.get("password2")
17
+ if p1 and p2 and p1 != p2:
18
+ raise forms.ValidationError(_("Passwords do not match."))
19
+ return cleaned_data
20
+
21
+ def _set_user_attributes(self, user, data):
22
+ """Helper to apply optional name fields."""
23
+ user.first_name = data.get("first_name", "")
24
+ user.last_name = data.get("last_name", "")
25
+ user.save()
26
+ return user
27
+
28
+
29
+ class AdminSetupForm(forms.ModelForm, BaseUserFormMixin):
30
+ password1 = forms.CharField(widget=forms.PasswordInput, label=_("Password"))
31
+ password2 = forms.CharField(widget=forms.PasswordInput, label=_("Confirm Password"))
32
+
33
+ class Meta:
34
+ model = User
35
+ fields = ("username", "first_name", "last_name", "email")
36
+
37
+ def clean(self):
38
+ cleaned_data = super().clean()
39
+ return self.clean_passwords(cleaned_data)
40
+
41
+ def save(self, commit=True):
42
+ data = self.cleaned_data
43
+ user = User.objects.create_superuser(
44
+ username=data["username"], email=data["email"], password=data["password1"]
45
+ )
46
+ return self._set_user_attributes(user, data)
47
+
48
+
49
+ class UserSignupForm(UserCreationForm, BaseUserFormMixin):
50
+ language = forms.ChoiceField(
51
+ choices=settings.LANGUAGES,
52
+ label=_("Preferred language"),
53
+ initial=settings.LANGUAGE_CODE,
54
+ )
55
+ avatar = forms.ImageField(label=_("Profile Image"), required=False)
56
+ bio = forms.CharField(
57
+ widget=forms.Textarea(attrs={"rows": 3}), label=_("Bio"), required=False
58
+ )
59
+
60
+ class Meta(UserCreationForm.Meta):
61
+ model = User
62
+ fields = (
63
+ "username",
64
+ "first_name",
65
+ "last_name",
66
+ "email",
67
+ "language",
68
+ "avatar",
69
+ "bio",
70
+ )
71
+
72
+ def clean(self):
73
+ return super().clean()
74
+
75
+ def save(self, commit=True):
76
+ user = super().save(commit=False)
77
+ user.is_superuser = False
78
+ user.is_staff = False
79
+ # Explicitly save the extra fields if they aren't automatically handled by ModelForm save (they should be if in Meta.fields)
80
+ user.language = self.cleaned_data["language"]
81
+ user.avatar = self.cleaned_data["avatar"]
82
+ user.bio = self.cleaned_data["bio"]
83
+ if commit:
84
+ user.save()
85
+ return user
86
+
87
+
88
+ class UserEditForm(forms.ModelForm):
89
+ class Meta:
90
+ model = User
91
+ fields = (
92
+ "username",
93
+ "first_name",
94
+ "last_name",
95
+ "email",
96
+ "is_staff",
97
+ "is_active",
98
+ "language",
99
+ "avatar",
100
+ "bio",
101
+ )
102
+
103
+
104
+ class TagForm(forms.ModelForm):
105
+ class Meta:
106
+ model = Tag
107
+ fields = ("name",)
108
+
109
+
110
+ class RecipeForm(forms.ModelForm):
111
+ tags_string = forms.CharField(
112
+ required=False,
113
+ label=_("Tags (comma separated)"),
114
+ widget=forms.TextInput(attrs={"placeholder": _("e.g. spicy, vegan, quick")}),
115
+ )
116
+ rotation = forms.IntegerField(widget=forms.HiddenInput(), initial=0, required=False)
117
+
118
+ class Meta:
119
+ model = Recipe
120
+ fields = [
121
+ "title",
122
+ "image",
123
+ "uploaded_by",
124
+ "description",
125
+ "ingredients",
126
+ "instructions",
127
+ ]
128
+ widgets = {
129
+ "image": forms.FileInput(),
130
+ }
131
+
132
+ def __init__(self, *args, **kwargs):
133
+ super().__init__(*args, **kwargs)
134
+ if self.instance.pk:
135
+ self.fields["tags_string"].initial = ", ".join(
136
+ self.instance.tags.values_list("name", flat=True)
137
+ )
138
+
139
+ def save(self, commit=True):
140
+ recipe = super().save(commit=commit)
141
+
142
+ # Handle rotation if an image exists and rotation is requested
143
+ rotation = self.cleaned_data.get("rotation", 0)
144
+ if rotation != 0 and recipe.image:
145
+ try:
146
+ from PIL import Image as PILImage
147
+
148
+ img = PILImage.open(recipe.image.path)
149
+ # PIL rotates counter-clockwise by default, our 'rotation' is clockwise
150
+ img = img.rotate(-rotation, expand=True)
151
+ img.save(recipe.image.path)
152
+ except Exception as e:
153
+ print(f"Error rotating image: {e}")
154
+
155
+ if commit:
156
+ recipe.set_tags_from_string(self.cleaned_data.get("tags_string", ""))
157
+ else:
158
+ # We'll need to handle this in the view if commit=False
159
+ self.save_m2m = lambda: recipe.set_tags_from_string(
160
+ self.cleaned_data.get("tags_string", "")
161
+ )
162
+ return recipe
163
+
164
+
165
+ class RatingForm(forms.Form):
166
+ """Form for rating recipes (0-10) with an optional comment."""
167
+
168
+ score = forms.FloatField(
169
+ min_value=0.0,
170
+ max_value=10.0,
171
+ widget=forms.NumberInput(
172
+ attrs={"step": "0.1", "min": "0", "max": "10", "class": "slider"}
173
+ ),
174
+ label=_("Your rating"),
175
+ )
176
+ comment = forms.CharField(
177
+ widget=forms.Textarea(attrs={"rows": 2}),
178
+ label=_("Comment (optional)"),
179
+ required=False,
180
+ )
181
+
182
+
183
+ class SettingForm(forms.ModelForm):
184
+ class Meta:
185
+ model = Setting
186
+ fields = [
187
+ "site_name",
188
+ "site_description",
189
+ "email",
190
+ "ai_connection_point",
191
+ "ai_model",
192
+ "ai_api_key",
193
+ ]
194
+ widgets = {
195
+ "ai_api_key": forms.PasswordInput(render_value=True),
196
+ }