def protocol_view(request, protocol, template_name="protocols/protocol.html"): try: protocol_object = get_object_or_404(Protocol, id=protocol) except MultipleObjectsReturned: protocol_object = Protocol.objects.filter(id=protocol).latest('pk') tri = 0 max_dose = 0 first_dose_time = None last_dose_time = None doses = ProtocolDose.objects.filter(protocol=protocol) for dose in doses: tri += dose.dose if max_dose < dose.dose: max_dose = dose.dose if first_dose_time is None or first_dose_time > dose.time: first_dose_time = dose.time if last_dose_time is None or last_dose_time < dose.time: last_dose_time = dose.time pd = { "protocol": protocol_object, "doses": doses, "total_radiation_intake": tri, "first_dose_time": first_dose_time, "last_dose_time": last_dose_time, "max_dose": max_dose, "time_step": settings.PROTOCOL_TIME_STEP, } return render_with_context(request, template_name, {'pd': pd})
def protocol_edit(request, protocol, template_name="protocols/edit.html"): try: protocol_object = get_object_or_404(Protocol, id=protocol) except MultipleObjectsReturned: protocol_object = Protocol.objects.filter(id=protocol).latest('pk') if not request.user.has_perm("protocols.change_protocol", protocol_object): return error_401_view(request) if request.method == 'POST': doses = request.POST.getlist("dose[]") times = request.POST.getlist("time[]") name = request.POST.get("name") # TODO protocol validation protocol_object.name = name protocol_object.save() ProtocolDose.objects.filter(protocol=protocol).delete() for i in range(0, len(times)): ProtocolDose(protocol=protocol_object, time=times[i], dose=doses[i]).save() return redirect('protocol_view', protocol) else: doses = ProtocolDose.objects.filter(protocol=protocol) doses_table = [] for dose in doses: doses_table.append({"time": dose.time, "dose": dose.dose}) pd = { "protocol": protocol_object, "name": protocol_object.name, "doses": doses_table, "time_step": settings.PROTOCOL_TIME_STEP, } return render_with_context(request, template_name, {'pd': pd})
def dashboard(request, template_name="protocols/dashboard.html"): protocols = get_objects_for_user( request.user, "protocols.view_protocol" ).union(Protocol.objects.filter(author=request.user)) protocol_data = [] for protocol in protocols: tri = 0 max_dose = 0 first_dose_time = None last_dose_time = None doses = ProtocolDose.objects.filter(protocol=protocol) for dose in doses: tri += dose.dose if max_dose < dose.dose: max_dose = dose.dose if first_dose_time is None or first_dose_time > dose.time: first_dose_time = dose.time if last_dose_time is None or last_dose_time < dose.time: last_dose_time = dose.time pd = { "protocol": protocol, "doses": doses, "total_radiation_intake": tri, "first_dose_time": first_dose_time, "last_dose_time": last_dose_time, "max_dose": max_dose, "time_step": settings.PROTOCOL_TIME_STEP, } protocol_data.append(pd) return render_with_context(request, template_name, {"protocol_data": protocol_data})
def dashboard(request, template_name="simulations/dashboard.html"): simulations = get_objects_for_user( request.user, "simulations.view_simulation").union( Simulation.objects.filter(author=request.user)) simulation_data = [] for simulation in simulations: tri = 0 max_dose = 0 first_dose_time = None last_dose_time = None doses = ProtocolDose.objects.filter(protocol=simulation.protocol) for dose in doses: tri += dose.dose if max_dose < dose.dose: max_dose = dose.dose if first_dose_time is None or first_dose_time > dose.time: first_dose_time = dose.time if last_dose_time is None or last_dose_time < dose.time: last_dose_time = dose.time pd = { "protocol": simulation.protocol, "doses": doses, "total_radiation_intake": tri, "first_dose_time": first_dose_time, "last_dose_time": last_dose_time, "max_dose": max_dose, "time_step": settings.PROTOCOL_TIME_STEP, } first_state = SimulationState.objects.filter( simulation=simulation).order_by('time').first() images1 = [] images2 = [] images3 = [] if first_state is not None: images1.append(first_state.W_img) images1.append(first_state.CHO_img) images1.append(first_state.OX_img) images1.append(first_state.GI_img) images1.append(first_state.timeInRepair_img) images2.append(first_state.irradiation_img) images2.append(first_state.cellState_img) images2.append(first_state.cellCycle_img) images2.append(first_state.proliferationTime_img) images2.append(first_state.cycleChanged_img) images3.append(first_state.G1time_img) images3.append(first_state.Stime_img) images3.append(first_state.G2time_img) images3.append(first_state.Mtime_img) images3.append(first_state.Dtime_img) sd = { "simulation": simulation, "images1": images1, "images2": images2, "images3": images3, "pd": pd } simulation_data.append(sd) return render_with_context(request, template_name, {'simulation_data': simulation_data})
def sserver_dashboard(request, template_name="simulations/sserver_dashboard.html"): sservers = get_objects_for_user(request.user, "simulations.view_simulationserver") refresh_time = datetime.now() for sserver in sservers: SimulationServer.refresh_status(sserver) return render_with_context(request, template_name, {"sservers": sservers})
def simulation_view(request, simulation, template_name="simulations/simulation.html"): try: simulation_object = get_object_or_404(Simulation, id=simulation) except MultipleObjectsReturned: simulation_object = Simulation.objects.filter( id=simulation).latest('pk') states = SimulationState.objects.filter( simulation=simulation_object).order_by('time') sd = {'simulation': simulation_object, 'states': states} return render_with_context(request, template_name, {'sd': sd})
def simulation_create(request, template_name="simulations/create.html"): if not request.user.has_perm("protocols.create_simulation"): return error_401_view(request) if request.method == 'POST': protocol_id = request.POST.get("protocol_id") try: protocol = get_object_or_404(Protocol, id=protocol_id) except MultipleObjectsReturned: protocol = Protocol.objects.filter(id=protocol_id).latest('pk') name = request.POST.get("name") time_duration = request.POST.get("time_duration") description = request.POST.get("description") automaton_file = request.FILES.get("automaton_file") automaton_saved = os.path.join(settings.MEDIA_ROOT, media_file_path(None, "automaton.json")) with open(automaton_saved, 'wb+') as destination: for chunk in automaton_file.chunks(): destination.write(chunk) Simulation.create_and_run(name, request.user, protocol, description, int(time_duration), automaton_saved) return redirect('simulation_dashboard') else: protocols = get_objects_for_user( request.user, "protocols.view_protocol").union( Protocol.objects.filter(author=request.user)) protocol_data = [] for protocol in protocols: tri = 0 max_dose = 0 first_dose_time = None last_dose_time = None doses = ProtocolDose.objects.filter(protocol=protocol) for dose in doses: tri += dose.dose if max_dose < dose.dose: max_dose = dose.dose if first_dose_time is None or first_dose_time > dose.time: first_dose_time = dose.time if last_dose_time is None or last_dose_time < dose.time: last_dose_time = dose.time pd = { "protocol": protocol, "doses": doses, "total_radiation_intake": tri, "first_dose_time": first_dose_time, "last_dose_time": last_dose_time, "max_dose": max_dose, "time_step": settings.PROTOCOL_TIME_STEP, } protocol_data.append(pd) form_data = {"protocol_data": protocol_data} return render_with_context(request, template_name, form_data)
def protocol_create(request, template_name="protocols/create.html"): if not request.user.has_perm("protocols.create_protocol"): return error_401_view(request) if request.method == 'POST': doses = request.POST.getlist("dose[]") times = request.POST.getlist("time[]") name = request.POST.get("name") # TODO protocol validation protocol_object = Protocol(name=name, author=request.user) protocol_object.save() for i in range(0, len(times)): ProtocolDose(protocol=protocol_object, time=times[i], dose=doses[i]).save() return redirect('protocol_view', protocol_object.id) else: pd = { "name": '', "doses": [], "time_step": settings.PROTOCOL_TIME_STEP, } return render_with_context(request, template_name, {'pd': pd})
def register(request, template_name="profiles/register.html"): context = { 'recaptcha_key': settings.GOOGLE_RECAPTCHA_PUBLIC_KEY, } if request.method == 'POST': context['full_name'] = request.POST.get('full_name') context['username'] = request.POST.get('username') context['email'] = request.POST.get('email') context['password'] = request.POST.get('password') context['accept_terms'] = (request.POST.get('accept_terms') == 'on') if not context['accept_terms']: context['register_status'] = 'invalid_terms' elif not request.recaptcha_is_valid: context['register_status'] = 'invalid_recaptcha' elif User.objects.filter(username=context['username']).count() != 0: context['register_status'] = 'username_taken' elif User.objects.filter(email=context['email']).count() != 0: context['register_status'] = 'email_taken' elif not is_valid_full_name(context['full_name']): context['register_status'] = 'invalid_full_name' elif not is_valid_username(context['username']): context['register_status'] = 'invalid_username' elif not is_valid_email(context['email']): context['register_status'] = 'invalid_email' elif not is_valid_password(context['password']): context['register_status'] = 'invalid_password' else: with transaction.atomic(): user = User() user.username = context['username'] user.set_password(context['password']) user.save() group, created = Group.objects.get_or_create(name='DOCTOR_GROUP') user.groups.add(group) avatar_filename = user.username + "_avatar.svg" avatar_media_path = media_file_path(user, avatar_filename) avatar_system_path = os.path.join(settings.MEDIA_ROOT, avatar_media_path) if not os.path.exists(os.path.dirname(avatar_system_path)): os.makedirs(os.path.dirname(avatar_system_path)) avatar_generator_url = \ settings.AVATAR_PROVIDER + '{0}?theme=berrypie&numcolors=4&size=220&fmt=svg' \ .format(avatar_filename) urllib.request.urlretrieve(avatar_generator_url, avatar_system_path) profile = Profile() profile.full_name = context['full_name'] profile.user = user profile.email = context['email'] profile.avatar = avatar_media_path profile.save() context['register_status'] = 'registered' login(request, user, backend='django.contrib.auth.backends.ModelBackend') return HttpResponseRedirect(reverse('home')) else: context['register_status'] = 'registering' return render_with_context(request, template_name, context)
def profile_view(request, username, template_name="profiles/profile.html"): try: profile = get_object_or_404(Profile, user__username=username) except MultipleObjectsReturned: profile = Profile.objects.filter(user__username=username).latest('pk') return render_with_context(request, template_name, {'profile': profile})
def profile_edit(request, template_name="profiles/edit.html"): return render_with_context(request, template_name, {})