How to link with intersphinx to django-specific constructs (like settings)? - django

With a proper intersphinx setup, you can link to Django classes from your own documentation like this:
:class:`django:django.db.models.Model`
But how do you link to a setting? Django uses its own :setting: construct for that instead of something build-in like :class:. How do I link to a setting with intersphinx?
I've tried various incantations, but none work (and some are probably plain wrong):
:ref:`django:ROOT_URLCONF`
:ref:`django:root_urlconf`
:setting:`django:ROOT_URLCONF`
:ref:`django:setting:ROOT_URLCONF`
:django:setting:`ROOT_URLCONF`
Errors like undefined label: django:root_urlconf and Unknown interpreted text role "setting" greet me.

The problem: my local sphinx did not know about Django's custom sphinx roles, like setting. So a perfectly fine intersphinx reference like this:
:django:setting:`ROOT_URLCONF`
does not work until you've told Sphinx about the intersphinx target's custom role.
In the end got it working by copying a small snippet from Django's sphinx extension as _ext/djangodocs.py next to my documentation:
def setup(app):
app.add_crossref_type(
directivename = "setting",
rolename = "setting",
indextemplate = "pair: %s; setting",
)
And I added the following to my Sphinx' conf.py:
import os
import sys
...
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "_ext")))
# ^^^ I'll do that neater later on.
extensions = ['djangodocs',
# ^^^ I added that one.
'sphinx.ext.autodoc',
...
]
...
So: intersphinx works, but if you point at a custom role, you need to have that custom role defined locally.

You need to look at the objects.inv for django to figure out what the correct cross reference should be.
It appears that:
:std:setting:`ROOT_URLCONF <django:ROOT_URLCONF>`
should work.
Somehow I have the objects.inv for django but can't find the URL I retrieved it from, in theory it should be https://docs.djangoproject.com/en/1.4/objects.inv but that redirects several times eventually resulting in a file-not-found error.

Related

drf-yasg Customize SwaggerUIRenderer

I'd like to customize the style (font, colors, logo, etc) of drf_yasg generated docs.
I see that I can extend drf_yasg/swagger-ui.html with the blocks extra_head, extra_styles, extra_body, extra_scripts, and can even overwrite the other blocks if I need to.
What I am not clear on is how I point to my template that extends swagger-ui.html.
I started with
class MyCustomSwaggerUIRenderer(SwaggerUIRenderer):
template = 'api/custom-swagger-ui.html'
I want to replace SwaggerUIRenderer with MyCustomSwaggerUIRenderer in get_schema_view but do not understand how/where to do it without explicitly trying to enumerate all the other Renderers required too in some subclass of rest_framework.views.APIView and that seems convoluted.
Pointers to docs or examples are appreciated. I've already read https://drf-yasg.readthedocs.io/ without success.
You don't have to create a custom class for this. You just need to create a directory with the name drf-yasg under new or existing app and then place a file with the name swagger-ui.html underneath it with your custom template. For example, if you already have an app with the name api, you can just put it under api/templates/drf-yasg/swagger-ui.html. Make sure the app api is specified before drf-yasg in INSTALLED_APPS.
Reference: https://github.com/axnsan12/drf-yasg/issues/294#issuecomment-464461773

Cancel permissions at some part of function (Django)

Is there any possibility to cancel permissions for some part of function, wich specified to function with #is_stuff decorator in Django (1.4.11) with python 2.7?
I mean the following:
#is_stuff(required_perms='<permissions>')
def my_function(request):
if request.POST['key']:
# do something as admin (1)
else:
# there I want to cancel permissions limitation (2)
I know that this is unusual using of decorators. And that decorators called before performing of function. But I am still interested is it possible? Maybe I can make something like this - dynamically change user.is_staff to True as in second link?
Related links where I didn't find answers:
Official docs, official docs (2nd link)

Django - ContentType Does Not Exist for own app

I'm trying to learn how to use the ContentTypes framework, I can't seem to get it to find my own apps.
The docs have clear instructions for importing a model from django.contrib.sites, which works for me. However, when I try to substitute my own app and model, I am unsuccessful.
I have a model at MyApp.Events.models.Event. I try to call:
i = ContentType.objects.get(app_label="Events", model="Event")
in response, console prints:
django.contrib.contenttypes.models.DoesNotExist: ContentType matching
query does not exist.
I tried this as well which also failed:
i = ContentType.objects.get(app_label="events", model="event")
I have 'django.contrib.contenttypes' as well as this app listed under installed apps. Is there another setting I am missing to enable this functionality?
Since no one else posted it, here is the solution.
i = ContentType.objects.get(app_label="Events", model="event")
Even if your model is capitalized in your models.py, it gets saved in all lowercase. I don't know if this is Django's idea of funny or PostgreSQL's, so your mileage may vary.

Django testing named URLs with additional GET parameters

I am trying to write some tests for a Django application I'm working on but I haven't yet decided on the exact urls I want to use for each view. Therefore, I'm using named urls in the tests.
For example, I have a url named dashboard:
c = Client()
resp = c.get(reverse('dashboard'))
This view should only be available to logged in users. If the current user is anonymous, it should redirect them to the login page, which is also a named url. However, when it does this, it uses an additional GET parameter to keep track of the url it just came from, which results in the following:
/login?next=dashboard
When I then try to test this redirect, it fails because of these additional parameters:
# It's expecting '/login' but gets '/login?next=dashboard'
self.assertRedirects(resp, reverse('login'))
Obviously, it works if I hard code them into the test:
self.assertRedirects(resp, '/login?next=dashboard')
But then, if I ever decide to change the URL for my dashboard view, I'd have to update every test that uses it.
Is there something I can do to make it easier to handle these extra parameters?
Any advice appreciated.
Thanks.
As you can see, reverse(...) returns a string. You can use it as:
self.assertRedirects(resp, '%s?next=dashboard' % reverse('login'))

django internalization in urls? how to make urls like this: "en/articles" and "pt/artigos"...?

Hy!
I would need to make urls based on language.
Example:
If I had the language english and subcategory named "articles" then the url might be like this:
/en/articles/...
If the language were portuguese and subcategory already translated is "artigos" then the url will be:
/pt/artigos/...
How can I do it?
I must use the urls.py or some monkeypatches?
thanks
This features is already existing in the yet-to-be released version 1.4. You can read about it in the release note.
If your really need this feature, and no existing app feets your needs, you can still try to apply the corresponding patch yourself.
Django LocaleURL is a piece of middleware that does exactly this. The documentation can be found in the source, or online.
Edit: I read over the fact that you want to translate the url itself... I'm not aware of any piece of code that provides this. Perhaps you could extend the LocalURL middleware to take care of the translations for you. Say you have a regex match like (?P<articles>\w+), you could in the middleware determine which view you want to use. Something like the following mapping perhaps?
if article_slug in ['articles', 'artigos', 'article']:
article(request) # Call the article view
I've been using transurlvania with great success, it does exactly what you need and more, however i see that in the next Django release django-i18nurls will be included in django core so perhaps it would be better to learn that