passport / app /apiview.py
thuralinhtut's picture
Add application file
ca9772c
from datetime import timedelta
import operator
import functools
import collections
from collections import OrderedDict
from datetime import datetime
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework.generics import CreateAPIView
from rest_framework import status
from rest_framework.authtoken.models import Token
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth import get_user_model
from django.utils import timezone
from . import models, serializers
from . import RemoveBg, PaperLayout
import json
from PIL import Image
import base64
def write_file_from_object_file(object_file, file_path):
file_data = base64.b64decode(object_file)
with open(file_path, 'wb') as file:
file.write(file_data)
class CreateUserApiView(CreateAPIView):
permission_classes = [AllowAny]
serializer_class = serializers.CreateUserSerializer
def post(self, request):
print('posteed')
print(request.data)
serializers = self.get_serializer(data=request.data)
serializers.is_valid(raise_exception=True)
self.perform_create(serializers)
headers = self.get_success_headers(serializers.data)
# Create a token than will be used for future auth
token = Token.objects.create(user=serializers.instance)
token_data = {'token': token.key}
return Response(
{**serializers.data, **token_data},
status=status.HTTP_201_CREATED,
headers=headers)
class SendImage(APIView):
def get(self,request):
return Response(status=status.HTTP_200_OK)
def post(self,request):
print(request.data)
image = request.data['image']
qty = request.data['qty']
size = request.data['img_size'] # img size
bw = request.data['bw'] # img is black or white
paper = request.data['paper_size'] # paper size to print out
bgcolor = request.data['bg_color']
try:
PPD = models.PassportDetail.objects.get(is_done=False)
except ObjectDoesNotExist:
#the passport detail dnexist create one
PPD = models.PassportDetail.objects.create(size=size,paper=paper,bgcolor=bgcolor)
if bw == "true":
PPD.bw = True
PPD.save()
models.Images.objects.create(image=image,img_qty=qty,ppd=PPD)
# models.Images.objects.create(image=image,img_qty=qty)
return Response(PPD.id)
class GenerateImage(APIView):
def get(self,request):
PDDID = request.GET.get('pdid')
PD = models.PassportDetail.objects.get(id=PDDID)
PD.is_done = True
PD.save()
images_list = models.Images.objects.filter(ppd=PD)
images = []
for img in images_list:
print(img)
images.append((img.image.url[1:],img.img_qty))
paper_size = tuple(int(x) for x in PD.paper.split(","))
img_size = tuple(float(x) for x in PD.size.split(","))
bg_color = tuple(int(x) for x in PD.bgcolor.split(","))
gap = 40; # gap between images;
img_max_width = img_size[0] * 300
img_max_height = img_size[1] * 300
aspect_ratio = img_max_width / img_max_height
output_di = 'img/'
print("Start Removing",PD.bw)
RemoveBg.RemoveBackground(images,aspect_ratio,bg_color,output_di,PD.bw).remove()
PaperLayout.GeneratePhoto(paper_size,gap,images,img_max_width,img_max_height,raw_export=output_di).export()
print("end Removing")
return Response(status=status.HTTP_200_OK)
# const token = '5499662958:AAEAoWy0khLJMf238ZwvvEoBV1FKfaVF6S0';
# const chatid = '-618629809';
#
# 'https://api.telegram.org/bot' +
# token +
# '/sendDocument?chat_id=' +
# chatid;
# class SalesTwoDigits(APIView):
# # permission_classes = [AllowAny]P
# def get(self, request):
# user = get_user_model().objects.get(username=request.user,is_plan=True,
# is_salesDigits=True)
# try:
# G = models.TwoDigitsGroup.objects.get(user=user, is_done=False)
# print('Group M Exists')
# except ObjectDoesNotExist:
# print('Group M Not Exits')
# G = models.TwoDigitsGroup.objects.create(user=user)
# print('Group M Created')
# data = models.SalesTwoDigits.objects.filter(user=user,group=G)
# ser = serializers.SalesTwoDigitSerializer(data,many=True)
# return Response(ser.data)
# def post(self, request):
# name = request.data['customername']
# phoneno = request.data['phoneno']
# digits = request.data['digits']
# totalamount = request.data['totalamount']
# print(request.user)
# user = get_user_model().objects.get(username=request.user,is_plan=True,
# is_salesDigits=True)
# try:
# G = models.TwoDigitsGroup.objects.get(user=user, is_done=False)
# print('Group M Exists')
# except ObjectDoesNotExist:
# print('Group M Not Exits')
# G = models.TwoDigitsGroup.objects.create(user=user)
# print('Group M Created')
# # print(str(name) +'\n')
# # print(str(phoneno) + '\n')
# # print(digits)
# # print(int(totalamount))
# Sales = models.SalesTwoDigits.objects.create(customername=name,phoneno=phoneno,totalprice=totalamount,user=user,group=G)
# # Sales1 = models.SalesTwoDigits.objects.create(customername=name,phoneno=phoneno,totalprice=totalamount,user=user,group=G1)
# print('Sales Objects Created')
# ds = json.loads(digits)
# # print(p)
# for d in ds:
# print(d)
# d1 = models.TwoDigits.objects.create(number=d['digits'],amount=d['amount'],user=user,sales=Sales)
# print('D1 Create')
# print('Finished All')
# # product = models.Product.objects.get(id=b['name'], user=user)
# # product.qty = int(product.qty) - int(b['qty'])
# # product.save()
# # a = models.SoldProduct.objects.create(
# # name=product, price=b['price'], qty=b['qty'], sales=S)
# # print(a)
# # S.save()
# return Response(status=status.HTTP_201_CREATED)