Django rest api custom class implementation - django

I have created a class for retrieving data about a specific food product from farsecret API.
In class I have created 3 functions for:
*obtaining authorization
*getting id of item, for which we are looking
*download data of item
class IngredientImport(APIView):
def get_authorization_token(self):
request_token_url = "https://oauth.fatsecret.com/connect/token"
consumer_key = os.environ.get('NTS_Client_ID')
consumer_secret = os.environ.get('NTS_Client_Secret')
data = {'grant_type':'client_credentials', "scope":"basic"}
access_token_response = requests.post(
request_token_url,
data=data,
verify=False,
allow_redirects=False,
auth=(consumer_key, consumer_secret)
)
return access_token_response.json()["access_token"]
def get_list_by_name(self, name, access_token):
api_url = "https://platform.fatsecret.com/rest/server.api"
params={
"method":"foods.search",
"search_expression":name,
"page_number":1,
"max_results":1,
"format":"json"
}
header = {"Authorization":access_token}
api_call_headers = {"Authorization": "Bearer " + access_token}
response = requests.get(
api_url,
params=params,
headers=api_call_headers
)
items = response.json()["foods"]
try:
return response.json()["foods"]["food"]["food_id"]
except KeyError:
return None
def import_item(self, item_id, api_token):
if item_id == None:
return None
api_url = "https://platform.fatsecret.com/rest/server.api"
params={"method":"food.get", "food_id":item_id, "format":"json"}
api_call_headers = {"Authorization": "Bearer " + access_token}
response = requests.get(
api_url,
params=params,
headers=api_call_headers
)
item = response.json()["food"]["servings"]["serving"]
item_name = response.json()["food"]["food_name"]
if type(item) == list:
item = item[0]
try:
portion_size = float(item["metric_serving_amount"])
carbs = round(float(item["carbohydrate"]) / portion_size * 100, 2)
fats = round(float(item["fat"]) / portion_size * 100, 2)
proteins = round(float(item["protein"]) / portion_size * 100, 2)
except KeyError:
return None
How can I implement this class in my aplication to avoid creating 3 different paths in urls.py for each function. Is it possible or should I break it into function-based views?

class IngredientImport(APIView):
def get_authorization_token(self):
...
def get_list_by_name(self, name, access_token):
...
def import_item(self, item_id, api_token):
...
def get(self, request):
# get name from query param
name = self.request.GET.get('name')
token = self.get_authorization_token()
food_list = self.get_list_by_name(name, token)
for food_id in food_list:
self.import_item(food_id, token)
return Response({'imported_foods': food_list})
Then, on your urls.py:
urlpatterns = [
path('import_foods', IngredientImport.as_view())
]

Related

how to merge two views functions with minor differences

