Skip to content

Commit

Permalink
create OP Training from admin console
Browse files Browse the repository at this point in the history
  • Loading branch information
superryeti committed Mar 28, 2023
1 parent 342f4a3 commit 034f961
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 6 deletions.
29 changes: 23 additions & 6 deletions physionet-django/console/templates/console/console_navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,31 @@
</li>
{% endif %}

<!-- Training -->
{% if perms.user.can_review_training %}
<!-- training -->
<li class="nav-item {% if training_nav %}active{% endif %}" data-toggle="tooltip" data-placement="right">
<a id="nav_storage_requests" class="nav-link" href="{% url 'training_list' 'review' %}">
<i class="fa fa-fw fa-school"></i>
<span class="nav-link-text">Training check</span>
<li class="nav-item" data-toggle="tooltip" data-placement="right">
{% if training_type_nav or training_nav %}
<a id="nav_pages_dropdown" class="nav-link nav-link-collapse drop" data-toggle="collapse" href="#trainingComponent" data-parent="#sideAccordion" aria-expanded="true">
{% else %}
<a id="nav_pages_dropdown" class="nav-link nav-link-collapse drop collapsed" data-toggle="collapse" href="#trainingComponent" data-parent="#sideAccordion" aria-expanded="false">
{% endif %}
<i class="fa fa-fw fa-window-maximize"></i>
<span class="nav-link-text">Training</span>
</a>
</li>
<!-- submenu -->
{% if training_type_nav or training_nav %}
<ul class="sidenav-second-level collapse show" id="trainingComponent" style="">
{% else %}
<ul class="sidenav-second-level collapse" id="trainingComponent">
{% endif %}
<li class="nav-item {% if training_nav %}active{% endif %}">
<a id="nav_console_news" class="nav-link" href="{% url 'training_list' 'review' %}">Training Check</a>
</li>
<li class="nav-item {% if training_type_nav %}active{% endif %}">
<a id="nav_console_news" class="nav-link" href="{% url 'op_training' %}">OP Training</a>
</li>
</ul>
</li>
{% endif %}

<!-- events -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{% extends "console/base_console.html" %}

{% load static %}

{% block title %}Training Types{% endblock %}

{% block content %}
<div class="card mb-3">
<div class="card-header">
Training Types <span class="badge badge-pill badge-info">{{ training_types|length }}</span>
</div>
<div class="card-body">
<div><button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#op-training">Create/Update On Platform Training</button></div>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Valid Duration</th>
<th>On Platform Training</th>
</tr>
</thead>
<tbody>
{% for training in training_types %}
<tr>
<td>{{ training.name|title }}</td>
<td>{{ training.valid_duration }}</td>
<td>{% if training.required_field == 2 %} True {% else %} False {% endif %}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

<div class="modal fade" id="op-training" tabindex="-1">
<div class="modal-dialog">
<form action="{% url 'op_training' %}" method="POST" enctype="multipart/form-data" class="">
<div class="modal-content">
<div class="modal-body">
<p>Download Sample Json File by <a href="{% static 'sample/create.json' %}" download>Clicking Here</a></p>
<div>
{% csrf_token %}
<div class="form-group">
<label>Training Type: </label>
<select name="training_id" class="form-control" required>
<option disabled>Choose...</option>
<option value="-1">Create new training</option>
{% for training in training_types %}
<option value="{{ training.id }}">{{ training.name|title }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label>File: </label>
<input type="file" name="json_file" id="json_file" required="True" class="form-control">
</div>
{# <div class="form-group">#}
{# <input type="submit" name="create" class="btn btn-primary">#}
{# </div>#}

</div>
</div>
<div class="modal-footer">
<input type="submit" name="create" class="btn btn-primary">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>

</div>
</div>
{% endblock %}
4 changes: 4 additions & 0 deletions physionet-django/training/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.urls import path
from training import views


urlpatterns = [
path('training/', views.on_platform_training, name='op_training'),
]
56 changes: 56 additions & 0 deletions physionet-django/training/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json
import operator
from itertools import chain

from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.db import transaction
from django.db.models import Prefetch
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from django.utils.crypto import get_random_string

from rest_framework.parsers import JSONParser

from user.models import Training, TrainingType, TrainingQuestion, RequiredField
from user.enums import TrainingStatus

from training.models import OnPlatformTraining, Quiz, QuizChoice, ContentBlock
from training.models import OnPlatformTrainingProgress, ModuleProgress, CompletedContent, CompletedQuiz
from training.serializers import TrainingTypeSerializer


@permission_required('physionet.change_onplatformtraining', raise_exception=True)
def on_platform_training(request):

if request.POST:

if request.POST.get('training_id') != "-1":
training_type = get_object_or_404(TrainingType, pk=request.POST.get('training_id'))
else:
training_type = None

json_file = request.FILES.get("json_file", "")

if not json_file.name.endswith('.json'):
messages.error(request, 'File is not of JSON type')
return redirect("create_training")
file_data = JSONParser().parse(json_file.file)
serializer = TrainingTypeSerializer(training_type, data=file_data, partial=True)
if serializer.is_valid(raise_exception=False):
serializer.save()
messages.success(request, 'On platform training created successfully.')
else:
messages.error(request, serializer.errors)

return redirect("op_training")

training_types = TrainingType.objects.filter(required_field=RequiredField.PLATFORM)
return render(
request,
'console/training_type/index.html',
{
'training_types': training_types,
'training_type_nav': True,
})

0 comments on commit 034f961

Please sign in to comment.