-
-
Save xmanemran/3bea651cc9fd38ce30a60de7fab473a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django_renderpdf.views import PDFView | |
from rest_framework import status, views, viewsets | |
from rest_framework.permissions import IsAuthenticated | |
from rest_framework.response import Response | |
from core.models import LocalDescription | |
from invoice.utils import get_pdf_download | |
from user.permissions import * | |
from .serializers import * | |
logger = logging.getLogger(__name__) | |
class OCRGeneratorAPIView(views.APIView): | |
def get(self, request): | |
now = str(datetime.datetime.now()) | |
replaceable_char = ['-', ':', '.', ' '] | |
for ch in replaceable_char: | |
now = now.replace(ch, "") | |
result = {"ocr": now[2:]} | |
return Response(result) if request.user.is_authenticated else Response( | |
{"error": "User is not authenticated"}) | |
class InvoiceAPIViewSet(viewsets.ModelViewSet): | |
queryset = Invoice.objects.all() | |
serializer_class = InvoiceSerializer | |
permission_classes = (IsAuthenticated,) | |
class InvoicePDFAPIView(views.APIView): | |
def get(self, request, format=None): | |
try: | |
ocr = request.GET['ocr'] | |
invoice_filter_data = Invoice.objects.filter(ocr_number=ocr) | |
if invoice_filter_data.exists(): | |
invoice = invoice_filter_data.first() | |
response = get_pdf_download(invoice) | |
return response | |
else: | |
return Response(data={"detail": "OCR Not Found"}, | |
status=status.HTTP_404_NOT_FOUND) | |
except: | |
return Response(data={"detail": "OCR Key Error"}, | |
status=status.HTTP_404_NOT_FOUND) | |
class InvoicePDFView(PDFView): | |
template_name = 'invoice_pdf_template.html' | |
base_url = 'file://' + settings.STATIC_ROOT | |
download_filename = 'hello.pdf' | |
def get_context_data(self, *args, **kwargs): | |
logger.critical(kwargs["invoice_id"]) | |
invoice_id = kwargs["invoice_id"] | |
invoice_instance = Invoice.get_invoice_instance(invoice_id) | |
profile_instance = Profile.objects.all().first() | |
if invoice_instance is not None: | |
award, bank_giro, city, currency, customer_number, departure_date, invoice_created_at, invoice_number, ocr_number, postcode, street, tour_name = invoice_instance.get_data_for_pdf() | |
return super(InvoicePDFView, self).get_context_data( | |
pagesize='A4', | |
invoice_number=invoice_number, | |
street=street, | |
postcode=postcode, | |
city=city, | |
departure_date=departure_date, | |
invoice_created_at=invoice_created_at, | |
customer_number=customer_number, | |
bank_giro=bank_giro, | |
ocr_number=ocr_number, | |
tour_name=tour_name, | |
award=award, | |
currency=currency, | |
due_date=invoice_created_at + datetime.timedelta(days=1), | |
organisation_number=profile_instance.organisation_number if profile_instance else None, | |
iban=profile_instance.iban if profile_instance else None, | |
bic=profile_instance.bic if profile_instance else None, | |
telephone=profile_instance.telephone if profile_instance else None, | |
email=profile_instance.email if profile_instance else None, | |
website=profile_instance.website if profile_instance else None, | |
sender=profile_instance.sender if profile_instance else None, | |
*args, | |
**kwargs | |
) | |
else: | |
return super(InvoicePDFView, self).get_context_data( | |
pagesize='A4', | |
*args, | |
**kwargs | |
) | |
class InvoicePDFDownloadAPIView(PDFView): | |
template_name = 'invoice_pdf_template.html' | |
base_url = 'file://' + settings.STATIC_ROOT | |
download_filename = 'hello.pdf' | |
prompt_download = True | |
@property | |
def download_name(self): | |
invoice_instance = Invoice.get_invoice_instance(self.kwargs.get('invoice_id')) | |
return invoice_instance.get_invoice_pdf_name() | |
def get_context_data(self, *args, **kwargs): | |
logger.critical(kwargs["invoice_id"]) | |
invoice_id = kwargs["invoice_id"] | |
invoice_instance = Invoice.get_invoice_instance(invoice_id) | |
profile_instance = Profile.objects.all().first() | |
if invoice_instance is not None: | |
award, bank_giro, city, currency, customer_number, departure_date, invoice_created_at, invoice_number, ocr_number, postcode, street, tour_name = invoice_instance.get_data_for_pdf() | |
return super(InvoicePDFDownloadAPIView, self).get_context_data( | |
pagesize='A4', | |
invoice_number=invoice_number, | |
street=street, | |
postcode=postcode, | |
city=city, | |
departure_date=departure_date, | |
invoice_created_at=invoice_created_at, | |
customer_number=customer_number, | |
bank_giro=bank_giro, | |
ocr_number=ocr_number, | |
tour_name=tour_name, | |
award=award, | |
currency=currency, | |
due_date=invoice_created_at + datetime.timedelta(days=1), | |
organisation_number=profile_instance.organisation_number if profile_instance else None, | |
iban=profile_instance.iban if profile_instance else None, | |
bic=profile_instance.bic if profile_instance else None, | |
telephone=profile_instance.telephone if profile_instance else None, | |
email=profile_instance.email if profile_instance else None, | |
website=profile_instance.website if profile_instance else None, | |
sender=profile_instance.sender if profile_instance else None, | |
*args, | |
**kwargs | |
) | |
else: | |
return super(InvoicePDFDownloadAPIView, self).get_context_data( | |
pagesize='A4', | |
*args, | |
**kwargs | |
) | |
class InvoiceDownloadAPIView(PDFView): | |
# permission_classes = (IsAuthenticated,) | |
template_name = 'invoice_pdf_template.html' | |
base_url = 'file://' + settings.STATIC_ROOT | |
download_filename = 'hello.pdf' | |
prompt_download = True | |
@property | |
def download_name(self): | |
invoice = Invoice.objects.get_invoice_by_transaction_id(self.kwargs.get('transaction_id')) | |
return invoice.get_invoice_pdf_name() | |
def get_context_data(self, *args, **kwargs): | |
logger.critical(kwargs["transaction_id"]) | |
print("Hello before get") | |
invoice = Invoice.objects.get_invoice_by_transaction_id(self.kwargs.get('transaction_id')) | |
print("Hello after get") | |
profile_instance = Profile.objects.all().first() | |
if invoice is not None: | |
award, bank_giro, city, currency, customer_number, departure_date, invoice_created_at, invoice_number, ocr_number, postcode, street, tour_name = invoice.get_data_for_pdf() | |
return super(InvoiceDownloadAPIView, self).get_context_data( | |
pagesize='A4', | |
invoice_number=invoice_number, | |
street=street, | |
postcode=postcode, | |
city=city, | |
departure_date=departure_date, | |
invoice_created_at=invoice_created_at, | |
customer_number=customer_number, | |
bank_giro=bank_giro, | |
ocr_number=ocr_number, | |
tour_name=tour_name, | |
award=award, | |
currency=currency, | |
due_date=invoice_created_at + datetime.timedelta(days=1), | |
organisation_number=profile_instance.organisation_number if profile_instance else None, | |
iban=profile_instance.iban if profile_instance else None, | |
bic=profile_instance.bic if profile_instance else None, | |
telephone=profile_instance.telephone if profile_instance else None, | |
email=profile_instance.email if profile_instance else None, | |
website=profile_instance.website if profile_instance else None, | |
sender=profile_instance.sender if profile_instance else None, | |
*args, | |
**kwargs | |
) | |
else: | |
return super(InvoiceDownloadAPIView, self).get_context_data( | |
pagesize='A4', | |
*args, | |
**kwargs | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment