mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
reworked MergeMixin to handle non-trees
This commit is contained in:
@@ -119,7 +119,6 @@ class FuzzyFilterMixin(ViewSetMixin):
|
||||
.filter(name__icontains=query).order_by('-exact')
|
||||
)
|
||||
|
||||
|
||||
updated_at = self.request.query_params.get('updated_at', None)
|
||||
if updated_at is not None:
|
||||
try:
|
||||
@@ -165,7 +164,12 @@ class MergeMixin(ViewSetMixin): # TODO update Units to use merge API
|
||||
if target in source.get_descendants_and_self():
|
||||
content = {'error': True, 'msg': _('Cannot merge with child object!')}
|
||||
return Response(content, status=status.HTTP_403_FORBIDDEN)
|
||||
isTree = True
|
||||
except AttributeError:
|
||||
# AttributeError probably means its not a tree, so can safely ignore
|
||||
isTree = False
|
||||
|
||||
try:
|
||||
for link in [field for field in source._meta.get_fields() if issubclass(type(field), ForeignObjectRel)]:
|
||||
linkManager = getattr(source, link.get_accessor_name())
|
||||
related = linkManager.all()
|
||||
@@ -182,9 +186,10 @@ class MergeMixin(ViewSetMixin): # TODO update Units to use merge API
|
||||
else:
|
||||
# a new scenario exists and needs to be handled
|
||||
raise NotImplementedError
|
||||
children = source.get_children().exclude(id=target.id)
|
||||
for c in children:
|
||||
c.move(target, 'sorted-child')
|
||||
if isTree:
|
||||
children = source.get_children().exclude(id=target.id)
|
||||
for c in children:
|
||||
c.move(target, 'sorted-child')
|
||||
content = {'msg': _(f'{source.name} was merged successfully with {target.name}')}
|
||||
source.delete()
|
||||
return Response(content, status=status.HTTP_200_OK)
|
||||
@@ -363,14 +368,12 @@ class KeywordViewSet(viewsets.ModelViewSet, TreeMixin):
|
||||
pagination_class = DefaultPagination
|
||||
|
||||
|
||||
class UnitViewSet(viewsets.ModelViewSet, FuzzyFilterMixin):
|
||||
class UnitViewSet(viewsets.ModelViewSet, MergeMixin, FuzzyFilterMixin):
|
||||
queryset = Unit.objects
|
||||
model = Unit
|
||||
serializer_class = UnitSerializer
|
||||
permission_classes = [CustomIsUser]
|
||||
|
||||
def get_queryset(self):
|
||||
self.queryset = self.queryset.filter(space=self.request.space)
|
||||
return super().get_queryset()
|
||||
pagination_class = DefaultPagination
|
||||
|
||||
|
||||
class FoodViewSet(viewsets.ModelViewSet, TreeMixin):
|
||||
|
||||
@@ -133,3 +133,20 @@ def food(request):
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@group_required('user')
|
||||
def unit(request):
|
||||
# recipe-param is the name of the parameters used when filtering recipes by this attribute
|
||||
# model-name is the models.js name of the model, probably ALL-CAPS
|
||||
return render(
|
||||
request,
|
||||
'generic/model_template.html',
|
||||
{
|
||||
"title": _("Units"),
|
||||
"config": {
|
||||
'model': "UNIT", # *REQUIRED* name of the model in models.js
|
||||
# 'recipe_param': 'units' # *OPTIONAL* name of the listRecipes parameter if filtering on this attribute
|
||||
}
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user