The responseMessages is not displaying in Django-Swagger integration - python

I am using DRF YASG to document my API.
I want to remove calls for PUT and PATCH from here. For the views here I have used viewsets.ModelViewSet.
Another app uses APIview for its views.py. How can I exclude methods for a particular url.
My view contains two methods POST and DELETE. But for implementing the methods I have seperate URLs.
The problem is that both the URLs show both methods and the list just gets too big.
The question might be similar but I have a constraint of not making changes in the previous code-base.

Related

Django setup url parameter for certain requests only

So I'm trying to setup my Django view such that when they POST to an endpoint they don't need an url parameters, but when they call GET they require it
path('posts/', views.PostView.as_view(), name="post"),
path('posts/<int:count>/', views.PostView.as_view(), name="post"),
The issue with this is that it makes swagger messy and swagger with assume that url/posts/ you can POST and GET from, and url/posts// you can as well, but I only want to be able to POST to the first and GET from the 2nd. Is there a way to do this without creating separate view classes for them? Or is it better practice in this case to create separate view classes?

Other methods on class based views in Django

I currently have a class based view with get(), post() and delete() methods for the following URL:
/property/<str:property_uuid>
This works fine and is great.
However, I now want URLs such as /property/<str:property_uuid>/publish and /property/<str:property_uuid>/publish to make my API easier to use. These will be GET methods.
I was hoping to have corresponding publish() and unpublish() functions and put these within my class. However, I'm not sure how to do this or if this is even posible with class based views? Would I have to go back to function based views with all my functions defined in a "property.py" file for ease of reference and then include this in my urls.py file?

wagtail main search add modelAdmin models

Quick question on wagtail's main search at the top of the left sidebar beneath the logo.
By default that search box searches pages, images, documents, users.
Two questions:
Is there a way to modify that search scope so it also includes modelAdmin models?
Is there a way to remove pages from the search query list so it only searches images, documents, users?
I can't seem to find anything in the docs about it. I know you can search modelAdmin models once on the model admin list view, I have that working. I was just looking for a way to extend that search to be included on the main sidebar search as well.
Any direction you can provide would be much appreciated.
The admin search area does show multiple items, such as pages, images, documents etc.
However, this page only searches page models, when you click the other models (e.g. images), it will take you to the images search page, it will also append the query param q based on any existing search.
So just to clarify, this page only searches pages and to search for other items, it takes you elsewhere in the Wagtail admin.
Hopefully the below answers your specific scenario questions.
1. Modify the admin/pages search scope or results
This could possibly be done by making your own view, but it is not simple
You can see the search view function here https://github.com/wagtail/wagtail/blob/master/wagtail/admin/views/pages.py#L958
You can redirect any URL by modifying your urls.py to direct the admin/pages/search/ page to your custom view.
However, you would likely need to re-write (copy/paste) most of the view as it is a function, not class view
2. Adding custom search areas
This can be done by the register_admin_search_area hook, it will add (in a custom order) an item next to the 'other searches' text
See documentation here https://docs.wagtail.io/en/latest/reference/hooks.html#register-admin-search-area
This lets you add what is essentially a link to another search area elsewhere in the Wagtail admin (code example below)
3. Removing a search area
The simplest way to hide one of the items in the 'other searches' list would be with a CSS change
The next best way would be to customise the template wagtail/admin/templates/wagtailadmin/shared/search_other.html with an override and then filter the results
I am not aware of a way to remove hooks or registered hooks without some monkey patching, but the hooks get collected and stored as admin_search_areas you can see the code here - https://github.com/wagtail/wagtail/blob/master/wagtail/admin/search.py#L102
Example Code for using the register_admin_search_area hook
This basically uses the documentation example but gives you a rough idea of how to go to a specific ModelAdmin index view using this feature. Note: this does not search ALL ModelAdmin models, only one specific one.
You can always make a custom search page for searching all models at once though.
from wagtail.core import hooks
from wagtail.admin.search import SearchArea
# ...
#hooks.register('register_admin_search_area')
def register_model_admin_search_area():
# PeopleModelAdmin is a ModelAdmin also in the same hooks file
index_url = PeopleModelAdmin().url_helper.index_url
return SearchArea(
'People ModelAdmin',
index_url,
classnames='icon icon-user',
order=10000
)
#hooks.register('register_admin_search_area')
def register_snippets_search_area():
url = reverse('wagtailsnippets:list', args=('base', 'people'))
return SearchArea(
'People Snippets',
url,
classnames='icon icon-user',
order=10000
)

What are the differences between Generics, Views, Viewsets and Mixins in Django?

I am new to Django and Django-Rest. I am confused about when I should use these? what are their advantages and disadvantages? I have only seen this- http://www.cdrf.co
The only thing I know is there are a lot of ways to do 1 thing. But this is totally unclear to me.
In Django, these four terms we use frequently for different purposes in the projects. I have tried to collect and share the actual meaning with the links to details description of each term. Please check if you find these helpful.
Generic views:
“Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.”
— Django Documentation
Read more details
Views:
A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.
Read more details
Viewsets:
Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet. In other frameworks, you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'.
A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as .get() or .post(), and instead provides actions such as .list() and .create().
The method handlers for a ViewSet are only bound to the corresponding actions at the point of finalizing the view, using the .as_view() method.
Read more details
Mixins:
The mixin classes provide the actions that are used to provide the basic view behavior. Note that the mixin classes provide action methods rather than defining the handler methods, such as .get() and .post(), directly. This allows for more flexible composition of behavior.
The mixin classes can be imported from rest_framework.mixins.
Read more details

Is it possible to call different function of class based views through different actions in template

I am new to django and i ahve gone through all the docs of django. right now if we give some link in template and defined that link in urls.py i.e which view is going to handle that link. like this url(r'^dashboard/gift/$', login_required(CouponPageView.as_view())),
But i have this little doubt can i call different function of a view on clicking different links present in template.
The idea behind a class-based view is not to serve multiple resources (the targets of the links in your template). The idea is that the class-based view implements methods for the various HTTP methods (i.e. get, post, put, delete, head).
So you can server an HTTP GET of a certain URI using the SomeView.get() method, or you can handle a POST to the same resource from the post() method in the same SomeView class. This is helpful to support object oriented code, as the different methods on the object will typically share some resources.
If you want to handle different URL's, write different View classes. If their functionality is similar, use inheritance to prevent code duplication. If their functionality is almost identical, use parameters in the urlpattern.
I think you need to study the URL dispatcher a little more: https://docs.djangoproject.com/en/dev/topics/http/urls/

Categories