Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rex Dri
Rex Dri
Commits
810d42ed
Commit
810d42ed
authored
Apr 25, 2018
by
Florent Chehab
Browse files
Campus added
University updated, etc.
parent
54d5bdfb
Pipeline
#22055
passed with stage
in 1 minute and 49 seconds
Changes
14
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
rex/admin.py
View file @
810d42ed
...
...
@@ -10,4 +10,4 @@ admin.site.register(Region)
admin
.
site
.
register
(
City
)
admin
.
site
.
register
(
University
,
CompareVersionAdmin
)
admin
.
site
.
register
(
MainCampus
)
admin
.
site
.
register
(
MainCampus
,
CompareVersionAdmin
)
rex/migrations/0001_initial.py
View file @
810d42ed
# Generated by Django 2.0.3 on 2018-04-2
3 15:30
# Generated by Django 2.0.3 on 2018-04-2
5 09:47
from
django.conf
import
settings
import
django.core.validators
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
rex.utils.friendly_path
...
...
@@ -10,14 +12,48 @@ class Migration(migrations.Migration):
initial
=
True
dependencies
=
[
migrations
.
swappable_dependency
(
settings
.
AUTH_USER_MODEL
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'City'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'local_name'
,
models
.
CharField
(
max_length
=
200
,
null
=
True
)),
(
'area'
,
models
.
CharField
(
max_length
=
200
,
null
=
True
)),
],
),
migrations
.
CreateModel
(
name
=
'Country'
,
fields
=
[
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'iso_code'
,
models
.
CharField
(
max_length
=
2
,
primary_key
=
True
,
serialize
=
False
)),
(
'iso_alpha3_code'
,
models
.
CharField
(
max_length
=
3
,
primary_key
=
True
,
serialize
=
False
)),
],
),
migrations
.
CreateModel
(
name
=
'MainCampus'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'modified_date'
,
models
.
DateTimeField
(
auto_now
=
True
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
,
null
=
True
)),
(
'comment'
,
models
.
TextField
(
blank
=
True
,
null
=
True
)),
(
'lat'
,
models
.
DecimalField
(
decimal_places
=
6
,
max_digits
=
10
,
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
-
85.05112878
),
django
.
core
.
validators
.
MaxValueValidator
(
85.05112878
)])),
(
'lon'
,
models
.
DecimalField
(
decimal_places
=
6
,
max_digits
=
10
,
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
-
180
),
django
.
core
.
validators
.
MaxValueValidator
(
180
)])),
(
'city'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.City'
)),
(
'modified_by'
,
models
.
ForeignKey
(
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
SET_NULL
,
to
=
settings
.
AUTH_USER_MODEL
)),
],
options
=
{
'abstract'
:
False
,
},
),
migrations
.
CreateModel
(
name
=
'Region'
,
fields
=
[
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'un_code'
,
models
.
CharField
(
max_length
=
3
,
primary_key
=
True
,
serialize
=
False
)),
(
'parent'
,
models
.
ForeignKey
(
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
SET_NULL
,
to
=
'rex.Region'
)),
],
),
migrations
.
CreateModel
(
...
...
@@ -26,10 +62,18 @@ class Migration(migrations.Migration):
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'acronym'
,
models
.
CharField
(
max_length
=
20
)),
(
'localization_lat'
,
models
.
DecimalField
(
decimal_places
=
6
,
max_digits
=
10
,
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
-
85.05112878
),
django
.
core
.
validators
.
MaxValueValidator
(
85.05112878
)])),
(
'localization_lon'
,
models
.
DecimalField
(
decimal_places
=
6
,
max_digits
=
10
,
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
-
180
),
django
.
core
.
validators
.
MaxValueValidator
(
180
)])),
(
'logo'
,
models
.
ImageField
(
default
=
'path/to/my/default/image.jpg'
,
max_length
=
250
,
upload_to
=
rex
.
utils
.
friendly_path
.
friendly_path
(
'uploads/universities/logos/'
,
'name'
))),
(
'
country'
,
models
.
ForeignKey
(
on_delete
=
'CASCADE
'
,
to
=
'rex.
Country
'
)),
(
'
main_campus'
,
models
.
OneToOneField
(
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
related_name
=
'main_campus_of
'
,
to
=
'rex.
MainCampus
'
)),
],
),
migrations
.
AddField
(
model_name
=
'country'
,
name
=
'region'
,
field
=
models
.
ForeignKey
(
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.Region'
),
),
migrations
.
AddField
(
model_name
=
'city'
,
name
=
'country'
,
field
=
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.Country'
),
),
]
rex/migrations/0002_auto_20180424_2119.py
deleted
100644 → 0
View file @
54d5bdfb
# Generated by Django 2.0.3 on 2018-04-24 19:19
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'rex'
,
'0001_initial'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Region'
,
fields
=
[
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'un_code'
,
models
.
CharField
(
max_length
=
3
,
primary_key
=
True
,
serialize
=
False
)),
(
'parent'
,
models
.
ForeignKey
(
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
SET_NULL
,
to
=
'rex.Region'
)),
],
),
migrations
.
RemoveField
(
model_name
=
'country'
,
name
=
'iso_code'
,
),
migrations
.
AddField
(
model_name
=
'country'
,
name
=
'iso_alpha3_code'
,
field
=
models
.
CharField
(
default
=
'TMP'
,
max_length
=
3
,
primary_key
=
True
,
serialize
=
False
),
preserve_default
=
False
,
),
migrations
.
AddField
(
model_name
=
'country'
,
name
=
'region'
,
field
=
models
.
ForeignKey
(
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.Region'
),
),
]
rex/migrations/000
5
_auto_20180425_11
31
.py
→
rex/migrations/000
2
_auto_20180425_11
50
.py
View file @
810d42ed
# Generated by Django 2.0.3 on 2018-04-25 09:
31
# Generated by Django 2.0.3 on 2018-04-25 09:
50
from
django.db
import
migrations
,
models
...
...
@@ -6,13 +6,18 @@ from django.db import migrations, models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'rex'
,
'000
4_maincampus
'
),
(
'rex'
,
'000
1_initial
'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'maincampus'
,
name
=
'comment'
,
field
=
models
.
TextField
(
blank
=
True
,
null
=
True
),
model_name
=
'city'
,
name
=
'area'
,
field
=
models
.
CharField
(
blank
=
True
,
max_length
=
200
,
null
=
True
),
),
migrations
.
AlterField
(
model_name
=
'city'
,
name
=
'local_name'
,
field
=
models
.
CharField
(
blank
=
True
,
max_length
=
200
,
null
=
True
),
),
]
rex/migrations/000
6_maincampus_name
.py
→
rex/migrations/000
3_auto_20180425_1217
.py
View file @
810d42ed
# Generated by Django 2.0.3 on 2018-04-25
09:33
# Generated by Django 2.0.3 on 2018-04-25
10:17
from
django.db
import
migrations
,
models
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'rex'
,
'000
5
_auto_20180425_11
31
'
),
(
'rex'
,
'000
2
_auto_20180425_11
50
'
),
]
operations
=
[
migrations
.
Add
Field
(
migrations
.
Remove
Field
(
model_name
=
'maincampus'
,
name
=
'name'
,
field
=
models
.
CharField
(
max_length
=
200
,
null
=
True
),
name
=
'modified_by'
,
),
migrations
.
RemoveField
(
model_name
=
'maincampus'
,
name
=
'modified_date'
,
),
]
rex/migrations/0003_city.py
deleted
100644 → 0
View file @
54d5bdfb
# Generated by Django 2.0.3 on 2018-04-25 08:21
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'rex'
,
'0002_auto_20180424_2119'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'City'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
200
)),
(
'local_name'
,
models
.
CharField
(
max_length
=
200
,
null
=
True
)),
(
'area'
,
models
.
CharField
(
max_length
=
200
,
null
=
True
)),
(
'country'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.Country'
)),
],
),
]
rex/migrations/0004_auto_20180425_1335.py
0 → 100644
View file @
810d42ed
# Generated by Django 2.0.3 on 2018-04-25 11:35
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'rex'
,
'0003_auto_20180425_1217'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'country'
,
name
=
'region'
,
field
=
models
.
ForeignKey
(
blank
=
True
,
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.Region'
),
),
migrations
.
AlterField
(
model_name
=
'region'
,
name
=
'parent'
,
field
=
models
.
ForeignKey
(
blank
=
True
,
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
SET_NULL
,
to
=
'rex.Region'
),
),
]
rex/migrations/0004_maincampus.py
deleted
100644 → 0
View file @
54d5bdfb
# Generated by Django 2.0.3 on 2018-04-25 09:22
from
django.conf
import
settings
import
django.core.validators
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
migrations
.
swappable_dependency
(
settings
.
AUTH_USER_MODEL
),
(
'rex'
,
'0003_city'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'MainCampus'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'modified_date'
,
models
.
DateTimeField
(
auto_now
=
True
)),
(
'comment'
,
models
.
TextField
(
null
=
True
)),
(
'lat'
,
models
.
DecimalField
(
decimal_places
=
6
,
max_digits
=
10
,
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
-
85.05112878
),
django
.
core
.
validators
.
MaxValueValidator
(
85.05112878
)])),
(
'lon'
,
models
.
DecimalField
(
decimal_places
=
6
,
max_digits
=
10
,
validators
=
[
django
.
core
.
validators
.
MinValueValidator
(
-
180
),
django
.
core
.
validators
.
MaxValueValidator
(
180
)])),
(
'city'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'rex.City'
)),
(
'modified_by'
,
models
.
ForeignKey
(
null
=
True
,
on_delete
=
django
.
db
.
models
.
deletion
.
SET_NULL
,
to
=
settings
.
AUTH_USER_MODEL
)),
],
options
=
{
'abstract'
:
False
,
},
),
]
rex/models/localization/localization.py
View file @
810d42ed
...
...
@@ -8,7 +8,7 @@ class Region(models.Model):
name
=
models
.
CharField
(
max_length
=
200
)
un_code
=
models
.
CharField
(
primary_key
=
True
,
max_length
=
3
)
parent
=
models
.
ForeignKey
(
'self'
,
on_delete
=
models
.
SET_NULL
,
null
=
True
)
'self'
,
on_delete
=
models
.
SET_NULL
,
null
=
True
,
blank
=
True
)
def
r_type
(
self
):
p
=
0
...
...
@@ -30,7 +30,8 @@ class Region(models.Model):
class
Country
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
200
)
iso_alpha3_code
=
models
.
CharField
(
primary_key
=
True
,
max_length
=
3
)
region
=
models
.
ForeignKey
(
Region
,
on_delete
=
models
.
PROTECT
,
null
=
True
)
region
=
models
.
ForeignKey
(
Region
,
on_delete
=
models
.
PROTECT
,
null
=
True
,
blank
=
True
)
def
get_regions
(
self
):
regions
=
[
None
]
*
3
...
...
@@ -43,10 +44,10 @@ class Country(models.Model):
class
City
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
200
)
local_name
=
models
.
CharField
(
max_length
=
200
,
null
=
True
)
local_name
=
models
.
CharField
(
max_length
=
200
,
null
=
True
,
blank
=
True
)
# We add an area to distinguish similarly named cities
# in a country
area
=
models
.
CharField
(
max_length
=
200
,
null
=
True
)
area
=
models
.
CharField
(
max_length
=
200
,
null
=
True
,
blank
=
True
)
country
=
models
.
ForeignKey
(
Country
,
on_delete
=
models
.
PROTECT
)
...
...
rex/models/module/module.py
View file @
810d42ed
from
django.db
import
models
from
rest_framework
import
serializers
,
viewsets
,
permissions
from
django.contrib.auth.models
import
User
from
reversion.models
import
Version
class
Module
(
models
.
Model
):
modified_date
=
models
.
DateTimeField
(
auto_now
=
True
)
modified_by
=
models
.
ForeignKey
(
User
,
on_delete
=
models
.
SET_NULL
,
null
=
True
)
module_icon
=
"You forget to override the icon in the sub-class"
module_name
=
"You forget to override the name in the sub-class"
module_description
=
"You forget to override the description in the sub-class"
def
modified_date
(
self
):
try
:
v
=
Version
.
objects
.
get_for_object
(
self
)
return
v
.
last
().
revision
.
date_created
except
AttributeError
:
return
None
def
modified_by
(
self
):
try
:
v
=
Version
.
objects
.
get_for_object
(
self
)
user
=
v
.
last
().
revision
.
user
return
{
'user_name'
:
user
.
get_full_name
(),
'user_id'
:
user
.
pk
}
except
AttributeError
:
return
None
class
Meta
:
abstract
=
True
...
...
rex/models/university/__init__.py
View file @
810d42ed
from
.university
import
University
# noqa: F401
from
.campus
import
MainCampus
# noqa: F401
from
.campus
import
MainCampus
,
MainCampusViewSet
# noqa: F401
rex/models/university/campus.py
View file @
810d42ed
...
...
@@ -2,6 +2,7 @@ from django.db import models
from
rex.models.module
import
Module
from
rex.models.localization
import
City
from
django.core.validators
import
MinValueValidator
,
MaxValueValidator
from
rest_framework
import
serializers
,
viewsets
,
permissions
class
Campus
(
Module
):
...
...
@@ -23,6 +24,9 @@ class Campus(Module):
MaxValueValidator
(
180
)]
)
def
localization
(
self
):
return
{
'lat'
:
self
.
lat
,
'lon'
:
self
.
lon
}
class
Meta
:
abstract
=
True
...
...
@@ -31,3 +35,28 @@ class MainCampus(Campus):
module_icon
=
"You forget to override the icon in the sub-class : I know"
module_name
=
"Campus principal"
module_description
=
"Campus principal de l'université."
class
MainCampusSerializer
(
serializers
.
ModelSerializer
):
gps_coordinates
=
serializers
.
SerializerMethodField
()
modified_by
=
serializers
.
SerializerMethodField
()
modified_date
=
serializers
.
SerializerMethodField
()
def
get_gps_coordinates
(
self
,
obj
):
return
obj
.
localization
()
def
get_modified_by
(
self
,
obj
):
return
obj
.
modified_by
()
def
get_modified_date
(
self
,
obj
):
return
obj
.
modified_date
()
class
Meta
:
model
=
City
fields
=
(
'gps_coordinates'
,
'modified_by'
,
'modified_date'
,
'id'
)
class
MainCampusViewSet
(
viewsets
.
ModelViewSet
):
# TODO switch to dict
permission_classes
=
(
permissions
.
DjangoModelPermissions
,)
queryset
=
MainCampus
.
objects
.
all
()
# noqa: E1101
serializer_class
=
MainCampusSerializer
rex/models/university/university.py
View file @
810d42ed
from
django.db
import
models
from
django.core.validators
import
MinValueValidator
,
MaxValueValidator
from
rex.utils.friendly_path
import
friendly_path
from
rex.models.localization
import
Country
import
reverse_geocoder
as
rg
from
rex.models.university.campus
import
MainCampus
path_and_rename
=
friendly_path
(
"uploads/universities/logos/"
,
'name'
)
...
...
@@ -15,30 +12,11 @@ class University(models.Model):
"""
name
=
models
.
CharField
(
max_length
=
200
)
acronym
=
models
.
CharField
(
max_length
=
20
)
localization_lat
=
models
.
DecimalField
(
max_digits
=
10
,
decimal_places
=
6
,
validators
=
[
MinValueValidator
(
-
85.05112878
),
MaxValueValidator
(
85.05112878
)]
)
localization_lon
=
models
.
DecimalField
(
max_digits
=
10
,
decimal_places
=
6
,
validators
=
[
MinValueValidator
(
-
180
),
MaxValueValidator
(
180
)]
)
country
=
models
.
ForeignKey
(
Country
,
on_delete
=
'CASCADE'
)
logo
=
models
.
ImageField
(
upload_to
=
path_and_rename
,
max_length
=
250
,
default
=
'path/to/my/default/image.jpg'
)
# TODO CHANGE !
def
localization
(
self
):
return
{
'lat'
:
self
.
localization_lat
,
'lon'
:
self
.
localization_lon
}
main_campus
=
models
.
OneToOneField
(
MainCampus
,
on_delete
=
models
.
PROTECT
,
related_name
=
'main_campus_of'
)
def
save
(
self
,
*
args
,
**
kwargs
):
"""
Override traditional save function to make sure that
the Country is coherent with given lat / lon
"""
coord
=
(
self
.
localization_lat
,
self
.
localization_lon
)
res
=
rg
.
search
(
coord
,
verbose
=
False
)
self
.
country
=
Country
.
objects
.
get
(
pk
=
res
[
0
][
'cc'
])
super
(
University
,
self
).
save
(
*
args
,
**
kwargs
)
def
localization
(
self
):
return
self
.
main_campus
.
localization
()
rex/urls.py
View file @
810d42ed
...
...
@@ -2,6 +2,7 @@ from django.conf.urls import url, include
from
rex
import
views
from
rest_framework
import
routers
from
rex.models.localization
import
CountryViewSet
,
RegionViewSet
,
CityViewSet
from
rex.models.university
import
MainCampusViewSet
from
rest_framework.documentation
import
include_docs_urls
...
...
@@ -15,5 +16,6 @@ router = routers.DefaultRouter()
router
.
register
(
r
'country'
,
CountryViewSet
)
router
.
register
(
r
'region'
,
RegionViewSet
)
router
.
register
(
r
'city'
,
CityViewSet
)
router
.
register
(
r
'main-campus'
,
MainCampusViewSet
)
urlpatterns
+=
[
url
(
r
'^api/'
,
include
(
router
.
urls
))]
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment