Skip to content

Commit

Permalink
Merge branch 'develop' into facet-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Jan 18, 2022
2 parents 4e0cc34 + ef4ce62 commit ba1f10c
Show file tree
Hide file tree
Showing 7 changed files with 1,610 additions and 1,429 deletions.
49 changes: 46 additions & 3 deletions cookbook/admin.py
Expand Up @@ -15,7 +15,7 @@
Recipe, RecipeBook, RecipeBookEntry, RecipeImport, SearchPreference, ShareLink,
ShoppingList, ShoppingListEntry, ShoppingListRecipe, Space, Step, Storage,
Supermarket, SupermarketCategory, SupermarketCategoryRelation, Sync, SyncLog,
TelegramBot, Unit, UserFile, UserPreference, ViewLog)
TelegramBot, Unit, UserFile, UserPreference, ViewLog, Automation)


class CustomUserAdmin(UserAdmin):
Expand All @@ -29,11 +29,52 @@ def has_add_permission(self, request, obj=None):
admin.site.unregister(Group)


@admin.action(description='Delete all data from a space')
def delete_space_action(modeladmin, request, queryset):
for space in queryset:
CookLog.objects.filter(space=space).delete()
ViewLog.objects.filter(space=space).delete()
ImportLog.objects.filter(space=space).delete()
BookmarkletImport.objects.filter(space=space).delete()

Comment.objects.filter(recipe__space=space).delete()
Keyword.objects.filter(space=space).delete()
Ingredient.objects.filter(space=space).delete()
Food.objects.filter(space=space).delete()
Unit.objects.filter(space=space).delete()
Step.objects.filter(space=space).delete()
NutritionInformation.objects.filter(space=space).delete()
RecipeBookEntry.objects.filter(book__space=space).delete()
RecipeBook.objects.filter(space=space).delete()
MealType.objects.filter(space=space).delete()
MealPlan.objects.filter(space=space).delete()
ShareLink.objects.filter(space=space).delete()
Recipe.objects.filter(space=space).delete()

RecipeImport.objects.filter(space=space).delete()
SyncLog.objects.filter(sync__space=space).delete()
Sync.objects.filter(space=space).delete()
Storage.objects.filter(space=space).delete()

ShoppingListEntry.objects.filter(shoppinglist__space=space).delete()
ShoppingListRecipe.objects.filter(shoppinglist__space=space).delete()
ShoppingList.objects.filter(space=space).delete()

SupermarketCategoryRelation.objects.filter(supermarket__space=space).delete()
SupermarketCategory.objects.filter(space=space).delete()
Supermarket.objects.filter(space=space).delete()

InviteLink.objects.filter(space=space).delete()
UserFile.objects.filter(space=space).delete()
Automation.objects.filter(space=space).delete()


class SpaceAdmin(admin.ModelAdmin):
list_display = ('name', 'created_by', 'max_recipes', 'max_users', 'max_file_storage_mb', 'allow_sharing')
search_fields = ('name', 'created_by__username')
list_filter = ('max_recipes', 'max_users', 'max_file_storage_mb', 'allow_sharing')
date_hierarchy = 'created_at'
actions = [delete_space_action]


admin.site.register(Space, SpaceAdmin)
Expand Down Expand Up @@ -128,7 +169,7 @@ def sort_tree(modeladmin, request, queryset):
class KeywordAdmin(TreeAdmin):
form = movenodeform_factory(Keyword)
ordering = ('space', 'path',)
search_fields = ('name', )
search_fields = ('name',)
actions = [sort_tree, enable_tree_sorting, disable_tree_sorting]


Expand Down Expand Up @@ -171,13 +212,15 @@ def created_by(obj):
admin.site.register(Recipe, RecipeAdmin)

admin.site.register(Unit)


# admin.site.register(FoodInheritField)


class FoodAdmin(TreeAdmin):
form = movenodeform_factory(Keyword)
ordering = ('space', 'path',)
search_fields = ('name', )
search_fields = ('name',)
actions = [sort_tree, enable_tree_sorting, disable_tree_sorting]


Expand Down
3 changes: 2 additions & 1 deletion cookbook/serializer.py
Expand Up @@ -165,9 +165,10 @@ class Meta:
read_only_fields = ['id']


class UserPreferenceSerializer(serializers.ModelSerializer):
class UserPreferenceSerializer(WritableNestedModelSerializer):
food_inherit_default = FoodInheritFieldSerializer(source='space.food_inherit', many=True, allow_null=True, required=False, read_only=True)
plan_share = UserNameSerializer(many=True, allow_null=True, required=False, read_only=True)
shopping_share = UserNameSerializer(many=True, allow_null=True, required=False)

def create(self, validated_data):
if not validated_data.get('user', None):
Expand Down
12 changes: 3 additions & 9 deletions cookbook/views/telegram.py
Expand Up @@ -45,21 +45,15 @@ def hook(request, token):
tb.save()

if tb.chat_id == str(data['message']['chat']['id']):
sl = ShoppingList.objects.filter(Q(created_by=tb.created_by)).filter(finished=False, space=tb.space).order_by('-created_at').first()
if not sl:
sl = ShoppingList.objects.create(created_by=tb.created_by, space=tb.space)

request.space = tb.space # TODO this is likely a bad idea. Verify and test
request.user = tb.created_by
ingredient_parser = IngredientParser(request, False)
amount, unit, ingredient, note = ingredient_parser.parse(data['message']['text'])
f = ingredient_parser.get_food(ingredient)
u = ingredient_parser.get_unit(unit)
sl.entries.add(
ShoppingListEntry.objects.create(
food=f, unit=u, amount=amount
)
)

ShoppingListEntry.objects.create(food=f, unit=u, amount=amount, created_by=request.user, space=request.space)

return JsonResponse({'data': data['message']['text']})
except Exception:
pass
Expand Down

0 comments on commit ba1f10c

Please sign in to comment.