I just wrote two view functions for two different models but they are very similar and only different in some names. what is the best way to merge these two view functions to prevent code repetition?
these are the view functions:
def manager_scientific(request, *args, **kwargs):
context = {}
if request.user.role == 'manager':
template = loader.get_template('reg/scientific-manager-dashboard.html')
users = User.objects.filter(role='applicant', isPreRegistered=True, scientificinfo__is_interviewed=True).order_by('-date_joined')
approved = ScientificInfo.objects.filter(is_approved__exact='0').all()
context['users'] = users
context['all_users'] = len(users)
context['approved'] = len(approved)
context['survey_choices'] = SURVEY_CHOICES
if request.GET.get('user', 'all') == 'all':
users = users
if request.GET.get('user', 'all') == 'new':
users = users.filter(scientificinfo__is_approved__exact='0')
field_list = request.GET.getlist('field')
if field_list:
if 'None' in field_list:
users = users.filter(fields__title=None) | users.filter(fields__title__in=field_list)
else:
users = users.filter(fields__title__in=field_list)
gender_list = request.GET.getlist('gender')
if gender_list:
users = users.filter(gender__in=gender_list)
education_list = request.GET.getlist('education')
if education_list:
users = users.filter(educationalinfo__grade__in=education_list)
work_list = request.GET.getlist('work')
if work_list:
users = users.filter(workinfo__position__in=work_list)
province_list = request.GET.getlist('province')
if province_list:
if 'None' in province_list:
users = users.filter(prevaddress__address_province__in=province_list) | users.filter(
prevaddress__address_province=None)
else:
users = users.filter(prevaddress__address_province__in=province_list)
query_string = request.GET.get('query_string')
if query_string:
name_query = None
for term in query_string.split():
if name_query:
name_query = name_query & (Q(first_name__contains=term) | Q(last_name__contains=term))
else:
name_query = Q(first_name__contains=term) | Q(last_name__contains=term)
users = users.filter(name_query |
Q(educationalinfo__field__contains=query_string) |
Q(educationalinfo__tendency__contains=query_string) |
Q(educationalinfo__university_name__contains=query_string) |
Q(workinfo__organization__contains=query_string) |
Q(ngoinfo__ngo_name__contains=query_string) |
Q(melli_code__contains=query_string))
users = users.distinct()
context['grade_choices'] = []
for g, grade in EDUCATIONAL_GRADE_CHOICES:
count = EducationalInfo.objects.filter(user__in=users, grade=g).count()
context['grade_choices'].append((g, grade, count))
context['work_position_choices'] = []
for p, position in WORK_POSITION_CHOICES:
count = WorkInfo.objects.filter(user__in=users, position=p).count()
context['work_position_choices'].append((p, position, count))
provinces = users.values_list('prevaddress__address_province')
provinces = Counter([d[0] for d in provinces])
context['provinces'] = provinces.items()
paginator = Paginator(users, 25) # Show 25 contacts per page.
page_number = request.GET.get('page', 1)
page_obj = paginator.get_page(page_number)
context['users'] = page_obj
context['is_interviewed'] = ScientificInfo.objects.filter(is_approved__exact='0', user__is_staff=False).count()
context['not_interviewed'] = ScientificInfo.objects.filter(is_interviewed=True, user__is_staff=False).count()
context['men'] = users.filter(gender="male").count()
context['women'] = users.filter(gender="female").count()
context['query_string'] = query_string
return HttpResponse(template.render(request=request, context=context))
and the other one:
def manager_religious(request, *args, **kwargs):
context = {}
if request.user.role == 'manager':
template = loader.get_template('reg/religious-manager-dashboard.html')
users = User.objects.filter(role='applicant', isPreRegistered=True, religiousinfo__is_interviewed=True).order_by('-date_joined')
approved = ReligiousInfo.objects.filter(is_approved__exact='0').all()
context['users'] = users
context['all_users'] = len(users)
context['approved'] = len(approved)
context['survey_choices'] = SURVEY_CHOICES
if request.GET.get('user', 'all') == 'all':
users = users
if request.GET.get('user', 'all') == 'new':
users = users.filter(religiousinfo__is_approved__exact='0')
field_list = request.GET.getlist('field')
if field_list:
if 'None' in field_list:
users = users.filter(fields__title=None) | users.filter(fields__title__in=field_list)
else:
users = users.filter(fields__title__in=field_list)
gender_list = request.GET.getlist('gender')
if gender_list:
users = users.filter(gender__in=gender_list)
education_list = request.GET.getlist('education')
if education_list:
users = users.filter(educationalinfo__grade__in=education_list)
work_list = request.GET.getlist('work')
if work_list:
users = users.filter(workinfo__position__in=work_list)
province_list = request.GET.getlist('province')
if province_list:
if 'None' in province_list:
users = users.filter(prevaddress__address_province__in=province_list) | users.filter(
prevaddress__address_province=None)
else:
users = users.filter(prevaddress__address_province__in=province_list)
query_string = request.GET.get('query_string')
if query_string:
name_query = None
for term in query_string.split():
if name_query:
name_query = name_query & (Q(first_name__contains=term) | Q(last_name__contains=term))
else:
name_query = Q(first_name__contains=term) | Q(last_name__contains=term)
users = users.filter(name_query |
Q(educationalinfo__field__contains=query_string) |
Q(educationalinfo__tendency__contains=query_string) |
Q(educationalinfo__university_name__contains=query_string) |
Q(workinfo__organization__contains=query_string) |
Q(ngoinfo__ngo_name__contains=query_string) |
Q(melli_code__contains=query_string))
users = users.distinct()
context['grade_choices'] = []
for g, grade in EDUCATIONAL_GRADE_CHOICES:
count = EducationalInfo.objects.filter(user__in=users, grade=g).count()
context['grade_choices'].append((g, grade, count))
context['work_position_choices'] = []
for p, position in WORK_POSITION_CHOICES:
count = WorkInfo.objects.filter(user__in=users, position=p).count()
context['work_position_choices'].append((p, position, count))
provinces = users.values_list('prevaddress__address_province')
provinces = Counter([d[0] for d in provinces])
context['provinces'] = provinces.items()
paginator = Paginator(users, 25) # Show 25 contacts per page.
page_number = request.GET.get('page', 1)
page_obj = paginator.get_page(page_number)
context['users'] = page_obj
context['is_interviewed'] = ReligiousInfo.objects.filter(is_approved__exact='0', user__is_staff=False).count()
context['not_interviewed'] = ReligiousInfo.objects.filter(is_interviewed=True, user__is_staff=False).count()
context['men'] = users.filter(gender="male").count()
context['women'] = users.filter(gender="female").count()
context['query_string'] = query_string
return HttpResponse(template.render(request=request, context=context))
the only differences are in model names and template addresses.
and also how can I rewrite them in a class based format?
You can create one common class that inherits from View Class and then two separate classes that inherit from the previous one. e.g
class ManagerView(View)
template_name = None
model = None
def get(self, request):
...
template = loader.get_template(self.template_name)
approved = self.model.objects.filter(is_approved__exact='0').all()
...
class ManagerReligiousView(ManagerView)
template_name = 'reg/religious-manager-dashboard.html'
model = ReligiousInfo
class ManagerScientificView(ManagerView)
template_name ='reg/scientific-manager-dashboard.html'
model = ScientificInfo
Another way with a class-based view is to emply the rarely-used ability to pass configuration options to it in the url.
class SomeView( AnyClassBasedView):
foo = 'bar'
In urls.py
path( '/do_bar/<int:pk>', SomeView.as_view(), name='do_bar'),
path( '/do_baz/<int:pk>', SomeView.as_view( foo='baz'), name='do_baz'),
And in SomeView, conditional tests on self.foo. Note, you have to declare any such parameter with a default value in the class definition before you can use it in urls.py.
A variant is to use write a view that can handle >1 method of accessing an object by interrogating self.kwargs
path( '/x/<int:pk>', X_View.as_view(), name='x_by_pk'),
path( '/x/<str:order_number>', X_View.as_view(), name='x_by_order'),
where typically the get_object method is subclassed:
class X_View( DetailView):
...
def get_object( self, queryset=None):
if 'pk' in self.kwargs:
obj = get_object_or_404( X, pk=self.kwargs['pk'] )
elif 'order_number' in self.kwargs:
obj = get_object_or_404( X, order_number=self.kwargs['order_number'] )
...
else:
raise ConfigurationError( 'Check urls.py. No valid kwarg was parsed')
return obj
NB this is a simplified get_object which ignores queryset. See the code of get_object for a better template to slot this pattern into. Classy CBVs to the rescue.

