Skip to content
Snippets Groups Projects
Commit 79055a79 authored by James Graham's avatar James Graham
Browse files

Add institution model - #30

parent 2e21e593
No related branches found
No related tags found
No related merge requests found
...@@ -5,3 +5,8 @@ from . import models ...@@ -5,3 +5,8 @@ from . import models
admin.site.register(models.User, UserAdmin) admin.site.register(models.User, UserAdmin)
@admin.register(models.Institution)
class InstitutionAdmin(admin.ModelAdmin):
pass
# Generated by Django 2.0.8 on 2018-11-14 14:43
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('profiles', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Institution',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=63)),
('type', models.CharField(choices=[('aca', 'Academic'), ('bus', 'Business'), ('cha', 'Charity'), ('gov', 'Government'), ('oth', 'Other')], max_length=3)),
],
),
migrations.AddField(
model_name='user',
name='is_auditor',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='user',
name='is_superauditor',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='user',
name='institution',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='users', to='profiles.Institution'),
),
]
import enum
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse from django.urls import reverse
from core.models import MAX_LENGTH_NAME
class Institution(models.Model):
"""
Model representing the institution with which a user is associated.
"""
@enum.unique
class InstitutionType(enum.Enum):
ACADEMIC = 'aca'
BUSINESS = 'bus'
CHARITY = 'cha'
GOVERNMENT = 'gov'
OTHER = 'oth'
@classmethod
def choices(cls):
return tuple((i.value, i.name.title()) for i in cls)
#: Name of the institution
name = models.CharField(max_length=MAX_LENGTH_NAME,
blank=False, null=False)
#: Type of institution - e.g. academic, business, etc.
type = models.CharField(max_length=3,
choices=InstitutionType.choices(),
blank=False, null=False)
@property
def auditors(self):
return self.users.filter(is_auditor=True) + User.objects.filter(is_superauditor=True)
def __str__(self):
return self.name
class User(AbstractUser): class User(AbstractUser):
""" """
Custom Django user model to allow for additional functionality to be Model representing a user of PEDASI.
added more easily in the future.
""" """
#: Institution to which this user belongs
institution = models.ForeignKey(Institution, related_name='users',
on_delete=models.SET_NULL,
blank=True, null=True)
#: Is this user an auditor of their institution?
is_auditor = models.BooleanField(default=False,
blank=False, null=False)
#: Is this user a superauditor? Superauditors are auditors across all of PEDASI
is_superauditor = models.BooleanField(default=False,
blank=False, null=False)
def get_uri(self): def get_uri(self):
""" """
Get a URI for this user. Get a URI for this user.
......
{% extends "base.html" %}
{% load bootstrap4 %}
{% block content %}
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item" aria-current="page">
<a href="{% url 'index' %}">Home</a>
</li>
<li class="breadcrumb-item" aria-current="page">
Institutions
</li>
<li class="breadcrumb-item active" aria-current="page">
{{ institution.name }}
</li>
</ol>
</nav>
<h2>{{ institution.name }}</h2>
{% endblock %}
\ No newline at end of file
...@@ -15,4 +15,24 @@ ...@@ -15,4 +15,24 @@
<h2>My Profile</h2> <h2>My Profile</h2>
<table class="table">
<thead>
<th scope="col" class="col-md-2 border-0"></th>
<th scope="col" class="border-0"></th>
</thead>
<tbody>
{% if request.user.institution %}
<tr>
<td>Institution</td>
<td>
{{ request.user.institution.name }}
<a href="{% url 'profiles:institution.detail' pk=request.user.institution_id %}"
class="btn btn-info">Detail</a>
</td>
</tr>
{% endif %}
</tbody>
</table>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import include, path from django.urls import include, path
from .views.views import UserInactiveView, UserProfileView, UserUriView from . import views
app_name = 'profiles' app_name = 'profiles'
...@@ -8,14 +8,18 @@ urlpatterns = [ ...@@ -8,14 +8,18 @@ urlpatterns = [
path('', include('django.contrib.auth.urls')), path('', include('django.contrib.auth.urls')),
path('profile', path('profile',
UserProfileView.as_view(), views.user.UserProfileView.as_view(),
name='profile'), name='profile'),
path('inactive', path('inactive',
UserInactiveView.as_view(), views.user.UserInactiveView.as_view(),
name='inactive'), name='inactive'),
path('uri/<int:pk>', path('uri/<int:pk>',
UserUriView.as_view(), views.user.UserUriView.as_view(),
name='uri'), name='uri'),
path('institutions/<int:pk>',
views.institution.InstitutionDetailView.as_view(),
name='institution.detail'),
] ]
from .views import IndexView from django.views.generic import TemplateView
from applications.models import Application
from datasources.models import DataSource
from . import institution, user
__all__ = ['IndexView', 'institution', 'user']
class IndexView(TemplateView):
template_name = 'index.html'
def get_context_data(self, **kwargs):
"""
Add recent Applications and DataSources to index page.
:return: Django context dictionary
"""
context = super().get_context_data(**kwargs)
context['datasources'] = DataSource.objects.order_by('-id')[:3]
context['applications'] = Application.objects.order_by('-id')[:3]
return context
from django.views.generic.detail import DetailView
from profiles import models
class InstitutionDetailView(DetailView):
model = models.Institution
template_name = 'profiles/institution/detail.html'
context_object_name = 'institution'
...@@ -3,26 +3,6 @@ from django.http import JsonResponse ...@@ -3,26 +3,6 @@ from django.http import JsonResponse
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from applications.models import Application
from datasources.models import DataSource
class IndexView(TemplateView):
template_name = 'index.html'
def get_context_data(self, **kwargs):
"""
Add recent Applications and DataSources to index page.
:return: Django context dictionary
"""
context = super().get_context_data(**kwargs)
context['datasources'] = DataSource.objects.order_by('-id')[:3]
context['applications'] = Application.objects.order_by('-id')[:3]
return context
class UserProfileView(DetailView): class UserProfileView(DetailView):
template_name = 'profiles/user/profile.html' template_name = 'profiles/user/profile.html'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment