django-rest-framework - autogenerate form in browsable API? - forms

Not sure if i'm using the right vocabulary. In the browsable api that comes for free with django-rest-framework, I was wondering if there was a way to autogenerate a form similar to how we define ModelForms. This would allow us to more easily test input to the API in some cases.
I'm currently using ModelSerializers and the generic view APIView in case that makes a difference.
I have read the documentation (several times at this point) but didn't see it mentioned anywhere.

If you're using the generic class-based-views you'll get that for free. Try the live tutorial at http://restframework.herokuapp.com logging in as one of the users, so that you can create some snippets. eg user: 'max', password: 'max'.
Any views subclassing GenericAPIView and setting a serializer_class will get that behavior, as REST framework can determine what the form should look like.
For example:
(Note the form input at the bottom of the screen shot)
If you're just working from APIView you'll get the generic content input (such as json), like the once you've included a screenshot of, which is also useful, but not quite as convenient as the forms.

Create a serialiser class that fits the form input fields you want and set it on your APIView like so;
class MyView(APIView):
serializer_class = MySerializer # Used for the form in the browsable api
That works just perfectly.
Example of a serializer class based on a model:
from rest_framework import serializers
class MySerializer(serializers.ModelSerializer):
class Meta:
model = MyModel

class MyApiView(APIView):
"""My Demo API View"""
serializer_class = serializers.MySerializers
Make sure you're using the name "serializer_class" and not any other name like serializers_class.
using the exact "serializer_class" will autogenerate form in the browseable API

Related

Custom forms in Drupal 8

I am struggling with some Drupal 8 form/entity type coding.
Basically here is a short summary of the task:
I have entity types which are from the eck module, so say three fields:
Name:
Email:
Phone:
So I have created a custom form using the FormBase class for users to fill in and that's fine but, I don't know how to save the post data to the entity.
If it was a node I would use the Node::create function but this is an entity so I need another way.
So I guess my task is:
1. Get the ID of the entity I want to save too
2. Find a way to save the form data to the entity
I am very new to Drupal 8 (just like most people I guess), I have tried Googling for info but I can't find anything that I understand, any help with this would be amazing, it should be a simple task I would have thought?
I personally don't use the ECK module, so I don't know how it works exactly, I can only imagine. I build my entities by myself.
But what I do know is that the FormBase class is the wrong (not wrong per se, but there's another class which handles exactly what you want) one to use to save entities. You're supposed to use the ContentEntityForm class for that.
Docs: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEntityForm.php/class/ContentEntityForm/8.2.x
The FormBase class is the generic form class, you use it basically for custom and simple stuff, where you only need the buildForm, validateForm and submitForm implementations (more or less).

itextpdf : MarkedObject use

What is the interest of using MarkedObject in itext pdf ?
Show a main use example please. There is only javadoc on the net related to this object.
The MarkedObject class was introduced to allow developers to add attributes to an object when creating XML or HTML. For instance: when you created HTML, you could add an id or class attribute. This use case has disappeared in 2009 when we removed XML and HTML generation from iText.
We thought it could also be used in the context of PDF, more specifically in the context of generating PDF/A. However, we decided to create PDF/A in a difference way, using the IAccessibleElement interface. If you check the API docs, you see that this interface also defines methods to set and get attributes.
In short: you can safely ignore the MarkedObject class: it is no longer used. Writing a "main use example" would be a waste of time.

Mixing vocabularies, Google's structured data testing tool and Schema.org extensions

We are using several vocabularies along with schema.org and struggle with the structured data testing tool from Google. Is is even possible to completely pacify it when mixing vocabularies?
Some of the classes and properties we use are specializations of classes and properties of schema.org.
I have read the page about the extension mechanism. It is completely unclear to me what external extensions actually are. It is completely unclear to me if and how it is possible to communicate to Google that a class/property is a specialization of a schema.org class/property (so that Google uses RDFS reasoning to get statements involving the schema.org namespace).
The example I am using is http://www.netestate.de/imgtag_schema_example/lio.html
The RDFa in that page describes the image shown. The <img> tag in the source has a typeof attribute.
If I use typeof="lio:Image", I get 1 error about lio:Image not being known to Google. Makes sense. Validation URL: http&colon;//www.netestate.de/imgtag_schema_example/lio.html
If I use typeof="lio:Image schema:ImageObject", I get exactly the same error. Validation URL: http&colon;//www.netestate.de/imgtag_schema_example/lioschema.html
If I use typeof="schema:ImageObject", I get 19 errors about properties not recognized as compatible with ImageObject. Validation URL: http&colon;//www.netestate.de/imgtag_schema_example/schema.html
If I use typeof="schema:ImageObject lio:Image", I get 1 error about a class that is not known to Google (the class is not named but "ImageObject" is red!). Validation URL: http&colon;//www.netestate.de/imgtag_schema_example/schemalio.html
If I use typeof="lio:Image" and add the statement lio:Image rdfs:subClassOf schema:ImageObject to the RDFa, the validator separates the triples about http&colon;//purl.org/net/lio#Image ("class not defined, no errors") and the image (unknown class #__sid=rd0, 1 error). Validation URL: http&colon;//www.netestate.de/imgtag_schema_example/liosubclass.html
Where does the relative URI #__sid=rd0 come from?
Why is the error about #__sid=rd0 missing in this simpler example?
http://www.netestate.de/imgtag_schema_example/minimal.html
Don't let any Google Structured Data Testing Tool complaints about unknown vocabulary bother you. Its main purpose it to help publishers understand when they are using structures which Google products/features expect and use. Generally it will only understand the schema.org parts (and won't exploit subtypes to other vocabularies). You might find using the additionalType property helps make some errors go away. The __sid=rd0 ID is just a generated URI for what RDF would consider a 'blank node' in the graph.

Mirror formatting spring-data-rest/spring-hateoas in custom controllers

I used the suggested approach in this question to return HATEOAS formatted outputs that match those returned by spring-data-rest. It works good, but is there a way to avoid boiler plate code to create entity resource assemblers like the QuestionResourceAssembler in the referenced question, if I only want to add 'self' links using the id to all entities? Perhaps using ResourceAssemblerSupport?
The easiest way is to simply use the Resource wrapper type:
Resource<Person> personResource = new Resource<>(person);
personResource.addLink(…);
personResource.addLink(…);
Links can be created either by simply instantiating them (i.e. new Link("http://localhost/foo", "relation") or by using the ControllerLinkBuilder which allows you to point to Controller methods for obtain a reverse mapping. See this section of the Readme for details.

using kohana zend lucene search

i have a kohana-based website, and i want to use zend-lucene search.
as i have seen in the documentation here https://github.com/evopix/kohana-search/blob/master/README.markdown but i do not understand: must i re-create the models i already have in order to be able to use it? the model should extend the ORM_Searchable (it is a must)? and where can i find the ORM_Searchable class in kohana?
thanks a lot!
You don't have to recreate the models. You extend from a new class, implement its methods which specify what data you want to be indexed by Zend Lucene. The documentation is really straight forward.
the model should extend the ORM_Searchable (it is a must)? and where can i find the ORM_Searchable class in kohana?
This is in the code repository you linked to. classes/kohana/orm/searchable.php