add api endpoints and genereic views
This commit is contained in:
@ -724,6 +724,33 @@ class StepRecipeSerializer(WritableNestedModelSerializer):
|
||||
)
|
||||
|
||||
|
||||
class UnitConversionSerializer(WritableNestedModelSerializer):
|
||||
base_unit = UnitSerializer()
|
||||
converted_unit = UnitSerializer()
|
||||
food = FoodSerializer(allow_null=True)
|
||||
base_amount = CustomDecimalField()
|
||||
converted_amount = CustomDecimalField()
|
||||
|
||||
def create(self, validated_data):
|
||||
validated_data['space'] = self.context['request'].space
|
||||
validated_data['created_by'] = self.context['request'].user
|
||||
return super().create(validated_data)
|
||||
|
||||
class Meta:
|
||||
model = UnitConversion
|
||||
fields = ('id', 'base_amount', 'base_unit', 'converted_amount', 'converted_unit', 'food')
|
||||
|
||||
|
||||
class NutritionTypeSerializer(serializers.ModelSerializer):
|
||||
def create(self, validated_data):
|
||||
validated_data['space'] = self.context['request'].space
|
||||
return super().create(validated_data)
|
||||
|
||||
class Meta:
|
||||
model = NutritionType
|
||||
fields = ('id', 'name', 'icon', 'unit', 'description')
|
||||
|
||||
|
||||
class NutritionInformationSerializer(serializers.ModelSerializer):
|
||||
carbohydrates = CustomDecimalField()
|
||||
fats = CustomDecimalField()
|
||||
|
@ -12,7 +12,7 @@ from recipes.version import VERSION_NUMBER
|
||||
from .models import (Automation, Comment, CustomFilter, Food, InviteLink, Keyword, MealPlan, Recipe,
|
||||
RecipeBook, RecipeBookEntry, RecipeImport, ShoppingList, Step, Storage,
|
||||
Supermarket, SupermarketCategory, Sync, SyncLog, Unit, UserFile,
|
||||
get_model_name, UserSpace, Space)
|
||||
get_model_name, UserSpace, Space, NutritionType, UnitConversion)
|
||||
from .views import api, data, delete, edit, import_export, lists, new, telegram, views
|
||||
from .views.api import CustomAuthToken
|
||||
|
||||
@ -34,6 +34,8 @@ router.register(r'meal-type', api.MealTypeViewSet)
|
||||
router.register(r'recipe', api.RecipeViewSet)
|
||||
router.register(r'recipe-book', api.RecipeBookViewSet)
|
||||
router.register(r'recipe-book-entry', api.RecipeBookEntryViewSet)
|
||||
router.register(r'unit-conversion', api.UnitConversionViewSet)
|
||||
router.register(r'nutrition-type', api.NutritionTypeViewSet)
|
||||
router.register(r'shopping-list', api.ShoppingListViewSet)
|
||||
router.register(r'shopping-list-entry', api.ShoppingListEntryViewSet)
|
||||
router.register(r'shopping-list-recipe', api.ShoppingListRecipeViewSet)
|
||||
@ -189,7 +191,7 @@ for m in generic_models:
|
||||
)
|
||||
)
|
||||
|
||||
vue_models = [Food, Keyword, Unit, Supermarket, SupermarketCategory, Automation, UserFile, Step, CustomFilter]
|
||||
vue_models = [Food, Keyword, Unit, Supermarket, SupermarketCategory, Automation, UserFile, Step, CustomFilter, UnitConversion, NutritionType]
|
||||
for m in vue_models:
|
||||
py_name = get_model_name(m)
|
||||
url_name = py_name.replace('_', '-')
|
||||
|
@ -65,7 +65,7 @@ from cookbook.models import (Automation, BookmarkletImport, CookLog, CustomFilte
|
||||
MealType, Recipe, RecipeBook, RecipeBookEntry, ShareLink, ShoppingList,
|
||||
ShoppingListEntry, ShoppingListRecipe, Space, Step, Storage,
|
||||
Supermarket, SupermarketCategory, SupermarketCategoryRelation, Sync,
|
||||
SyncLog, Unit, UserFile, UserPreference, UserSpace, ViewLog)
|
||||
SyncLog, Unit, UserFile, UserPreference, UserSpace, ViewLog, UnitConversion, NutritionType)
|
||||
from cookbook.provider.dropbox import Dropbox
|
||||
from cookbook.provider.local import Local
|
||||
from cookbook.provider.nextcloud import Nextcloud
|
||||
@ -88,7 +88,7 @@ from cookbook.serializer import (AutomationSerializer, BookmarkletImportListSeri
|
||||
SupermarketCategorySerializer, SupermarketSerializer,
|
||||
SyncLogSerializer, SyncSerializer, UnitSerializer,
|
||||
UserFileSerializer, UserSerializer, UserPreferenceSerializer,
|
||||
UserSpaceSerializer, ViewLogSerializer, AccessTokenSerializer, FoodSimpleSerializer, RecipeExportSerializer)
|
||||
UserSpaceSerializer, ViewLogSerializer, AccessTokenSerializer, FoodSimpleSerializer, RecipeExportSerializer, UnitConversionSerializer, NutritionTypeSerializer)
|
||||
from cookbook.views.import_export import get_integration
|
||||
from recipes import settings
|
||||
|
||||
@ -921,6 +921,24 @@ class RecipeViewSet(viewsets.ModelViewSet):
|
||||
return Response(self.serializer_class(qs, many=True).data)
|
||||
|
||||
|
||||
class UnitConversionViewSet(viewsets.ModelViewSet):
|
||||
queryset = UnitConversion.objects
|
||||
serializer_class = UnitConversionSerializer
|
||||
permission_classes = [CustomIsUser & CustomTokenHasReadWriteScope]
|
||||
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(space=self.request.space)
|
||||
|
||||
|
||||
class NutritionTypeViewSet(viewsets.ModelViewSet):
|
||||
queryset = NutritionType.objects
|
||||
serializer_class = NutritionTypeSerializer
|
||||
permission_classes = [CustomIsUser & CustomTokenHasReadWriteScope]
|
||||
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(space=self.request.space)
|
||||
|
||||
|
||||
class ShoppingListRecipeViewSet(viewsets.ModelViewSet):
|
||||
queryset = ShoppingListRecipe.objects
|
||||
serializer_class = ShoppingListRecipeSerializer
|
||||
|
@ -228,3 +228,33 @@ def step(request):
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@group_required('user')
|
||||
def unit_conversion(request):
|
||||
# model-name is the models.js name of the model, probably ALL-CAPS
|
||||
return render(
|
||||
request,
|
||||
'generic/model_template.html',
|
||||
{
|
||||
"title": _("Unit Conversions"),
|
||||
"config": {
|
||||
'model': "UNIT_CONVERSION", # *REQUIRED* name of the model in models.js
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@group_required('user')
|
||||
def nutrition_type(request):
|
||||
# model-name is the models.js name of the model, probably ALL-CAPS
|
||||
return render(
|
||||
request,
|
||||
'generic/model_template.html',
|
||||
{
|
||||
"title": _("Unit Conversions"),
|
||||
"config": {
|
||||
'model': "NUTRITION_TYPE", # *REQUIRED* name of the model in models.js
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@ -562,6 +562,112 @@ export class Models {
|
||||
},
|
||||
}
|
||||
|
||||
static UNIT_CONVERSION = {
|
||||
name: "Unit Conversion",
|
||||
apiName: "UnitConversion",
|
||||
paginated: false,
|
||||
list: {
|
||||
header_component: {
|
||||
name: "BetaWarning",
|
||||
},
|
||||
},
|
||||
create: {
|
||||
params: [['base_amount', 'base_unit', 'converted_amount', 'converted_unit', 'food']],
|
||||
form: {
|
||||
// TODO add proper help texts for everything
|
||||
base_amount: {
|
||||
form_field: true,
|
||||
type: "text",
|
||||
field: "base_amount",
|
||||
label: "base_amount",
|
||||
placeholder: "",
|
||||
},
|
||||
base_unit: {
|
||||
form_field: true,
|
||||
type: "lookup",
|
||||
field: "base_unit",
|
||||
list: "UNIT",
|
||||
list_label: "name",
|
||||
label: "Base Unit",
|
||||
multiple: false,
|
||||
},
|
||||
converted_amount: {
|
||||
form_field: true,
|
||||
type: "text",
|
||||
field: "converted_amount",
|
||||
label: "base_amount",
|
||||
placeholder: "",
|
||||
},
|
||||
converted_unit: {
|
||||
form_field: true,
|
||||
type: "lookup",
|
||||
field: "converted_unit",
|
||||
list: "UNIT",
|
||||
list_label: "name",
|
||||
label: "Converted Unit",
|
||||
multiple: false,
|
||||
},
|
||||
food: {
|
||||
form_field: true,
|
||||
type: "lookup",
|
||||
field: "food",
|
||||
list: "FOOD",
|
||||
list_label: "Food",
|
||||
label: "Food",
|
||||
multiple: false,
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
static NUTRITION_TYPE = {
|
||||
name: "Nutrition Type",
|
||||
apiName: "NutritionType",
|
||||
paginated: false,
|
||||
list: {
|
||||
header_component: {
|
||||
name: "BetaWarning",
|
||||
},
|
||||
},
|
||||
create: {
|
||||
params: [['name', 'icon', 'unit', 'description']],
|
||||
form: {
|
||||
|
||||
name: {
|
||||
form_field: true,
|
||||
type: "text",
|
||||
field: "name",
|
||||
label: "Name",
|
||||
placeholder: "",
|
||||
},
|
||||
icon: {
|
||||
form_field: true,
|
||||
type: "emoji",
|
||||
field: "icon",
|
||||
label: "Icon",
|
||||
placeholder: "",
|
||||
},
|
||||
unit: {
|
||||
form_field: true,
|
||||
type: "text",
|
||||
field: "unit",
|
||||
label: "Unit",
|
||||
placeholder: "",
|
||||
},
|
||||
description: {
|
||||
form_field: true,
|
||||
type: "text",
|
||||
field: "description",
|
||||
label: "Description",
|
||||
placeholder: "",
|
||||
},
|
||||
},
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
static RECIPE = {
|
||||
name: "Recipe",
|
||||
apiName: "Recipe",
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user