Commit 8a9a298d authored by Thomas Frion's avatar Thomas Frion

[FEAT] Add Gendarmerie units in database

* Model
* Serializer
* Admin
* View
parent edad46f4
......@@ -16,20 +16,20 @@ class UnitAdmin(admin.OSMGeoAdmin): # We use the OpenStreet Map Admin model
# Fields to which filters can be applied
list_filter = ('dept',) # Keep the comma, ovewise there will be an error
# Fields used to order the list
ordering = ('code', 'nom', 'dept')
ordering = ('code', 'name', 'dept')
# Column of the list
list_display = ('code', 'nom', 'email', 'dept')
list_display = ('code', 'name', 'email', 'dept')
# Fields used by the search bar
search_fields = ('code', 'nom', 'email', 'dept')
search_fields = ('code', 'name', 'email', 'dept')
# We create groups to structure the forms
fieldsets = (
('Unité', {'fields': ('code', 'nom', 'email')}),
('Unité', {'fields': ('code', 'name', 'email')}),
('Localisation', {'fields': ('dept','geom')}),
)
# We create groups and we select the fields for the Add form
add_fieldsets = (
('Unité', {'fields': ('code', 'nom', 'email')}),
('Unité', {'fields': ('code', 'name', 'email')}),
('Localisation', {'fields': ('dept','geom')})
)
......@@ -50,3 +50,41 @@ class UnitAdmin(admin.OSMGeoAdmin): # We use the OpenStreet Map Admin model
defaults['form'] = self.add_form
defaults.update(kwargs)
return super().get_form(request, obj, **defaults)
@admin.register(GendUnit)
class GendUnitAdmin(admin.ModelAdmin):
# Fields used to order the list
ordering = ('code', 'name', 'email')
# Column of the list
list_display = ('code', 'name', 'email', 'addr')
# Fields used by the search bar
search_fields = ('code', 'name', 'email', 'addr')
# We create groups to structure the forms
fieldsets = (
('Unité', {'fields': ('code', 'name', 'email')}),
('Localisation', {'fields': ('addr','geom')}),
)
# We create groups and we select the fields for the Add form
add_fieldsets = (
('Unité', {'fields': ('code', 'name', 'email')}),
('Localisation', {'fields': ('addr','geom')})
)
form = GendUnitForm
# We select the add form
add_form = GendUnitForm
# We get the defined structure
def get_fieldsets(self, request, obj=None):
if not obj:
return self.add_fieldsets
return super().get_fieldsets(request, obj)
# We get the creation form
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults['form'] = self.add_form
defaults.update(kwargs)
return super().get_form(request, obj, **defaults)
\ No newline at end of file
......@@ -9,7 +9,7 @@ class UnitCreationForm(forms.ModelForm):
max_length=16,
)
nom = forms.CharField(
name = forms.CharField(
label="Nom",
max_length=64,
)
......@@ -46,7 +46,7 @@ class UnitForm(forms.ModelForm):
max_length=16,
)
nom = forms.CharField(
name = forms.CharField(
label="Nom",
max_length=64,
)
......@@ -72,3 +72,74 @@ class UnitForm(forms.ModelForm):
model = Unit
exclude = ()
# This form was created to put label.
class GendUnitCreationForm(forms.ModelForm):
code = forms.CharField(
label="Code",
max_length=16,
)
name = forms.CharField(
label="Nom",
max_length=128,
)
email = forms.EmailField(
label="E-mail",
max_length=128,
)
addr = forms.CharField(
label="Adresse",
required=False,
widget=forms.Textarea
)
geom = forms.PointField(
label="Location",
widget=forms.OSMWidget(
attrs={
'default_lon': 2.299281,
'default_lat': 47.011913,
'default_zoom': 5.4,
}
)
)
class Meta:
model = GendUnit
exclude = ()
# This form was created to put label.
class GendUnitForm(forms.ModelForm):
code = forms.CharField(
label="Code",
max_length=16,
)
nom = forms.CharField(
label="Nom",
max_length=128,
)
email = forms.EmailField(
label="E-mail",
max_length=128,
)
addr = forms.CharField(
label="Adresse",
required=False,
widget=forms.Textarea
)
geom = forms.PointField(
label="Location",
widget=forms.OSMWidget(
)
)
class Meta:
model = GendUnit
exclude = ()
\ No newline at end of file
# Generated by Django 3.0.4 on 2020-05-26 06:14
import django.contrib.gis.db.models.fields
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('unit', '0002_auto_20200518_1116'),
]
operations = [
migrations.CreateModel(
name='GendUnit',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(max_length=16, unique=True)),
('name', models.CharField(max_length=128)),
('email', models.EmailField(blank=True, max_length=128, null=True, unique=True)),
('geom', django.contrib.gis.db.models.fields.PointField(blank=True, null=True, srid=4326)),
('addr', models.TextField(blank=True, null=True)),
],
options={
'db_table': 'gend_unit',
'managed': True,
},
),
migrations.RenameField(
model_name='unit',
old_name='nom',
new_name='name',
),
migrations.AlterField(
model_name='settingunit',
name='name',
field=models.CharField(choices=[('Zoom', 'Zoom')], max_length=255),
),
]
......@@ -4,15 +4,13 @@ from django.contrib.gis.db import models
class Unit(models.Model):
code = models.CharField(max_length=16, unique = True)
nom = models.CharField(max_length=64)
name = models.CharField(max_length=64)
email = models.EmailField(max_length=64, unique = True)
geom = models.PointField(blank=True, null=True)
dept = models.CharField(max_length=3, blank=True, null=True)
#zoom = models.IntegerField(default=10)
def __str__(self):
return self.code + ' : ' + self.nom
return self.code + ' : ' + self.name
class Meta:
managed = True # Allows to write the table (in database)
......@@ -43,4 +41,18 @@ class SettingUnit(models.Model):
class Meta:
managed = True # Allows to write the table (in database)
db_table = 'pghm_unit_settings'
unique_together = (('name','unit'),)
\ No newline at end of file
unique_together = (('name','unit'),)
class GendUnit(models.Model):
code = models.CharField(max_length=16, unique = True)
name = models.CharField(max_length=128)
email = models.EmailField(max_length=128, unique = True, blank=True, null=True)
geom = models.PointField(blank=True, null=True)
addr = models.TextField(blank=True,null=True)
def __str__(self):
return self.code + ' : ' + self.name
class Meta:
managed = True # Allows to write the table (in database)
db_table = 'gend_unit'
\ No newline at end of file
......@@ -24,3 +24,7 @@ class UnitSerializer(serializers.ModelSerializer):
'email':{'validators': [not_empty]},
}
class GendUnitSerializer(serializers.ModelSerializer):
class Meta:
model = GendUnit
fields = '__all__'
\ No newline at end of file
......@@ -8,4 +8,7 @@ urlpatterns = [
path('units/<int:pk>', views.UnitDetail.as_view()),
path('units/<slug:code>', views.UnitDetailCode.as_view()),
path('units/settings/<slug:code>',views.SettingUnitDetail.as_view()),
path('units/gendarmerie', views.GendUnitList.as_view()),
path('units/gendarmerie/<int:pk>', views.GendUnitDetail.as_view()),
path('units/gendarmerie/<slug:code>', views.GendUnitDetailCode.as_view()),
]
\ No newline at end of file
......@@ -131,3 +131,106 @@ class SettingUnitDetail(APIView):
settings = self.get_object(code)
serializer = SettingUnitSerializer(settings, many = True)
return Response(serializer.data)
"""
=== GendUnit view: List
"""
class GendUnitList(APIView):
@swagger_auto_schema(
responses={200: GendUnitSerializer(many=True)},
)
def get(self, request, format=None):
units = GendUnit.objects.all()
serializer = GendUnitSerializer(units, many = True)
return Response(serializer.data)
@swagger_auto_schema(request_body=GendUnitSerializer,)
def post(self, request, format=None):
serializer = GendUnitSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
"""
=== GendUnit view: Detail
"""
class GendUnitDetail(APIView):
def get_object(self, pk):
try:
return GendUnit.objects.get(pk=pk)
except GendUnit.DoesNotExist:
raise Http404
@swagger_auto_schema(
responses={200: GendUnitSerializer},
)
def get(self, request, pk, format=None):
unit = self.get_object(pk)
serializer = GendUnitSerializer(unit)
return Response(serializer.data)
@swagger_auto_schema(request_body=GendUnitSerializer,)
def put(self, request, pk, format=None):
unit = self.get_object(pk)
serializer = GendUnitSerializer(unit, data = request.data)
if serializer.is_valid(): # If sent data are correct, we update the user
serializer.save()
# We return sent data, the HTTP code is 200
return Response(user_serializer.data)
# We return all catched errors whith the HTTP code 400
return Response(user_serializer.errors, status = 400)
@swagger_auto_schema(
responses={
status.HTTP_204_NO_CONTENT: openapi.Response(
description="This should not crash (response object with no schema)"
)
}
)
def delete(self, request, pk, format=None):
unit = self.get_object(pk)
# We return with HTTP code 204
return Response(status = 204)
"""
=== GendUnit view: Detail by Code
"""
class GendUnitDetailCode(UnitDetail):
def get_object(self, code):
try:
return GendUnit.objects.get(code=code)
except GendUnit.DoesNotExist:
raise Http404
@swagger_auto_schema(
responses={200: GendUnitSerializer},
)
def get(self, request, code, format=None):
unit = self.get_object(code)
serializer = GendUnitSerializer(unit)
return Response(serializer.data)
@swagger_auto_schema(request_body=GendUnitSerializer,)
def put(self, request, code, format=None):
unit = self.get_object(code)
serializer = GendUnitSerializer(unit, data = request.data)
if serializer.is_valid(): # If sent data are correct, we update the user
serializer.save()
# We return sent data, the HTTP code is 200
return Response(user_serializer.data)
# We return all catched errors whith the HTTP code 400
return Response(user_serializer.errors, status = 400)
@swagger_auto_schema(
responses={
status.HTTP_204_NO_CONTENT: openapi.Response(
description="This should not crash (response object with no schema)"
)
}
)
def delete(self, request, code, format=None):
unit = self.get_object(code)
# We return with HTTP code 204
return Response(status = 204)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment