105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.shortcuts import render
|
|
from django.urls import reverse
|
|
from rest_framework.views import APIView
|
|
|
|
from core.clients.sources.psychwiki import PsychWikiClient
|
|
from core.forms import DrugForm
|
|
from core.models import Drug
|
|
from core.views.helpers import synchronize_async_helper
|
|
from mxs.restrictions import StaffMemberRequiredMixin
|
|
from mxs.views import ObjectCreate, ObjectDelete, ObjectList, ObjectRead, ObjectUpdate
|
|
|
|
# from mixins.views import ObjectRead
|
|
|
|
|
|
class DrugList(LoginRequiredMixin, StaffMemberRequiredMixin, ObjectList):
|
|
list_template = "partials/drug-list.html"
|
|
model = Drug
|
|
page_title = "Global list of drugs"
|
|
|
|
list_url_name = "drugs"
|
|
list_url_args = ["type"]
|
|
|
|
submit_url_name = "drug_create"
|
|
delete_all_url_name = "drug_clear"
|
|
|
|
def get_context_data(self):
|
|
context = super().get_context_data()
|
|
self.extra_buttons = [
|
|
{
|
|
"url": reverse("drug_pull_merge"),
|
|
"action": "refresh",
|
|
"method": "post",
|
|
"label": "Update database from sources",
|
|
"icon": "fa-solid fa-refresh",
|
|
},
|
|
]
|
|
return context
|
|
|
|
|
|
class DrugCreate(LoginRequiredMixin, StaffMemberRequiredMixin, ObjectCreate):
|
|
model = Drug
|
|
form_class = DrugForm
|
|
|
|
submit_url_name = "drug_create"
|
|
|
|
|
|
class DrugUpdate(LoginRequiredMixin, StaffMemberRequiredMixin, ObjectUpdate):
|
|
model = Drug
|
|
form_class = DrugForm
|
|
|
|
submit_url_name = "drug_update"
|
|
|
|
|
|
class DrugDelete(LoginRequiredMixin, StaffMemberRequiredMixin, ObjectDelete):
|
|
model = Drug
|
|
|
|
|
|
class DrugDetail(LoginRequiredMixin, StaffMemberRequiredMixin, ObjectRead):
|
|
model = Drug
|
|
form_class = DrugForm
|
|
detail_template = "partials/drug-detail.html"
|
|
|
|
detail_url_name = "drug_detail"
|
|
detail_url_args = ["type", "pk"]
|
|
|
|
def get_object(self, **kwargs):
|
|
print("GET")
|
|
pk = kwargs.get("pk")
|
|
info = Drug.objects.get(pk=pk)
|
|
|
|
# self.extra_context = {}
|
|
print("info", info)
|
|
# return dictionary
|
|
return info.__dict__
|
|
|
|
|
|
class DrugClear(LoginRequiredMixin, StaffMemberRequiredMixin, APIView):
|
|
def delete(self, request):
|
|
template_name = "mixins/partials/notify.html"
|
|
drugs_all = Drug.objects.all()
|
|
drugs_all.delete()
|
|
context = {
|
|
"message": "Deleted all drugs",
|
|
"class": "success",
|
|
}
|
|
response = render(request, template_name, context)
|
|
response["HX-Trigger"] = "drugEvent"
|
|
return response
|
|
|
|
|
|
class DrugPullMerge(LoginRequiredMixin, StaffMemberRequiredMixin, APIView):
|
|
def post(self, request):
|
|
template_name = "mixins/partials/notify.html"
|
|
# Do something
|
|
run = synchronize_async_helper(PsychWikiClient())
|
|
result = synchronize_async_helper(run.update_drugs())
|
|
context = {
|
|
"message": f"Drugs fetched: {result}",
|
|
"class": "success",
|
|
}
|
|
response = render(request, template_name, context)
|
|
response["HX-Trigger"] = "drugEvent"
|
|
return response
|