Adding the core applications code to the repository
This commit is contained in:
0
utilisateurs/__init__.py
Normal file
0
utilisateurs/__init__.py
Normal file
BIN
utilisateurs/__pycache__/__init__.cpython-35.pyc
Normal file
BIN
utilisateurs/__pycache__/__init__.cpython-35.pyc
Normal file
Binary file not shown.
BIN
utilisateurs/__pycache__/admin.cpython-35.pyc
Normal file
BIN
utilisateurs/__pycache__/admin.cpython-35.pyc
Normal file
Binary file not shown.
BIN
utilisateurs/__pycache__/models.cpython-35.pyc
Normal file
BIN
utilisateurs/__pycache__/models.cpython-35.pyc
Normal file
Binary file not shown.
BIN
utilisateurs/__pycache__/tests.cpython-35.pyc
Normal file
BIN
utilisateurs/__pycache__/tests.cpython-35.pyc
Normal file
Binary file not shown.
20
utilisateurs/admin.py
Normal file
20
utilisateurs/admin.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import *
|
||||
# Register your models here.
|
||||
|
||||
admin.register(Organisme)
|
||||
|
||||
|
||||
@admin.register(Maraudeur)
|
||||
class MaraudeurAdmin(admin.ModelAdmin):
|
||||
fieldsets = [
|
||||
('Informations', {'fields': [('first_name', 'last_name')]}),
|
||||
]
|
||||
|
||||
list_display = ('first_name', 'last_name', 'is_superuser')
|
||||
|
||||
|
||||
@admin.register(ReferentMaraude)
|
||||
class ReferentMaraudeAdmin(admin.ModelAdmin):
|
||||
fields = ['maraudeur']
|
||||
5
utilisateurs/apps.py
Normal file
5
utilisateurs/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UtilisateursConfig(AppConfig):
|
||||
name = 'utilisateurs'
|
||||
79
utilisateurs/migrations/0001_initial.py
Normal file
79
utilisateurs/migrations/0001_initial.py
Normal file
@@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.7 on 2016-08-04 10:21
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.auth.models
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0007_alter_validators_add_error_messages'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Organisme',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('nom', models.CharField(max_length=64)),
|
||||
('email', models.EmailField(max_length=254, verbose_name='e-mail')),
|
||||
('adresse', models.CharField(max_length=128)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Organisme',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Professionnel',
|
||||
fields=[
|
||||
('user_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'users',
|
||||
'abstract': False,
|
||||
'verbose_name': 'user',
|
||||
},
|
||||
bases=('auth.user',),
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ReferentMaraude',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Référent de la maraude',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Maraudeur',
|
||||
fields=[
|
||||
('professionnel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='utilisateurs.Professionnel')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Maraudeur',
|
||||
},
|
||||
bases=('utilisateurs.professionnel',),
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='professionnel',
|
||||
name='organisme',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='professionnels', to='utilisateurs.Organisme'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='referentmaraude',
|
||||
name='maraudeur',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='utilisateurs.Maraudeur'),
|
||||
),
|
||||
]
|
||||
0
utilisateurs/migrations/__init__.py
Normal file
0
utilisateurs/migrations/__init__.py
Normal file
BIN
utilisateurs/migrations/__pycache__/0001_initial.cpython-35.pyc
Normal file
BIN
utilisateurs/migrations/__pycache__/0001_initial.cpython-35.pyc
Normal file
Binary file not shown.
BIN
utilisateurs/migrations/__pycache__/__init__.cpython-35.pyc
Normal file
BIN
utilisateurs/migrations/__pycache__/__init__.cpython-35.pyc
Normal file
Binary file not shown.
121
utilisateurs/models.py
Normal file
121
utilisateurs/models.py
Normal file
@@ -0,0 +1,121 @@
|
||||
import datetime
|
||||
|
||||
from django.db import models
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
# Create your models here.
|
||||
|
||||
class SingletonModel(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.__class__.objects.exclude(id=self.id).delete()
|
||||
super(SingletonModel, self).save(*args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def load(cls):
|
||||
try:
|
||||
return cls.objects.get()
|
||||
except cls.DoesNotExist:
|
||||
return cls()
|
||||
|
||||
|
||||
class Organisme(models.Model):
|
||||
""" Organisme : Association, Entreprise, Service public, ..."""
|
||||
|
||||
nom = models.CharField(max_length=64)
|
||||
email = models.EmailField("e-mail")
|
||||
adresse = models.CharField(max_length=128)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Organisme"
|
||||
|
||||
|
||||
class Professionnel(User):
|
||||
""" Professionnel d'un organisme """
|
||||
organisme = models.ForeignKey(
|
||||
Organisme,
|
||||
related_name="professionnels",
|
||||
blank=True, null=True # For now
|
||||
)
|
||||
|
||||
|
||||
class Maraudeur(Professionnel):
|
||||
""" Professionnels qui participent aux maraudes """
|
||||
|
||||
auto_fields = ['username', 'email', 'organisme']
|
||||
|
||||
# Donne accès aux vues des modules "maraudes" et "veille/suivis"
|
||||
|
||||
DEFAULT_ORGANISME = "ALSA"
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Maraudeur"
|
||||
|
||||
def _fill_fields(self):
|
||||
for field in self.auto_fields:
|
||||
filling_func = "fill_%s" % field
|
||||
try:
|
||||
val = getattr(self, filling_func)()
|
||||
except AttributeError:
|
||||
raise ValueError("'%s' is not defined on %s" % (filling_func, self))
|
||||
setattr(self, field, val)
|
||||
|
||||
def fill_email(self):
|
||||
return "%s@alsa68.org" % self.username
|
||||
|
||||
def fill_username(self):
|
||||
return "%s.%s" % (self.first_name[0].lower(), self.last_name.lower())
|
||||
|
||||
def fill_organisme(self):
|
||||
try:
|
||||
return Organisme.objects.get(nom=self.DEFAULT_ORGANISME)
|
||||
except Organisme.DoesNotExist:
|
||||
return None
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
create = False
|
||||
if not self.pk or not self.username or not self.email:
|
||||
create = True
|
||||
if create:
|
||||
self._fill_fields()
|
||||
self.is_staff = True
|
||||
|
||||
return super(Maraudeur, self).save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name[0])
|
||||
|
||||
|
||||
|
||||
class ReferentMaraude(SingletonModel):
|
||||
""" Référent de la maraude """
|
||||
maraudeur = models.ForeignKey(Maraudeur)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Référent de la maraude"
|
||||
|
||||
def __str__(self):
|
||||
return 'Referent: %s' % self.maraudeur
|
||||
|
||||
def set_unique_referent(self):
|
||||
""" Ensure 'is_referent' has only one 'True' value """
|
||||
for maraudeur in Maraudeur.objects.all():
|
||||
if maraudeur == self.maraudeur:
|
||||
maraudeur.is_superuser= True
|
||||
maraudeur.save()
|
||||
else:
|
||||
if maraudeur.is_superuser:
|
||||
maraudeur.is_superuser = False
|
||||
maraudeur.save()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# On s'assure que le référent (administrateur) est unique
|
||||
self.set_unique_referent()
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_referent(cls):
|
||||
instance = cls.load()
|
||||
return instance.maraudeur
|
||||
5
utilisateurs/tests.py
Normal file
5
utilisateurs/tests.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
|
||||
#TODO: Un seul objet Maraudeur peut avoir la propriété vraie 'is_referent'
|
||||
3
utilisateurs/views.py
Normal file
3
utilisateurs/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user