how to pass data in array to post method in angular and django?

I am trying to pass a list of student IDs but it is not working.
from angular I am passing data as follows
let noticeData = this.announceForm.value;
if (noticeData.students.includes('all')){
noticeData.students = noticeData.students.filter((s) => s != 'all')
}
noticeData.students = JSON.stringify(noticeData.students);
rest call
var formData = new FormData();
for (var key in noticeData) {
formData.set(key, noticeData[key]);
}
this._rest.one("/classwork/notices/")
.post('', formData)
.toPromise()
.then((classworks) => {
}, (error) => {
console.log("ERROR SENDING ANNOUNCEMENT ", error);
this.alert = {
type : "error",
message: "Something went wrong, please try again."
}
});
}
from django i am reading and trying to store data as
Below is the to_internal_value method which I have overridden.
def to_internal_value(self, data):
tempdict = data.copy()
tempdict['students'] = json.loads(data['students'])
data = tempdict
return super(NoticesSerializer, self).to_internal_value(data)
views.py
class ClassworkView(APIView):
def get(self, request, format=None):
notices = ClassworksFilter(request.query_params, queryset=Notice.objects.all()).qs
materials = ClassworksFilter(request.query_params, queryset=Material.objects.all()).qs
assignments = ClassworksFilter(request.query_params, queryset=Assignment.objects.all()).qs
queryset = sorted(
chain(notices, materials, assignments),
key=lambda instance: instance.create_date
)
model_serializers = {
'notice': NoticesSerializer,
}
classworks = []
for instance in queryset:
serializer = model_serializers[instance._meta.model_name]
data = serializer(instance).data
data['classwork_type'] = instance._meta.model_name
classworks.append(data)
page_number = request.query_params.get('page', 1)
page_size = request.query_params.get('page_size', 20)
page = Paginator(classworks, per_page=page_size).get_page(page_number)
base_url = f'{request.scheme}://{request.get_host()}{reverse("classworks")}'
params = [f'{k}={v}' if k!='page' else None for k,v in request.query_params.items()]
indx = 0
for param in params:
if param==None:
params.pop(indx)
indx+=1
params = '&'.join(params)
next = f'{base_url}?page={page.next_page_number()}&{params}' if page.has_next() else None
prev = f'{base_url}?page={page.previous_page_number()}&{params}' if page.has_previous() else None
response = {
'count': len(classworks),
'next': next,
'previous': prev,
'results': page.object_list
}
return Response(response)
here is serializer.py
class NoticesSerializer(serializers.ModelSerializer):
class Meta:
model = Notice
fields = '__all__'
def to_representation(self, instance):
representation = super(NoticesSerializer, self).to_representation(instance)
try:
representation['classroom'] = ClassroomsSerializer(instance.classroom, context=self.context).data
students = StudentsSerializer(instance.students, many=True, context=self.context).data
for student in students:
student['classroom'] = student['classroom']['id']
representation['students'] = students
except Exception as error:
print("ERROR SETTING REPRESENTATION FOR NOTICE SERIALIZER", error)
return representation
here is the models.py
class Notice(Classwork):
file = models.FileField(upload_to='classworks/attachments', blank=True, null=True)
link = models.URLField(blank=True, null=True)
description = models.TextField()
class Meta(BaseModel.Meta):
db_table = 'ec_notice'
but it always returns the same error
{students: [“Incorrect type. Expected pk value, received list.”]}
here the data I am posting by form data

