Skip to content

Commit

Permalink
dosportal modification, new models for users
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-dvorak committed Mar 25, 2024
1 parent b0139bf commit 3df59dc
Show file tree
Hide file tree
Showing 17 changed files with 501 additions and 52 deletions.
25 changes: 25 additions & 0 deletions DOSPORTAL/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,33 @@ class AirportsAdmin(ImportExportModelAdmin):
ordering = ['code_iata']


class OrganizationUserInline(admin.TabularInline):
model = OrganizationUser

class OrganizationAdmin(admin.ModelAdmin):
list_display = ('name', 'slug', 'data_policy', 'can_users_change_policy', 'created_at', 'updated_at', 'website', 'contact_email', 'user_count')
search_fields = ('name', 'slug', 'data_policy', 'website', 'contact_email', 'users')
list_filter = ('data_policy', 'can_users_change_policy')
inlines = [OrganizationUserInline]

def user_count(self, obj):
return obj.users.count()
user_count.short_description = 'User Count'

# def admin_count(self, obj):
# return obj.users.filter(role='admin').count()
# admin_count.short_description = 'Admin Count'

# def owner_count(self, obj):
# return obj.users.filter(role='owner').count()
# owner_count.short_description = 'Owner Count'



admin.site.register(DetectorManufacturer)
admin.site.register(measurement)
admin.site.register(Organization, OrganizationAdmin)
#admin.site.register(OrganizationUser)
admin.site.register(record)
admin.site.register(Detector)
admin.site.register(DetectorLogbook)
Expand Down
14 changes: 5 additions & 9 deletions DOSPORTAL/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RecordForm(forms.ModelForm):
'class': 'form-control',
}),
label="Log file",
help_text="Select a log file to upload."
#widget=forms.FileInput(attrs={
# 'class': 'form-control',
#})
Expand All @@ -30,7 +31,9 @@ class RecordForm(forms.ModelForm):
widget=forms.Select(attrs={
'class': 'form-control',
}),
required=False
required=False,
label="Detector",
help_text="Select used detector. It is not mandatory in case of detectors with auto-detect feature."
)

record_type = forms.ChoiceField(
Expand All @@ -39,15 +42,8 @@ class RecordForm(forms.ModelForm):
'class': 'form-control',
})
)

time_start = forms.DateTimeField(
widget=forms.DateTimeInput(attrs={
'type': "datetime-local",
'class': 'form-control datetimepicker-input',
})
)


class Meta:
model = record
exclude = ("time_end", "measurement", "log_filename", "metadata", "duration")
exclude = ("time_end", "measurement", "log_filename", "metadata", "duration", "time_start", "record_duration")
78 changes: 74 additions & 4 deletions DOSPORTAL/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from matplotlib.colors import LightSource
from martor.models import MartorField
from django_q.tasks import async_task
from django.utils.text import slugify

from .tasks import process_flight_entry

Expand All @@ -18,7 +19,6 @@ def get_enum_dsc(enum, t):


class UUIDMixin(models.Model):

id = models.UUIDField(
primary_key = True,
default = uuid.uuid4,
Expand All @@ -29,11 +29,63 @@ class UUIDMixin(models.Model):
def get_admin_url(self):
return reverse("admin:%s_%s_change" % (self._meta.app_label, self._meta.model_name), args=(self.id,))


class Meta:
abstract = True



class Organization(UUIDMixin):
DATA_POLICY_CHOICES = [
('PR', 'Private'),
('PU', 'Public'),
('NV', 'Non-public'),
]

name = models.CharField(max_length=200)
users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='organizations', through='OrganizationUser')
slug = models.SlugField(max_length=255, unique=True, blank=True)
data_policy = models.CharField(max_length=2, choices=DATA_POLICY_CHOICES, default='PR')
can_users_change_policy = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
website = models.URLField(max_length=200, null=True, blank=True)
contact_email = models.EmailField(max_length=200, null=True, blank=True)
description = models.TextField(null=True, blank=True)

def save(self, *args, **kwargs):
# Aktualizace slug pole na základě názvu, pokud není zadáno
if not self.slug:
self.slug = slugify(self.name)
super(Organization, self).save(*args, **kwargs)

def __str__(self):
return self.name

def get_members(self):
return ", ".join([str(user) for user in self.users.all()])

def get_admin_url(self):
return reverse("admin:%s_%s_change" % (self._meta.app_label, self._meta.model_name), args=(self.id,))



class OrganizationUser(models.Model):

USER_TYPE_CHOICES = [
('ME', 'Member'),
('AD', 'Admin'),
('OW', 'Owner'),
]

user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='organization_users')
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name='user_organizations')
user_type = models.CharField(max_length=2, choices=USER_TYPE_CHOICES, default='ME')

class Meta:
unique_together = ('user', 'organization')

def __str__(self):
return f'{self.user.username}: {self.get_user_type_display()} of {self.organization.name}'

class CARImodel(UUIDMixin):
data = models.JSONField()
Expand Down Expand Up @@ -154,14 +206,30 @@ class DetectorCalib(UUIDMixin):
date = models.DateTimeField(
)

