I am using redoc in django==2.0 for documenting some django API. I noticed that by default redoc will name the endpoints automatically as you can see on the image below on the left side. Most likely I don't want to use the names generated I want customize the names. From someone who has experience with redoc documentation could you advice please?
If you are using drf-yasg, you can use the swagger_auto_schema decorator to configure the operation_id.
from drf_yasg.utils import swagger_auto_schema
from django.utils.decorators import method_decorator
#method_decorator(name='get', decorator=swagger_auto_schema(operation_id='List Widgets', operation_description='List all available widgets'))
class WidgetListView(ListAPIView):
serializer_class = WidgetSerializer
def get_queryset(self):
return Widget.objects.all()
These summaries are actually populated from the input JSON, which can be found at this path in source ["paths"][path][method]["summary"].
You might want to edit these in order to change the summaries.
If you don't want to change the source input a hack around can be to change the text of DOM elements after REDOC load.
Related
I am new to Django and scratching my head with the following question
how do I know which function is run in specific processes?
for example in below view, retrieve function is executed. I flipped the documentation, however, could not find anything named retrieve to overwrite
class ProfileRetrieveAPIView(RetrieveAPIView):
serializer_class = ProfileSerializer
def retrieve(self, request, username, *args, **kwargs):
print("hey yo i am running 01")
try:
profile = Profile.objects.select_related('user').get(
user__username=username
)
except Profile.DoesNotExist:
raise
serializer = self.serializer_class(profile)
return Response(serializer.data)
In general the generic views in Django Rest Framework are made by inheriting from various mixins. It is also documented which mixin a view is inheriting from. For example the documentation for RetrieveAPIView which you use is as follows:
Used for read-only endpoints to represent a single model instance.
Provides a get method handler.
Extends:
GenericAPIView,
RetrieveModelMixin
So you can override get here if needed (it actually calls the retrieve method here). If you further follow the link for RetrieveModelMixin you will see that it's documentation has this line:
Provides a .retrieve(request, *args, **kwargs) method, that implements
returning an existing model instance in a response.
Here we can see it mentions that it provides a retrieve method. So you just need to check the documentation for the super classes for a respective class if you want to know what method is used by it.
Note: If all else fails when searching the documentation consider simply looking at it's the source code on GitHub or in your own IDE.
;)
According to DRF documentation, there is a default mapping the views and the router:
list is for GET on a list of objects, like /users/
retrieve is for GET on a single object, like /users/3/
create is for POST on a non-detailed endpoint, like /users/
update is for PUT on a detailed endpoint, like /users/3/
partial_update is for PATCH on a detailed endpoint, like /users/3/
destroy is for DELETE on a detailed endpoint, like /users/3/
Those are the expected/standard mappings within DRF views and viewsets
You can view some examples here: https://www.django-rest-framework.org/api-guide/generic-views/#mixins
I've developed a complex data analysis model using Python (for the sake of simplicity, let's call it analysis.py) which contains multiple long algorithms to transform a set of raw data and spits out output in the form of dataframe as well.
I've been looking into some tutorials recently on Django framework and have tried building a simple web app that resembles a forum/blog post web app.
With the dataframe output from the analysis.py, I would like to display the data in the form of multiple charts on the web app.
Can anyone point me to a direction to move from here? I've looked for multiple resources online but I think I am unable to find the correct keywords that match with what I actually want to build. I just want a shell code kind of a thing to integrate into my current django backend in order to be able to call the dataframe from the analysis.py
Thank you very much in advance.
It amazes me somehow that after posting a question here, I managed to come up with better keywords in finding the solution that is closely matched to my intended product.
Again, I would like to apologize if my question was too vague or did not contain any script that I've done so far, since I was merely looking for direction to move forward. And fortunately I did after giving it much thought and integrating different solutions from multiple sources on the net.
I'm not sure if my solution is the best solution, but it works for me and if it at least helps other people who were on the same boat as I did before, I'd be glad!
So my solution entails importing a dataframe from the analysis.py and then pass the data that I want as API Endpoints (using Django Rest Framework) in order to be displayed in my dashboard.
The following are some details (with some scripts) of my solution:
views.py
# Importing the external python script (analysis.py).
#This is where the data from csv is imported and is transformed.
#The script is written in a function form and returns the dataframe.
from . import analysis
from .analysis import data_transform
#Django Rest Framework
from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
class ChartData(APIView):
authentication_classes = []
permission_classes =[]
def get(self, request, format=None):
# Assign dataframe output to a new dataframe
df = data_transform()
# Assigning variables from datafram
data1 = df['VOL'].values.tolist()
data2 = df['PRICE'].values.tolist()
data = {
"data1":data1,
"data2":data2,
}
#Return endpoints in Json format
return Response(data)
Analysis.py looks more like this
import pandas as pd
import numpy as np
def data_transform()
#where all the transformations happened
return dataframe
And then I passed the endpoints ('data') to a html in order to visualize them using chart.js. You can find some good resources in the Tube. Look up keywords chart.js and django.
Again, I'm not sure this is the best method. I am open to a better solution, if you guys have any!
We use Python Django.
I want to store user-uploaded files in the following way:
/<INTEGER>/<HASH>/<FILE>
<INTEGER> is a subfolder which is meant to contain max 1000 files.
<HASH> is a secret code individual for each file, to prevent hackers to download a somebody's other file from our HTTPS server.
<FILE> is the filename.
How to write a Django storage implementing this?
First, can I derive my class from django.core.files.storage.FileSystemStorage or I need to derived from django.core.files.storage.Storage directly?
Second, which methods should I override? Should I override save? It seems yes, because it is best to generate the hash only when saving, but I am unsure I can do in save what I need. What about overriding _save? Also: what's about overriding get_available_name and/or generate_filename? They seem not be quite tailored to my needs, because it would be better to generate the hash only when actually saving the file, not just when considering its name.
So, what to do?
I would go for generate_filename in django.core.files.storage.FileSystemStorage. I find it more appropriate since than I still have the nice safety cushion in get_available_name.
I also wouldn't worry about getting filename in cases when the file is not being saved, just need to make sure that the hash generating is cheap enough.
# settings.py
DEFAULT_FILE_STORAGE = 'app.storage.CustomStorage'
# app/storage.py
from datetime import datetime
import hashlib
import random
from django.core.files.storage import FileSystemStorage
class CustomStorage(FileSystemStorage):
def generate_filename(self, filename):
# implement smarter hash creation method here
some_hash = hashlib.md5('{}-{}'.format('filename', datetime.now().isoformat())).hexdigest()
return '{0}/{1}/{2}'.format(random.randint(0, 1000), some_hash, filename)
i guess you can have your own function to handle the way you want to create folder structure and you can have in models.py as attachment = models.FileField(upload_to=change_file_path, blank=True, null=True)
where change_file_path is your custom function to create the desired folder structure
Using both storage and upload_to arguments for setting the upload directory.More info FileField django docs
Here I am using uuid as <HASH> (you could change using your own hash generator),max_num is <integer> and senfile is <FILE> in Django model
models.py
from django.core.files.storage import FileSystemStorage
import uuid
storeFile = FileSystemStorage(location='SensitiveFiles/')
def content_file_upload(instance, filename):
return '{0}/{1}/{2}'.format(instance.max_num,str(uuid.uuid4()), filename)
class SensitiveFiles(models.Model):
user = models.ForeignKey(User)
senfile = models.FileField(storage=storeFile,upload_to=content_file_upload,validators=[validation_for_max_num_files])
max_num=models.IntegerField(default=1000)
def get_file_url(self):
return self.senfile.url
Create your validators for checking current number of files in uploaded directory.
I'm looking for functionality vaguely like that provided by Semantic MediaWiki. In short, I'd like for a user, in an arbitrary text field, to be able to do things like the following (I'm making up the markup as I go).
*Hi, everyone, don't forget that we have [[::AfricanSwallow.count]] African Swallows in our land.
*Did you know that Harry the European Swallow has carried [[::EuropeanSwallow.get(name="harry").coconuts.count]] coconuts back with him?
In addition to these kinds of features, I'd like to be able to autocomplete inline - perhaps when the user starts typing.
I can do all of these things, but I'm hoping that some or all of them have been done. Any idea if that's the case?
I think something like this is feasible but making it universal (allowing full read-only access to the ORM) would be very difficult to make in a secure way.
Here are some ideas:
Limit the actions to a predefined set of explicitly marked methods on a custom manager class. For example:
from django.db import models
class MarkupAccessManager(models.Manager):
def count(self):
return super(MarkupAccessManager, self).count()
count.expose_to_markup = True
class AfricanSwallow(models.Model):
objects = MarkupAccessManager()
To refer to models from the markup, you could take advantage of the django.contrib.contenttypes framework and the tags could have the following format: app_label.model_name action or app_label.model_name action arg1 arg2.
Depending on the markup language you choose, you could either use custom tags (if the language provides them), Django template tags, or plain regular expressions. Once you get the contents of a tag, this is how you could replace it with the output of the referred method:
from django.contrib.contenttypes.models import ContentType
def replace_tag(tag):
"""
'birds.africanswallow count' => birds.models.AfricanSwallow.objects.count()
"""
bits = tag.split()
model_ref = bits[0]
action = bits[1]
args = bits[2:]
try:
ct = ContentType.objects.get_by_natural_key(*model_ref.split('.'))
except ContentType.DoesNotExist:
return 'Invalid model reference.'
model = ct.model_class()
method = getattr(model._base_manager, action, None)
if not method or not method.expose_to_markup:
return 'Invalid action.'
return method(*args)
To provide autocomplete, something along these lines would help you to build a list of all the available options:
from django.db.models.loading import get_models
from django.contrib.contenttypes.models import ContentType
def model_refs():
for model in get_models():
if isinstance(model._base_manager, MarkupAccessManager):
ct = ContentType.objects.get_for_model(model)
yield '%s.%s' % (ct.app_label, ct.model)
def actions():
for attr_name in dir(MarkupAccessManager):
attr = getattr(MarkupAccessManager, attr_name)
if attr.expose_to_markup:
yield attr.__name__
I haven't tested the code. Hope this helps a bit.
Most elegant solution would be to create a compiler that will allow execution of only certian instructions. Find out more # http://en.wikibooks.org/wiki/Compiler_Construction
Another way is to use exec() but you should avoid this as it brings a lot of security issues into your application. You can always try to parse the string first (for valid syntax) but it will still be a possible vulnerability.
I am trying out django-registration. I found that it allows multiple registration for same emailid. I want to prevent that. What is the best way to do that?
ok, I see there is a subclass RegistrationFormUniqueEmail. Now, how to use this class? I changed this
def get_form_class(self, request):
return RegistrationFormUniqueEmail
But, it must be better to change this from my application rather than in source code. So, how do I do that?
thanks
Once you've added registration to your settings file, you can use the form in your views.py like so:
from registration.forms import RegistrationFormUniqueEmail
form = RegistrationFormUniqueEmail()
That's it. That will give you the form that you need and will take care of the unique email validation.