2022-07-20 13:55:27 -04:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib import staticfiles
|
2023-02-02 15:08:48 -05:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2022-07-20 13:55:27 -04:00
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
|
2023-02-02 15:08:48 -05:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2022-02-15 21:43:19 -05:00
|
|
|
from django.views.generic import ListView
|
2021-03-23 17:08:00 -04:00
|
|
|
|
2022-07-20 13:55:27 -04:00
|
|
|
import requests
|
|
|
|
import weasyprint
|
|
|
|
|
2022-02-15 17:12:06 -05:00
|
|
|
from membershipworks.models import Member
|
2023-02-02 15:08:48 -05:00
|
|
|
from .models import Certification, Department
|
2022-02-15 17:12:06 -05:00
|
|
|
|
2022-07-20 13:55:27 -04:00
|
|
|
WIKI_URL = settings.WIKI_URL
|
|
|
|
|
2022-02-15 17:12:06 -05:00
|
|
|
|
2022-02-15 21:43:19 -05:00
|
|
|
class MemberCertificationListView(ListView):
|
|
|
|
template_name = "paperwork/member_certifications.dj.html"
|
|
|
|
context_object_name = "certifications"
|
|
|
|
|
2022-11-07 13:49:39 -05:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["show_outdated"] = (
|
|
|
|
self.request.GET.get("show_outdated", "false").lower() == "true"
|
|
|
|
)
|
|
|
|
return context
|
|
|
|
|
2022-02-15 21:43:19 -05:00
|
|
|
def get_queryset(self):
|
|
|
|
self.member = get_object_or_404(Member, uid=self.kwargs["uid"])
|
|
|
|
return Certification.objects.filter(member=self.member)
|
2022-07-20 13:55:27 -04:00
|
|
|
|
|
|
|
|
2023-02-02 15:08:48 -05:00
|
|
|
@login_required
|
|
|
|
def department_certifications(request):
|
|
|
|
departments = Department.objects.prefetch_related("shop_lead_flag__members")
|
|
|
|
if request.user.is_superuser:
|
|
|
|
departments = departments.all()
|
|
|
|
elif hasattr(request.user, "ldap_user"):
|
|
|
|
user_member = Member.objects.get(
|
|
|
|
uid=request.user.ldap_user.attrs["employeeNumber"][0]
|
|
|
|
)
|
|
|
|
# TODO: could be a lot simpler if membershipworks was in the same database
|
|
|
|
# TODO: should also select children
|
|
|
|
member_flags = list(user_member.flags.all().values_list("pk", flat=True))
|
|
|
|
departments = departments.filter(shop_lead_flag__in=member_flags)
|
|
|
|
else:
|
|
|
|
departments = []
|
|
|
|
|
|
|
|
certifications = Certification.objects.filter(
|
|
|
|
certification_version__definition__department__in=departments
|
|
|
|
).prefetch_related(
|
|
|
|
"certification_version__definition__department",
|
|
|
|
"member",
|
|
|
|
)
|
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"paperwork/department_certifications.dj.html",
|
|
|
|
{"departments": departments, "certifications": certifications},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-20 13:55:27 -04:00
|
|
|
def certification_pdf(request, cert_name):
|
|
|
|
wiki_page = f"{cert_name.replace('_', ' ')} Certification"
|
|
|
|
|
|
|
|
r = requests.get(
|
|
|
|
WIKI_URL + "/api.php",
|
|
|
|
params={
|
|
|
|
"action": "askargs",
|
|
|
|
"conditions": wiki_page,
|
|
|
|
"printouts": "Version|Approval Date|Approval status",
|
|
|
|
"format": "json",
|
|
|
|
"api_version": "2",
|
|
|
|
"origin": "*",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
results = r.json()["query"]["results"]
|
|
|
|
if wiki_page not in results:
|
|
|
|
return HttpResponseNotFound(
|
|
|
|
f'No such certification found on wiki: <a href="{WIKI_URL}/wiki/{wiki_page}">{wiki_page}</a>'
|
|
|
|
)
|
|
|
|
|
|
|
|
printouts = results[wiki_page]["printouts"]
|
|
|
|
if printouts["Approval status"] != ["approve"]:
|
|
|
|
return HttpResponseBadRequest(
|
|
|
|
f'Certification is not yet approved on wiki: <a href="{WIKI_URL}/wiki/{wiki_page}">{wiki_page}</a>'
|
|
|
|
)
|
|
|
|
|
|
|
|
filename = (
|
|
|
|
f'{wiki_page}_v{printouts["Version"][0]} - {printouts["Approval Date"][0]}.pdf'
|
|
|
|
)
|
|
|
|
|
|
|
|
html = weasyprint.HTML(f"{WIKI_URL}/index.php?title={wiki_page}")
|
|
|
|
|
|
|
|
stylesheet = staticfiles.finders.find("paperwork/certification-print.css")
|
|
|
|
pdf = html.write_pdf(stylesheets=[stylesheet])
|
|
|
|
return HttpResponse(
|
|
|
|
pdf,
|
|
|
|
headers={
|
|
|
|
"Content-Type": "application/pdf",
|
|
|
|
"Content-Disposition": f'inline; filename="{filename}"',
|
|
|
|
},
|
|
|
|
)
|