coef0 = models.FloatField(
_("Coefficient 0 (offset)"),
default=0.0
)

coef1 = models.FloatField(
_("Coefficient 1, (linear)"),
default=1
)

coef2 = models.FloatField(
_("Coefficient 2, (quadratic)"),
default=0.0
)

# author = models.ForeignKey(
# settings.AUTH_USER_MODEL,
# on_delete=models.CASCADE,
# )


cabib = models.JSONField(
_("Slozky kalibrace, json")
_("Slozky kalibrace, json"),
null=True
)


Expand Down Expand Up @@ -384,14 +452,16 @@ def user_directory_path(instance, filename):

# Tohle pole by melo obsahovat nasledujici typy:
RECORD_TYPES = (
('U', 'Unknown'),
('S', 'Spectral measurements'),
('E', 'Event measurements'),
('L', 'Location')
)

record_type = models.CharField(
verbose_name=_("Certain record type, enum"),
choices=RECORD_TYPES,
default="S",
default="U",
help_text=_("Type of log file")
)

Expand Down
15 changes: 12 additions & 3 deletions DOSPORTAL/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']
ALLOWED_HOSTS = ['*','localhost', '127.0.0.1', '0.0.0.0']

CSRF_TRUSTED_ORIGINS = ['https://portal.dos.ust.cz']

Expand All @@ -41,17 +41,23 @@
'django.contrib.postgres',
'bootstrap5',
#'background_task',
'django_json_widget',
#'django_json_widget',
'rest_framework',
'corsheaders',
'jquery',
'import_export',
'django_select2',
'martor',
'django_tables2',
#'django_tables2',

'DOSPORTAL',
'django_q',
'crispy_forms',
'crispy_bootstrap5',
'django_gravatar',


'organizations',
]

MIDDLEWARE = [
Expand All @@ -66,6 +72,9 @@
'django.middleware.common.CommonMiddleware',
]

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = 'bootstrap5'

ROOT_URLCONF = 'DOSPORTAL.urls'

TEMPLATES = [
Expand Down
5 changes: 4 additions & 1 deletion DOSPORTAL/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<a class="nav-link" href="/measurements">Measurements</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/records">Records</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/analysis">Analysis</a>
</li>
<li class="nav-item">
Expand All @@ -99,7 +102,7 @@

{% if user.is_authenticated %}

<span class="mx-4"> Hi <b>{{ user.username }}</b>! </span>
<span class="mx-4"> Hi <b><a href="{% url 'profile' %}">{{ user.username }}</a></b>! </span>
<a href="{% url 'logout' %}" class="btn btn-outline-dark">Log Out</a>
{% else %}
<a href="{% url 'login' %}" class="btn btn-outline-dark">Log In</a>
Expand Down
11 changes: 4 additions & 7 deletions DOSPORTAL/templates/measurements/measurement_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<li><strong>Records:</strong>
<ul>
{% for record in measurement.records.all %}
<li><a href="/record/{{record.id}}/">{{record.description}} {{record.type}}</a> {%if record.detector %}(<a href="/detector/{{record.detector.id}}/" data-bs-toggle="tooltip" data-bs-placement="top" title="SN: {{record.detector.sn}}">{{record.detector.name}}</a>){%endif%}
<li><a href="/record/{{record.id}}/"> {{record}}</a> {%if record.detector %}(<a href="/detector/{{record.detector.id}}/" data-bs-toggle="tooltip" data-bs-placement="top" title="SN: {{record.detector.sn}}">{{record.detector.name}}</a>){%endif%}
{% if user.is_authenticated %}
<a href="{{record.get_admin_url}}"><i class="bi bi-pencil"></i></a>
{%endif%}</li>
Expand All @@ -64,10 +64,11 @@
</div>

<div class="card-body is-one-third collapse py-2" id="upload_data_collapse">
{% load crispy_forms_tags %}
<form method="post" action="./record/new/" enctype="multipart/form-data">
{% csrf_token %}
{{ record_form.as_p }}
<button class="button is-success is-fullwidth is-medium" type="submit"> Submit </button>
{{ record_form | crispy }}
<button class="btn btn-primary" type="submit"> Submit </button>

</form>
</div>
Expand All @@ -90,11 +91,7 @@ <h3> NOT INITIALIZED </h3>
<div class="card-body">
<div id="graph1"></div>
<div id="graph2"></div>


</div>


</div>


Expand Down
10 changes: 4 additions & 6 deletions DOSPORTAL/templates/measurements/measurement_new.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@


{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}New Measurement{% endblock %}

{% block content %}



<div class="column is-one-third">
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button class="button is-success is-fullwidth is-medium mt-5" type="submit"> Submit </button>

{{ form | crispy }}
<button class="btn btn-primary is-success is-fullwidth is-medium mt-5" type="submit"> Submit </button>
</form>
</div>


{% endblock %}
Loading

0 comments on commit 3df59dc

Please sign in to comment.