Commit e26d998e authored by Florent Chehab's avatar Florent Chehab
Browse files

more pythonic code

parent 573a4464
...@@ -8,5 +8,6 @@ ...@@ -8,5 +8,6 @@
"**/.DS_Store": true, "**/.DS_Store": true,
"**/__pycache__":true "**/__pycache__":true
}, },
"cSpell.language": "en" "cSpell.language": "en",
"python.linting.flake8Enabled": true
} }
\ No newline at end of file
...@@ -56,22 +56,22 @@ MIDDLEWARE = [ ...@@ -56,22 +56,22 @@ MIDDLEWARE = [
] ]
LOGIN_URL = 'accounts/login' LOGIN_URL = 'accounts/login'
# TODO add ignore administration # TODO add ignore administration
AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend', 'django.contrib.auth.backends.ModelBackend',
'django_cas_ng.backends.CASBackend', 'django_cas_ng.backends.CASBackend',
] ]
CAS_SERVER_URL= 'https://cas.utc.fr/cas/' CAS_SERVER_URL = 'https://cas.utc.fr/cas/'
CAS_APPLY_ATTRIBUTES_TO_USER = True CAS_APPLY_ATTRIBUTES_TO_USER = True
CAS_RENAME_ATTRIBUTES={ CAS_RENAME_ATTRIBUTES = {
'mail':'email', 'mail': 'email',
'givenName':'first_name', 'givenName': 'first_name',
'sn':'last_name' 'sn': 'last_name'
} }
SESSION_EXPIRE_AT_BROWSER_CLOSE=True SESSION_EXPIRE_AT_BROWSER_CLOSE = True
ROOT_URLCONF = 'general.urls' ROOT_URLCONF = 'general.urls'
...@@ -110,16 +110,20 @@ DATABASES = { ...@@ -110,16 +110,20 @@ DATABASES = {
AUTH_PASSWORD_VALIDATORS = [ AUTH_PASSWORD_VALIDATORS = [
{ {
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 'NAME': 'django.contrib.auth.password_validation.\
UserAttributeSimilarityValidator',
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 'NAME': 'django.contrib.auth.password_validation.\
MinimumLengthValidator',
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 'NAME': 'django.contrib.auth.password_validation.\
CommonPasswordValidator',
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 'NAME': 'django.contrib.auth.password_validation.\
NumericPasswordValidator',
}, },
] ]
......
...@@ -5,12 +5,17 @@ from django.contrib import admin ...@@ -5,12 +5,17 @@ from django.contrib import admin
import django_cas_ng.views import django_cas_ng.views
urlpatterns = [ urlpatterns = [
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^accounts/login$', django_cas_ng.views.login, name='cas_ng_login'), url(r'^accounts/login$',
url(r'^accounts/logout$', django_cas_ng.views.logout, name='cas_ng_logout'), django_cas_ng.views.login,
url(r'^accounts/callback$', django_cas_ng.views.callback, name='cas_ng_proxy_callback'), name='cas_ng_login'),
url(r'^accounts/logout$',
django_cas_ng.views.logout,
name='cas_ng_logout'),
url(r'^accounts/callback$',
django_cas_ng.views.callback,
name='cas_ng_proxy_callback'),
url(r'', include('rex.urls')), url(r'', include('rex.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
......
...@@ -4,5 +4,6 @@ from reversion_compare.admin import CompareVersionAdmin ...@@ -4,5 +4,6 @@ from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University from rex.models.university import University
from rex.models.country import Country from rex.models.country import Country
admin.site.register(University, CompareVersionAdmin) admin.site.register(University, CompareVersionAdmin)
admin.site.register(Country) admin.site.register(Country)
from django.db import models from django.db import models
class Country(models.Model): class Country(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
iso_code = models.CharField(primary_key = True, max_length=2) iso_code = models.CharField(primary_key=True, max_length=2)
\ No newline at end of file \ No newline at end of file
...@@ -6,7 +6,8 @@ from rex.models.country import Country ...@@ -6,7 +6,8 @@ from rex.models.country import Country
import reverse_geocoder as rg import reverse_geocoder as rg
path_and_rename = friendly_path("uploads/universities/logos/",'name') path_and_rename = friendly_path("uploads/universities/logos/", 'name')
class University(models.Model): class University(models.Model):
""" """
...@@ -15,12 +16,13 @@ class University(models.Model): ...@@ -15,12 +16,13 @@ class University(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
acronym = models.CharField(max_length=20) acronym = models.CharField(max_length=20)
localization_lat = models.DecimalField( localization_lat = models.DecimalField(
max_digits=10, max_digits=10,
decimal_places=6, decimal_places=6,
validators=[MinValueValidator(-85.05112878), MaxValueValidator(85.05112878)] validators=[MinValueValidator(-85.05112878),
MaxValueValidator(85.05112878)]
) )
localization_lon = models.DecimalField( localization_lon = models.DecimalField(
max_digits=10, max_digits=10,
decimal_places=6, decimal_places=6,
validators=[MinValueValidator(-180), MaxValueValidator(180)] validators=[MinValueValidator(-180), MaxValueValidator(180)]
) )
...@@ -28,7 +30,7 @@ class University(models.Model): ...@@ -28,7 +30,7 @@ class University(models.Model):
logo = models.ImageField(upload_to=path_and_rename, max_length=250) logo = models.ImageField(upload_to=path_and_rename, max_length=250)
def localization(self): def localization(self):
return {'lat':self.localization_lat, 'lon':self.localization_lon} return {'lat': self.localization_lat, 'lon': self.localization_lon}
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" """
......
from django.conf.urls import url from django.conf.urls import url
from . import views from . import views
urlpatterns = [ urlpatterns = [
url(r'^$', views.home, name='home'), url(r'^$', views.home, name='home'),
] ]
\ No newline at end of file
from django.conf import settings from django.conf import settings
from django.utils.deconstruct import deconstructible from django.utils.deconstruct import deconstructible
import os import os
@deconstructible @deconstructible
class friendly_path(object): class friendly_path(object):
"""
class to generate file path with filenames
according to one of the model attribute
"""
def __init__(self, rel_path, attribute_distinction): def __init__(self, rel_path, attribute_distinction):
self.rel_path = rel_path self.rel_path = rel_path
self.attr = attribute_distinction self.attr = attribute_distinction
...@@ -14,8 +17,10 @@ class friendly_path(object): ...@@ -14,8 +17,10 @@ class friendly_path(object):
ext = filename.split('.')[-1] ext = filename.split('.')[-1]
i = 0 i = 0
while True: while True:
filename = '{}_{}.{}'.format(getattr(instance, self.attr), str(i), ext) filename = '{}_{}.{}'.format(
path_and_fn = os.path.join(settings.MEDIA_ROOT, self.rel_path, filename) getattr(instance, self.attr), str(i), ext)
path_and_fn = os.path.join(
settings.MEDIA_ROOT, self.rel_path, filename)
if not os.path.isfile(path_and_fn): if not os.path.isfile(path_and_fn):
break break
i += 1 i += 1
......
...@@ -5,13 +5,13 @@ Script to insert the country data in the database ...@@ -5,13 +5,13 @@ Script to insert the country data in the database
IT HAS TO BE RUN INSIDE ./manage.py shell IT HAS TO BE RUN INSIDE ./manage.py shell
""" """
import csv import csv
import os import os
from rex.models.country import Country from rex.models.country import Country
tmp = os.path.join(os.path.realpath(__file__),'../../assets/country.csv') tmp = os.path.join(os.path.realpath(__file__), '../../assets/country.csv')
country_file_loc = os.path.abspath(tmp) country_file_loc = os.path.abspath(tmp)
if not os.path.isfile(country_file_loc): if not os.path.isfile(country_file_loc):
......
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