How i can override user save Django allauth.socialaccounts

I need to override user save method in my custom OAuth2Adapter. How i can do this?
I need save some special fields in my user Model
auth_provider/views.py
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from config.settings.base import EXAMPLE_URLS
from app.users.auth_provider.provider import ExampleProvider
class ExampleAdapter(OAuth2Adapter):
provider_id = ExampleProvider.id
access_token_url = EXAMPLE_URLS["web"] + "oauth/token"
authorize_url = EXAMPLE_URLS["web"] + "oauth/authorize"
profile_url = EXAMPLE_URLS["api"] + "me"
def complete_login(self, request, app, access_token, **kwargs):
headers = {"Authorization": "Bearer %s" % access_token}
extra_data = requests.get(self.profile_url, headers=headers)
return self.get_provider().sociallogin_from_response(request, extra_data.json())
oauth2_login = OAuth2LoginView.adapter_view(ExampleAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(ExampleAdapter)

Django Rest Framework + Datatables + Class Based Views + Server Side Processing

How to use Django Rest Framework + Datatables + Class Based Views?
This is a python snipped to use Django Rest Framework + Datatables + Class Based Views.
import re
from rest_framework.generics import GenericAPIView
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response
class DataTablePagination(LimitOffsetPagination): # pragma: no cover
"""Custom pagination class for datatable server side processing."""
limit_query_param = "length"
offset_query_param = "start"
def get_paginated_response(self, data):
"""See datatables server side processing."""
return Response({
"recordsTotal": self.count,
"recordsFiltered": self.count,
"data": data,
"draw": int(self.request.GET["draw"])
})
#staticmethod
def get_datatable_ordering(request):
"""Parse datatables conf parameters."""
ordering = []
columns = dict()
for key in request.GET:
if key.startswith('columns'):
col = dict()
keys = [i[1:-1] for i in re.findall(r'\[\w+\]', key)]
for i in range(len(keys) - 1):
if i == 0:
if keys[i] not in columns: columns[keys[i]] = dict()
col = columns[keys[i]]
else:
if keys[i] not in col: col[keys[i]] = dict()
col = col[keys[i]]
col[keys[-1]] = request.GET[key]
if 'order[0][column]' in request.GET:
ret = columns[request.GET['order[0][column]']]['data']
if request.GET['order[0][dir]'] != 'asc': ret = '-' + ret
ordering.append(ret.replace('.', '__'))
return ordering
class DatatablesView(GenericAPIView): # pragma: no cover
"""Base view with datatables server side integration."""
ordering = ("id",)
def initialize_request(self, request, *args, **kwargs):
"""Enable server side processing for datatables and `q` searching."""
request.GET = request.GET.copy()
if 'draw' in request.GET:
self.pagination_class = DataTablePagination
self.ordering = self.pagination_class.get_datatable_ordering(
request) or self.ordering
if 'length' in request.GET and str(request.GET['length']) == '-1':
request.GET['length'] = 1e9
if 'search[value]' in request.GET:
request.GET['search'] = request.GET['search[value]']
elif 'q' in request.GET and 'search' not in request.GET:
request.GET['search'] = request.GET['q']
return super().initialize_request(request, *args, **kwargs)

django save not working as update in views.py with model having an overridden save

I've looked at all the related answers but none fixed my issue. I'm trying to save an object that's already created with no luck. I could see the view getting executed and the values I updated for the object but changes are not reflecting in the database. Here is the code snippet of the view and the model.
views.py
class Workspace(_LoggedInMixin, View):
def get(self, request):
user = self.request.user
components = ComponentModel.objects.filter(Q(user=user) | Q(user=None)).order_by('time')
component_status = request.session.get('component', None)
request.session['component'] = None
params = dict(components=components, status=component_status)
return render(request, 'workspace.html', params)
def post(self, request):
data = request.POST
formtype = data['form-type']
error = None
user = self.request.user
if formtype == 'component':
if data['action'] == 'create':
try:
if not os.path.exists('D:/' + self.request.user.username):
os.makedirs('D:/' + self.request.user.username)
cparent = ComponentModel.objects.get(pk=data['cparent'])
component = ComponentModel(user=user, name=data['cname'], time=dt.now(), stats=data['cstats'],
output=data['coutput'], parent=cparent)
component.save()
file = open('D:/' + self.request.user.username + '/' + str(component.id) + '.py', 'w+')
usercode = data['usercode']
usercode = "\n".join(usercode.split("\r\n"))
file.write(usercode)
component.codefile = 'D:/' + self.request.user.username + '/' + str(component.id) + '.py'
component.save()
request.session['component'] = {'name': data['cname'], 'message': 'Component created successfully!'}
except Exception as e:
component.delete()
error = e.message
elif data['action'] == 'delete':
try:
c = ComponentModel.objects.get(pk=data['compid'])
cname = c.name
c.delete()
os.remove('D:/' + self.request.user.username + '/' + data['compid'] + '.py')
request.session['component'] = {'name': cname, 'message': 'Component deleted successfully!'}
except Exception as e:
error = e.message
elif data['action'] == 'save':
try:
if not os.path.exists('D:/' + self.request.user.username):
os.makedirs('D:/' + self.request.user.username)
cparent = ComponentModel.objects.get(pk=data['cparent'])
component = ComponentModel.objects.get(pk=data['compid'])
component.user = user
component.name = data['cname']
component.time = dt.now()
component.stats = data['cstats']
component.output = data['coutput']
component.parent = cparent
component.save()
print component
file = open('D:/' + self.request.user.username + '/' + str(component.id) + '.py', 'w+')
usercode = data['usercode']
usercode = "\n".join(usercode.split("\r\n"))
file.write(usercode)
request.session['component'] = {'name': data['cname'], 'message': 'Component saved successfully!'}
except Exception as e:
error = e.message
if error is not None:
components = ComponentModel.objects.filter(Q(user=user) | Q(user=None)).order_by('time')
params = dict(error=error, components=components)
return render(request, 'workspace.html', params)
return redirect('/workspace')
models.py
class ComponentModel(models.Model):
class Meta:
# overriding the default table name with the following name
db_table = 'components'
verbose_name = 'components'
get_latest_by = 'time'
user = models.ForeignKey('auth.User', on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=255)
time = models.DateTimeField(db_column='created_on')
codefile = models.CharField(max_length=100, db_column='code_file_name', null=True)
stats = models.TextField(db_column='statistical_fields', null=True)
output = models.TextField(db_column='output_fields')
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
def save(self, *args, **kwargs):
try:
super(ComponentModel, self).save(self, *args, **kwargs)
except Exception as e:
return "Something went wrong while saving the component. Details - %s" % e.message
def __str__(self):
return "{id: %s, name: %s, user: %s, time: %s, parent: %s}" % (
self.id, self.name, self.user, self.time, self.parent)
The second save call in data['action'] == 'create' and the save call in data['action'] == 'save' are not updating the database. I appreciate any help. Thank you.
Your super call is incorrect. Update so that self is not called as a method argument - as you have it written it will always fail.
In addition, you are not raising the exception when a save is not completed, so you have no idea if there was an error unless viewing the std output. You probably want this to actually raise an error.
Update as such -
def save(self, *args, **kwargs):
try:
super(ComponentModel, self).save(*args, **kwargs)
except Exception as e:
raise Exception("Something went wrong while saving the component. Details - %s" % (e.message,))