drugs/core/views/drugs.py

84 lines
2.5 KiB
Python
Raw Normal View History

2024-01-01 18:21:27 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
2024-01-15 15:29:33 +00:00
from django.urls import reverse
2024-01-01 18:21:27 +00:00
from rest_framework.views import APIView
2024-01-15 15:29:33 +00:00
from core.clients.sources.psychwiki import PsychWikiClient
2024-01-01 18:21:27 +00:00
from core.forms import DrugForm
from core.models import Drug
2024-01-15 15:29:33 +00:00
from core.views.helpers import synchronize_async_helper
2024-01-01 18:21:27 +00:00
from mxs.restrictions import StaffMemberRequiredMixin
from mxs.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
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"
2024-01-15 15:29:33 +00:00
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
2024-01-01 18:21:27 +00:00
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 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
2024-01-15 15:29:33 +00:00
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