Let's say that I have a view:
my_view(request: Request, uuid: UUID):
pass
I'd like to automatically translate all uuid objects to base64-based strings, so that the framework user doesn't need to manually call slug_to_uuid() and uuid_to_slug. This would apply to all views and is based on Python 3 argument signature type hinting (if it hints it's UUID object then you want to translate it to a string and back).
route_url('viewname', uuid=my_uuid) would encode UUID arguments as base64 string
Routing machinery would read Python 3 signature of a view function and translate string back to UUID object before calling the view
What hooks and approaches I can take this in Pyramid?
Hooks to route_url
Hooks in router to translate incoming view arguments using custom predicates, tweens, etc.
You're asking about 2 workflows. 1) translate incoming data. 2) translate outgoing data in urls.
In Pyramid the translation of incoming data should be done by decorators, view mappers or possibly predicates. You'll have to decide which fits your use-case best. Tweens don't really make sense because they happen prior to creation of the matchdict.
As far as url generation, the only hook available for route_url is a pregenerator on the route which can take the kwargs and translate them.
Use functools.singledispatch? https://docs.python.org/3/library/functools.html#functools.singledispatch
Related
I want to create a Student Register and Login Api without using serializer in django Rest Framework.
So I want to know how I make CRUD operation for those api using ApiView
Any one please solve this
I don't think you can, or even should but first, you need to understand what a serializer actually does:
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML, or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data
So don't be scared by serializers. Just take your time and learn how to use them using the many tutorials available online.
If you are using DRF then you must need to create the serializers. But you can create the API without using DRF and serialzers.
Serializer is not just for converting json data ,it also used to validate payload. You can use request.data.get("field_name_in_json_object") and fill the fields one by one.and for return you can use .values("field_name") in
querysets.
Django values_list vs values
I need some help...
Value objects, by definition, are immutable data classes that only use other value objects or primitives. And an value object only should be created with a valid value.
But... what happens when to construct a valid value object we need to validate the data using a third party library?
For example a Phone and a Mobile value objects. Build a valid phone depends on a country and the validation rules could constantly change and be complex. We don't want to spend time changing phone validation rules because this is not the core of the our business... so we want to validate the data using Google libphone library.
Possible solutions:
Inject the third party library by construct: new Phone($phone, $validator)
Inject the third party library by factory method: Phone::fromString($phone, $validator)
Build the object from a class factory.
First and second options I guess is not the correct way, Value objects don't necessarily need collaborators...
Third option allows to create invalid phones if you instantiate the phone outside the factory.
Any idea?
Thanks.
The solution for this is to have 2 types UnvalidatedPhone and ValidatedPhone (both part of the domain), together with your 3rd option.
The incoming command will have an UnvalidatedPhone and as you process the command, at some point you'll validate it using the builder/factory and get a ValidatedPhone. This is even better if you can hide the constructor of ValidatedPhone, so the validator is the only thing that can create a ValidatedPhone.
Mini warning: If you are writing this in an OO language (I guess you do), the two phone classes don't need to be part of a hierarchy. Try to keep them separate.
I have a composite entity working fine. But when I try to change its value by another composition in fulfillment, it's been interpreted as a string value:
const entity = {
"name":"projects/myproject/agent/sessions/" + sessionID +
"/entityTypes/lia_parametro1",
"entities":[{
"value":"#sys.email:email",
"synonyms":[
"#sys.email:email"
]
},
When I put this value directly in Dialogflow Console(#sys.email:email) it works fine, but when I try to do this dynamically as above, it understands "#sys.email:email" as a value, instead of a System Entity.
Is there a special way to declare System Entities in Json format?
Many thanks for any tip!
Diego Mesquita
I think you're getting confused about what the purpose of entities are.
Entities are used when extracting parameters - they're hints to Dialogflow to say "I'm trying to grab a piece of data from whatever the user has said, by the way the piece of data I want you to extract looks like this -> [the values of #sys.email]".
When something reaches your fulfillment code, that whole data extraction process has been done, and therefore entities become irrelevant. You can run whatever code you wish to extract data (for example a regex), then assign that as a parameter value to some output context.
To quote the documentation:
Each intent parameter has a type, called the entity type, which dictates exactly how data from an end-user expression is extracted.
I hope this helps - if not, can you give a little more info about your use-case?
What's the difference between using : and ? in a URL? For example /products/:id and /products?id=1? I am trying to get the values from the URL like this Product.findById (req.params.id) but I was wondering which one is most suitable. I know using : do I have to use req.params and ? req.query but I don't understand the difference between them, are they the same?
in my point of view, it is totally different if you are using RESTFUL API pattern
/products/:id called path parameters
The path parameters determine the resource you’re requesting for. Think of it like an automatic answering machine that asks you to press 1 for service, press 2 for another service, 3 for yet another service and so on.
Path parameters are part of the endpoint itself and are not optional
but query parameters
Technically, query parameters are not part of the REST architecture, and they used to help you completely understand how to read and use API’s Query parameters give you the option to modify your request with key-value pairs.
Having your parameters in the query is conceptually optional to the router, query parameters are more of properties and descriptions of the request itself, like when saying GET /users?sort=asc, in this case, the sort attribute was more of a description to the request and the request could complete the fetch without it, that might not always be the case, but a query parameter still describes its request even if it was mandatory.
On the other hand, URL parameters are part of the request itself, the URL without the parameter doesn't make sense, like GET /users/:userID, in this case, not supplying userID will supply unexpected data (A list of users for example) if it didn't break the router completely. URL parameters play part in defining the request rather than just describing it, and they can't be optional.
I've found filter_var to be extremely useful in validating and sanitizing user input with PHP, but I've yet to find anything even remotely as convenient in ColdFusion (more specifically, CF8).
Obviously I can hack together something using REReplace, but that would take significantly more time to code up and would be much uglier than using the pre-defined filters available in PHP. Is there a more efficient way or do I just need to bite the bullet?
There are three different options available to you. Since you're attempting to manage user input, I assume you're using forms. isValid most closely mimics your functionality, allowing you to check if a value specified matches either a data type or a regular expression and returns true or false, and includes attributes by default to define a range. It does not support the ability to create a custom 'filter' beyond defining a regular expression however.
The second option would be using cfparam tags on your POST processing page, which allows you to specify the existance of a variable, test against a data type or define a regular expression, and optionally assign a default value if the variable doesn't exist. If you attempt to process a page where the field is not defined and no default value is assigned however, ColdFusion throws an error.
Finally, you can do validation by using cfform and cfinput fields on your form itself; which allows for client-side data validation for existence and types (it also supports server-side validation but it's implementation is sloppy), regular expressions, and input masking: taking user-inputted data and conforming it to a specific format (like adding dashes to phone numbers and zip codes).