improved plugin functionality

- added abiulity to extend default api router from plugion
- added dability to pass custom model definition to generic model/api functions
- added ability to pass custom API clients to generic API function
This commit is contained in:
vabene1111
2023-05-20 12:53:14 +02:00
parent ddd2f96b85
commit c777cfe5b9
5 changed files with 23 additions and 5 deletions

View File

@ -6,7 +6,7 @@ from rest_framework import permissions, routers
from rest_framework.schemas import get_schema_view from rest_framework.schemas import get_schema_view
from cookbook.helper import dal from cookbook.helper import dal
from recipes.settings import DEBUG from recipes.settings import DEBUG, PLUGINS
from recipes.version import VERSION_NUMBER from recipes.version import VERSION_NUMBER
from .models import (Automation, Comment, CustomFilter, Food, InviteLink, Keyword, MealPlan, Recipe, from .models import (Automation, Comment, CustomFilter, Food, InviteLink, Keyword, MealPlan, Recipe,
@ -16,7 +16,13 @@ from .models import (Automation, Comment, CustomFilter, Food, InviteLink, Keywor
from .views import api, data, delete, edit, import_export, lists, new, telegram, views from .views import api, data, delete, edit, import_export, lists, new, telegram, views
from .views.api import CustomAuthToken from .views.api import CustomAuthToken
router = routers.DefaultRouter() # extend DRF default router class to allow including additional routers
class DefaultRouter(routers.DefaultRouter):
def extend(self, r):
self.registry.extend(r.registry)
router = DefaultRouter()
router.register(r'automation', api.AutomationViewSet) router.register(r'automation', api.AutomationViewSet)
router.register(r'bookmarklet-import', api.BookmarkletImportViewSet) router.register(r'bookmarklet-import', api.BookmarkletImportViewSet)
router.register(r'cook-log', api.CookLogViewSet) router.register(r'cook-log', api.CookLogViewSet)
@ -53,6 +59,10 @@ router.register(r'user-space', api.UserSpaceViewSet)
router.register(r'view-log', api.ViewLogViewSet) router.register(r'view-log', api.ViewLogViewSet)
router.register(r'access-token', api.AccessTokenViewSet) router.register(r'access-token', api.AccessTokenViewSet)
for p in PLUGINS:
if c := locate(f'{p["module"]}.urls.{p["api_router_name"]}'):
router.extend(c)
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('setup/', views.setup, name='view_setup'), path('setup/', views.setup, name='view_setup'),
@ -119,7 +129,6 @@ urlpatterns = [
path('api/switch-active-space/<int:space_id>/', api.switch_active_space, name='api_switch_active_space'), path('api/switch-active-space/<int:space_id>/', api.switch_active_space, name='api_switch_active_space'),
path('api/download-file/<int:file_id>/', api.download_file, name='api_download_file'), path('api/download-file/<int:file_id>/', api.download_file, name='api_download_file'),
path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'),
# TODO is this deprecated? not yet, some old forms remain, could likely be changed to generic API endpoints # TODO is this deprecated? not yet, some old forms remain, could likely be changed to generic API endpoints
path('dal/food/', dal.IngredientsAutocomplete.as_view(), name='dal_food'), # TODO is this deprecated? path('dal/food/', dal.IngredientsAutocomplete.as_view(), name='dal_food'), # TODO is this deprecated?

View File

@ -144,6 +144,7 @@ try:
'base_path': os.path.join(BASE_DIR, 'recipes', 'plugins', d), 'base_path': os.path.join(BASE_DIR, 'recipes', 'plugins', d),
'base_url': plugin_class.base_url, 'base_url': plugin_class.base_url,
'bundle_name': plugin_class.bundle_name if hasattr(plugin_class, 'bundle_name') else '', 'bundle_name': plugin_class.bundle_name if hasattr(plugin_class, 'bundle_name') else '',
'api_router_name': plugin_class.api_router_name if hasattr(plugin_class, 'api_router_name') else '',
} }
PLUGINS.append(plugin_config) PLUGINS.append(plugin_config)
except Exception: except Exception:

View File

@ -76,7 +76,8 @@ export default {
return {} return {}
}, },
}, },
show: { required: true, type: Boolean, default: false }, show: {required: true, type: Boolean, default: false},
models: {required: false, type: Function, default: null}
}, },
data() { data() {
return { return {
@ -92,6 +93,10 @@ export default {
mounted() { mounted() {
this.id = Math.random() this.id = Math.random()
this.$root.$on("change", this.storeValue) // bootstrap modal placed at document so have to listen at root of component this.$root.$on("change", this.storeValue) // bootstrap modal placed at document so have to listen at root of component
if (this.models !== null){
this.Models = this.models // override models definition file with prop
}
}, },
computed: { computed: {
advancedForm() { advancedForm() {

View File

View File

@ -311,7 +311,7 @@ export function calculateHourMinuteSplit(amount) {
let minutes = amount - hours * 60 let minutes = amount - hours * 60
let output_text = hours + " h" let output_text = hours + " h"
if (minutes > 0){ if (minutes > 0) {
output_text += " " + minutes + " min" output_text += " " + minutes + " min"
} }
@ -368,6 +368,9 @@ export const ApiMixin = {
let func = setup.function let func = setup.function
let parameters = buildParams(options, setup) let parameters = buildParams(options, setup)
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
if (model.apiClient !== undefined) {
apiClient = model.apiClient
}
return apiClient[func](...parameters) return apiClient[func](...parameters)
}, },
genericGetAPI: function (url, options) { genericGetAPI: function (url, options) {