Custom views parameter depending url


#1

Hello,
Is there a way to add conditional parameter in views depending the url :?

I have a view which perform exactly the same task for 10 different things. But each should have his own schema.
Because we need to expose the schema in views parameters, I have to make 10 different views with for only difference the schema-name, I think it’s a bit to verbose.

shorter :
Whit url : Route(’/{thing_type}/{customer_id}/’, ‘POST’, my_view ),

def my_view(thing_type: str, customer_id: int, obj: ObjSchema):
pass

I need ObjSchema changes with “thing_type”

I hope it’s clear enough

Thanks for answer

Jimmy


#2

I answer to myself. I found a way to do it whith class BasedViews(:wink:) and closures :

First a dict :
my_schema = {modelname1:schema, modelname2:schema}

next a class instanciated with the model :
ViewsModel1 = MyViews(model1)
ViewsModel2 = MyViews(model2)

The class :

class MyViews:
   def __init__(self, model):
         self.model = model
   def create(self):
      def create_views(id:int, schema: my_shema[self.model.name], auth:Auth,  blabla: SomeType) --> myschema[self.model.name]:
           # this is just a simple apistar view
           return myschema[self.model.name](some_stuff)
   #same thing for ceate/update/delete

   def urls(self):
          return [
              Route('/' + self.name + '/', 'POST', self.create()),
              Route(some other routes)
          ]

Then I juste include in urls :

myapp_urls : [
     *ViewsModel1.urls(),
      *ViewsModel2.urls(),
]

thats all, thanks for your opinion