96 lines
3.6 KiB
Python
96 lines
3.6 KiB
Python
from django.shortcuts import render, HttpResponse, get_object_or_404, Http404, redirect
|
|
from .models import resulation
|
|
from datetime import datetime
|
|
import os, uuid
|
|
import misdghs.settings
|
|
from django.http import FileResponse, StreamingHttpResponse
|
|
from urllib.parse import unquote
|
|
from misdghs import settings
|
|
from django.core.files.storage import default_storage
|
|
|
|
# Create your views here.
|
|
def Home(request):
|
|
return render(request,'resulation/home.html')
|
|
|
|
def Savepdf(request):
|
|
if request.method == 'POST':
|
|
try:
|
|
# Get text form fields
|
|
pdftype= request.POST.get('pdftype')
|
|
org_unit = request.POST.get('org_unit')
|
|
topic = request.POST.get('topic')
|
|
tittle = request.POST.get('tittle')
|
|
m_number = request.POST.get('m_number')
|
|
m_date = request.POST.get('m_date')
|
|
m_venue = request.POST.get('m_venue')
|
|
|
|
# Get file uploads
|
|
attendance_file = request.FILES.get('attendance_file')
|
|
res_con_mou_file = request.FILES.get('res_con_mou_file')
|
|
|
|
# Create a new seminar instance with text fields
|
|
if attendance_file:
|
|
new_filename1 = f"attendance_{uuid.uuid4()}.pdf"
|
|
path1 = os.path.join('media', 'attendance_files', new_filename1)
|
|
|
|
with open(path1, 'wb') as destination:
|
|
for chunk in attendance_file.chunks():
|
|
destination.write(chunk)
|
|
|
|
if res_con_mou_file:
|
|
new_filename2 = f"res_con_{uuid.uuid4()}.pdf"
|
|
path2 = os.path.join('media', 'resolution_files', new_filename2)
|
|
|
|
print(path2)
|
|
with open(path2, 'wb') as destination:
|
|
for chunk in res_con_mou_file.chunks():
|
|
destination.write(chunk)
|
|
seminar = resulation(
|
|
pdftype=pdftype,
|
|
org_unit=org_unit,
|
|
topic=topic,
|
|
tittle=tittle,
|
|
m_number=m_number,
|
|
m_date=m_date,
|
|
m_venue=m_venue,
|
|
attendance_file=new_filename1,
|
|
res_con_mou_file=new_filename2,
|
|
)
|
|
seminar.save()
|
|
print(path1)
|
|
message = "Data Inserted successfully"
|
|
# Redirect after a successful save
|
|
return render(request, "resulation/home.html", {'message': message})
|
|
|
|
except Exception as e:
|
|
# Handle any errors (e.g., invalid file types or sizes)
|
|
print(f"Error processing form: {str(e)}")
|
|
return render(request, 'resulation/resulation.html', {'error': str(e)})
|
|
|
|
else:
|
|
# Render the form template
|
|
return render(request, 'resulation/resulation.html')
|
|
|
|
def Viewresulation(request):
|
|
# Get all records from database
|
|
pdf_records = resulation.objects.all().order_by('-m_date') # Latest first
|
|
return render(request, 'resulation/viewresulation.html', {'pdf_records': pdf_records})
|
|
|
|
|
|
def file_download(request, type, filename):
|
|
# Construct the full path to the file
|
|
if type== 'attendance':
|
|
file_path = os.path.join(settings.MEDIA_ROOT, 'attendance_files', filename)
|
|
if type=='resolution':
|
|
file_path = os.path.join(settings.MEDIA_ROOT, 'resolution_files', filename)
|
|
|
|
|
|
# Use sendfile to stream the file directly
|
|
return FileResponse(open(file_path, 'rb'), content_type='application/pdf')
|
|
|
|
|
|
def Contract(request):
|
|
return render(request,'resulation/contract.html')
|
|
|
|
def Mou(request):
|
|
return render(request,'resulation/mou.html') |