Commit 96626cfc authored by Florent Chehab's avatar Florent Chehab

Linted python files with black

Closes #44
parent 3a3bbbd2
...@@ -14,5 +14,8 @@ ...@@ -14,5 +14,8 @@
".pytest_cache": true, ".pytest_cache": true,
}, },
"cSpell.language": "en,fr-FR,fr", "cSpell.language": "en,fr-FR,fr",
"python.linting.flake8Enabled": true "python.linting.flake8Enabled": true,
"python.formatting.provider": "black",
"python.linting.pylintEnabled": false,
"python.linting.enabled": true
} }
\ No newline at end of file
[flake8] [flake8]
# E501 line too long # E501 line too long
ignore = E501 ignore = E501,W503
exclude = exclude =
env/* env/*
node_modules/* node_modules/*
......
default_app_config = 'backend_app.apps.BackendAppConfig' default_app_config = "backend_app.apps.BackendAppConfig"
...@@ -11,10 +11,12 @@ CLASSIC_MODELS = [] ...@@ -11,10 +11,12 @@ CLASSIC_MODELS = []
VERSIONNED_MODELS = [] VERSIONNED_MODELS = []
for model in api_config: for model in api_config:
if "model" in model and model['model']: if "model" in model and model["model"]:
model = DotMap(model) model = DotMap(model)
if (not model.requires_testing) and (not model.ignore_in_admin): if (not model.requires_testing) and (not model.ignore_in_admin):
module = importlib.import_module("backend_app.models.{}".format(model.import_location)) module = importlib.import_module(
"backend_app.models.{}".format(model.import_location)
)
if model.versionned: if model.versionned:
VERSIONNED_MODELS.append(getattr(module, model.model)) VERSIONNED_MODELS.append(getattr(module, model.model))
else: else:
...@@ -31,6 +33,5 @@ for model in CLASSIC_MODELS: ...@@ -31,6 +33,5 @@ for model in CLASSIC_MODELS:
for model in VERSIONNED_MODELS: for model in VERSIONNED_MODELS:
admin.site.register(model, CompareVersionAdmin) admin.site.register(model, CompareVersionAdmin)
if (model.get_serializer().Meta.model != model): if model.get_serializer().Meta.model != model:
raise Exception( raise Exception("Get_serializer configuration incorrect in", str(model))
"Get_serializer configuration incorrect in", str(model))
...@@ -2,9 +2,9 @@ from django.apps import AppConfig ...@@ -2,9 +2,9 @@ from django.apps import AppConfig
class BackendAppConfig(AppConfig): class BackendAppConfig(AppConfig):
name = 'backend_app' name = "backend_app"
def ready(self): def ready(self):
import backend_app.signals.__create_user_modules_post_create # noqa: F401 import backend_app.signals.__create_user_modules_post_create # noqa:F401
import backend_app.signals.__squash_revision_by_user # noqa: F401 import backend_app.signals.__squash_revision_by_user # noqa:F401
import backend_app.signals.__create_univ_modules_post_save # noqa: F401 import backend_app.signals.__create_univ_modules_post_save # noqa:F401
...@@ -6,12 +6,14 @@ class DictModeViewSet(viewsets.ModelViewSet): ...@@ -6,12 +6,14 @@ class DictModeViewSet(viewsets.ModelViewSet):
ViewSet that renders data as dict with keys corresponding to the model ViewSet that renders data as dict with keys corresponding to the model
primary key. Instead of list. primary key. Instead of list.
""" """
BYPASS_DICT_MODE = False BYPASS_DICT_MODE = False
LIST_SHOULD_BE_DETAIL = False LIST_SHOULD_BE_DETAIL = False
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003 response = super(viewsets.ModelViewSet, self).list( # pylint: disable=E1003
request, *args, **kwargs) # call the original 'list' request, *args, **kwargs
) # call the original 'list'
if self.LIST_SHOULD_BE_DETAIL: if self.LIST_SHOULD_BE_DETAIL:
if len(response.data) == 0: if len(response.data) == 0:
...@@ -20,12 +22,11 @@ class DictModeViewSet(viewsets.ModelViewSet): ...@@ -20,12 +22,11 @@ class DictModeViewSet(viewsets.ModelViewSet):
response.data = response.data[0] response.data = response.data[0]
else: else:
raise Exception( raise Exception(
"There should be no more than one element here check your queryset !") "There should be no more than one element here check your queryset !"
)
elif not self.BYPASS_DICT_MODE: elif not self.BYPASS_DICT_MODE:
pk_attr_name = self.serializer_class.Meta.model._meta.pk.name pk_attr_name = self.serializer_class.Meta.model._meta.pk.name
response.data = { response.data = {d[pk_attr_name]: d for d in response.data}
d[pk_attr_name]: d for d in response.data
}
return response return response
from .DictModeViewSet import DictModeViewSet # noqa: F401 from .DictModeViewSet import DictModeViewSet
from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401 from .mySerializerWithJSON import MySerializerWithJSON
__all__ = ["DictModeViewSet", "MySerializerWithJSON"]
...@@ -9,4 +9,5 @@ class MySerializerWithJSON(serializers.ModelSerializer): ...@@ -9,4 +9,5 @@ class MySerializerWithJSON(serializers.ModelSerializer):
""" """
Simple class to add support for custom JSONField support Simple class to add support for custom JSONField support
""" """
serializer_field_mapping = field_mapping serializer_field_mapping = field_mapping
...@@ -17,10 +17,7 @@ class JSONField(models.TextField): ...@@ -17,10 +17,7 @@ class JSONField(models.TextField):
try: try:
return json.loads(value) return json.loads(value)
except json.decoder.JSONDecodeError as e: except json.decoder.JSONDecodeError as e:
raise ValidationError( raise ValidationError(message=str(e), code="json_error")
message=str(e),
code="json_error"
)
def to_json(self, value): def to_json(self, value):
""" convert python dictionary to json string """ """ convert python dictionary to json string """
......
from .load_all import load_all # noqa: F401 from .load_all import load_all
__all__ = ["load_all"]
...@@ -11,19 +11,20 @@ import time ...@@ -11,19 +11,20 @@ import time
from geopy.geocoders import Nominatim from geopy.geocoders import Nominatim
import reverse_geocoder as rg import reverse_geocoder as rg
tmp = os.path.join(os.path.realpath(__file__), '../../assets/destinations.csv') tmp = os.path.join(os.path.realpath(__file__), "../../assets/destinations.csv")
destinations_path = os.path.abspath(tmp) destinations_path = os.path.abspath(tmp)
tmp = os.path.join(os.path.realpath(__file__), tmp = os.path.join(
'../../assets/destinations_extracted.csv') os.path.realpath(__file__), "../../assets/destinations_extracted.csv"
)
destinations_extracted_path = os.path.abspath(tmp) destinations_extracted_path = os.path.abspath(tmp)
if not os.path.isfile(destinations_path): if not os.path.isfile(destinations_path):
print(destinations_path) print(destinations_path)
raise Exception("Missing file containing country data") raise Exception("Missing file containing country data")
with open(destinations_path, 'rt') as input: with open(destinations_path, "rt") as input:
with open(destinations_extracted_path, 'w') as output: with open(destinations_extracted_path, "w") as output:
print("ini") print("ini")
reader = csv.reader(input) reader = csv.reader(input)
spamwriter = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC) spamwriter = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
...@@ -34,7 +35,7 @@ with open(destinations_path, 'rt') as input: ...@@ -34,7 +35,7 @@ with open(destinations_path, 'rt') as input:
for row in reader: for row in reader:
# handle the header # handle the header
if i == 0: if i == 0:
header = ['university', 'city', 'country', 'lat', 'lon'] header = ["university", "city", "country", "lat", "lon"]
spamwriter.writerow(header) spamwriter.writerow(header)
i += 1 i += 1
else: else:
...@@ -50,7 +51,13 @@ with open(destinations_path, 'rt') as input: ...@@ -50,7 +51,13 @@ with open(destinations_path, 'rt') as input:
if location is not None: if location is not None:
coord = (location.latitude, location.longitude) coord = (location.latitude, location.longitude)
res = rg.search(coord, verbose=False) res = rg.search(coord, verbose=False)
line = [row[2], row[1], res[0]['cc'], location.latitude, location.longitude] line = [
row[2],
row[1],
res[0]["cc"],
location.latitude,
location.longitude,
]
print(line) print(line)
spamwriter.writerow(line) spamwriter.writerow(line)
else: else:
......
from .loadGroups import LoadGroups # noqa: F401 from .loadGroups import LoadGroups
from .loadAdminUser import LoadAdminUser # noqa: F401 from .loadAdminUser import LoadAdminUser
from .loadCountries import LoadCountries # noqa: F401 from .loadCountries import LoadCountries
from .loadUniversities import LoadUniversities # noqa: F401 from .loadUniversities import LoadUniversities
from .loadTags import LoadTags # noqa: F401 from .loadTags import LoadTags
from .loadCurrencies import LoadCurrencies # noqa: F401 from .loadCurrencies import LoadCurrencies
from .loadUniversityEx import LoadUniversityEx # noqa: F401 from .loadUniversityEx import LoadUniversityEx
__all__ = [
"LoadGroups",
"LoadAdminUser",
"LoadCountries",
"LoadUniversities",
"LoadTags",
"LoadCurrencies",
"LoadUniversityEx",
]
...@@ -13,9 +13,7 @@ class LoadAdminUser(object): ...@@ -13,9 +13,7 @@ class LoadAdminUser(object):
self.admin = user[0] self.admin = user[0]
else: else:
User.objects.create_superuser( User.objects.create_superuser(
username="admin", username="admin", email="null@null.fr", password="admin"
email='null@null.fr',
password='admin',
) )
self.admin = User.objects.filter(username="admin")[0] self.admin = User.objects.filter(username="admin")[0]
......
...@@ -5,31 +5,30 @@ from .loadGeneric import LoadGeneric ...@@ -5,31 +5,30 @@ from .loadGeneric import LoadGeneric
class LoadCountries(LoadGeneric): class LoadCountries(LoadGeneric):
def __init__(self, admin): def __init__(self, admin):
self.admin = admin self.admin = admin
def load(self): def load(self):
tmp = os.path.join(os.path.realpath(__file__), tmp = os.path.join(os.path.realpath(__file__), "../../assets/country.csv")
'../../assets/country.csv')
country_file_loc = os.path.abspath(tmp) country_file_loc = os.path.abspath(tmp)
tmp = os.path.join(os.path.realpath(__file__), tmp = os.path.join(
'../../assets/alpha-conv-table.csv') os.path.realpath(__file__), "../../assets/alpha-conv-table.csv"
)
conv_alpha_file_loc = os.path.abspath(tmp) conv_alpha_file_loc = os.path.abspath(tmp)
country_pd = pd.read_csv(country_file_loc, sep=',', header=0, country_pd = pd.read_csv(
dtype=object).fillna('') country_file_loc, sep=",", header=0, dtype=object
).fillna("")
# Need to load the information for converting # Need to load the information for converting
# Countries alpha-3 code to alpha-2 code # Countries alpha-3 code to alpha-2 code
data_conv = pd.read_csv(conv_alpha_file_loc, sep=',', data_conv = pd.read_csv(conv_alpha_file_loc, sep=",", header=0, na_filter=False)
header=0, na_filter=False)
conv_alpha = {} conv_alpha = {}
for index, row in data_conv.iterrows(): for index, row in data_conv.iterrows():
conv_alpha[row["alpha-3"]] = row["alpha-2"] conv_alpha[row["alpha-3"]] = row["alpha-2"]
for index, r in country_pd.iterrows(): for index, r in country_pd.iterrows():
Iso_3 = str(r['ISO-alpha3 Code']) Iso_3 = str(r["ISO-alpha3 Code"])
code_alpha_2 = None code_alpha_2 = None
if Iso_3 in conv_alpha.keys(): if Iso_3 in conv_alpha.keys():
code_alpha_2 = conv_alpha[Iso_3] code_alpha_2 = conv_alpha[Iso_3]
......
...@@ -6,13 +6,11 @@ from decimal import Decimal ...@@ -6,13 +6,11 @@ from decimal import Decimal
class LoadCurrencies(LoadGeneric): class LoadCurrencies(LoadGeneric):
def __init__(self, admin): def __init__(self, admin):
self.admin = admin self.admin = admin
def load(self): def load(self):
tmp = os.path.join(os.path.realpath(__file__), tmp = os.path.join(os.path.realpath(__file__), "../../assets/currencies.csv")
'../../assets/currencies.csv')
currencies_file_loc = os.path.abspath(tmp) currencies_file_loc = os.path.abspath(tmp)
with open(currencies_file_loc) as csvfile: with open(currencies_file_loc) as csvfile:
...@@ -23,7 +21,7 @@ class LoadCurrencies(LoadGeneric): ...@@ -23,7 +21,7 @@ class LoadCurrencies(LoadGeneric):
code=r[0], code=r[0],
name=r[1], name=r[1],
symbol="", symbol="",
one_EUR_in_this_currency=Decimal(r[2]) one_EUR_in_this_currency=Decimal(r[2]),
) )
currency.save() currency.save()
self.add_info_and_save(currency, self.admin) self.add_info_and_save(currency, self.admin)
...@@ -3,7 +3,6 @@ import reversion ...@@ -3,7 +3,6 @@ import reversion
class LoadGeneric(object): class LoadGeneric(object):
@classmethod @classmethod
def add_info_and_save(cls, obj, admin): def add_info_and_save(cls, obj, admin):
with reversion.create_revision(): with reversion.create_revision():
......
...@@ -3,5 +3,5 @@ from django.contrib.auth.models import Group ...@@ -3,5 +3,5 @@ from django.contrib.auth.models import Group
class LoadGroups(object): class LoadGroups(object):
def __init__(self): def __init__(self):
Group.objects.get_or_create(name='Moderators') Group.objects.get_or_create(name="Moderators")
Group.objects.get_or_create(name='DRI') Group.objects.get_or_create(name="DRI")
...@@ -6,18 +6,16 @@ from .loadGeneric import LoadGeneric ...@@ -6,18 +6,16 @@ from .loadGeneric import LoadGeneric
class LoadTags(LoadGeneric): class LoadTags(LoadGeneric):
def __init__(self, admin): def __init__(self, admin):
self.admin = admin self.admin = admin
def load(self): def load(self):
tmp = os.path.join(os.path.realpath(__file__), tmp = os.path.join(os.path.realpath(__file__), "../../assets/tags.json")
'../../assets/tags.json')
tags_path = os.path.abspath(tmp) tags_path = os.path.abspath(tmp)
with open(tags_path) as f: with open(tags_path) as f:
tags = json.load(f) tags = json.load(f)
for tag in tags: for tag in tags:
t = Tag(name=tag['name'], config=tag['config']) t = Tag(name=tag["name"], config=tag["config"])
t.save() t.save()
self.add_info_and_save(t, self.admin) self.add_info_and_save(t, self.admin)
...@@ -9,29 +9,29 @@ from .loadGeneric import LoadGeneric ...@@ -9,29 +9,29 @@ from .loadGeneric import LoadGeneric
class LoadUniversities(LoadGeneric): class LoadUniversities(LoadGeneric):
def __init__(self, admin): def __init__(self, admin):
self.admin = admin self.admin = admin
def load(self): def load(self):
tmp = os.path.join(os.path.realpath(__file__), tmp = os.path.join(
'../../assets/destinations_extracted.csv') os.path.realpath(__file__), "../../assets/destinations_extracted.csv"
)
destinations_path = os.path.abspath(tmp) destinations_path = os.path.abspath(tmp)
data = pd.read_csv(destinations_path, sep=',', header=0, data = pd.read_csv(destinations_path, sep=",", header=0, dtype=object).fillna(
dtype=object).fillna('') ""
)
for index, row in data.iterrows(): for index, row in data.iterrows():
utc_id, univ_name, city_name, country_code, lat, lon, acronym, website, logo = row utc_id, univ_name, city_name, country_code, lat, lon, acronym, website, logo = (
row
)
lat = round(float(lat), 6) lat = round(float(lat), 6)
lon = round(float(lon), 6) lon = round(float(lon), 6)
country = Country.objects.get(pk=country_code) country = Country.objects.get(pk=country_code)
city = City( city = City(name=city_name, country=country)
name=city_name,
country=country
)
city.save() city.save()
self.add_info_and_save(city, self.admin) self.add_info_and_save(city, self.admin)
...@@ -41,8 +41,8 @@ class LoadUniversities(LoadGeneric): ...@@ -41,8 +41,8 @@ class LoadUniversities(LoadGeneric):
"name": univ_name, "name": univ_name,
"acronym": acronym, "acronym": acronym,
"website": website, "website": website,
"logo": logo "logo": logo,
} },
)[0] )[0]
self.add_info_and_save(univ, self.admin) self.add_info_and_save(univ, self.admin)
...@@ -52,7 +52,7 @@ class LoadUniversities(LoadGeneric): ...@@ -52,7 +52,7 @@ class LoadUniversities(LoadGeneric):
city=city, city=city,
university=univ, university=univ,
lat=lat, lat=lat,
lon=lon lon=lon,
) )
main_campus.save() main_campus.save()
self.add_info_and_save(main_campus, self.admin) self.add_info_and_save(main_campus, self.admin)
...@@ -14,19 +14,18 @@ from datetime import datetime ...@@ -14,19 +14,18 @@ from datetime import datetime
class LoadUniversityEx(LoadGeneric): class LoadUniversityEx(LoadGeneric):
def __init__(self, admin): def __init__(self, admin):
self.admin = admin self.admin = admin
def load(self): def load(self):
EPFL = University.objects.get(acronym='EPFL') EPFL = University.objects.get(acronym="EPFL")
CHF = Currency.objects.get(pk='CHF') CHF = Currency.objects.get(pk="CHF")
ACCOMMODATION_TAG = Tag.objects.get(name='accommodation') ACCOMMODATION_TAG = Tag.objects.get(name="accommodation")
SWITZERLAND = Country.objects.get(pk="CH") SWITZERLAND = Country.objects.get(pk="CH")
univ_dri_1 = UniversityDri( univ_dri_1 = UniversityDri(
title="Cours en anglais", title="Cours en anglais",
importance_level='+', importance_level="+",
comment="Les cours de master en computer science sont 100% en anglais", comment="Les cours de master en computer science sont 100% en anglais",
) )
univ_dri_1.save() univ_dri_1.save()
...@@ -38,13 +37,14 @@ class LoadUniversityEx(LoadGeneric): ...@@ -38,13 +37,14 @@ class LoadUniversityEx(LoadGeneric):
univ_info.costs_currency = CHF univ_info.costs_currency = CHF
self.add_info_and_save(univ_info, self.admin) self.add_info_and_save(univ_info, self.admin)
usd = UniversitySemestersDates.objects.get( usd = UniversitySemestersDates.objects.get(university=EPFL)
university=EPFL) usd.autumn_begin = datetime.strptime("17/09/2018", "%d/%m/%Y")
usd.autumn_begin = datetime.strptime("17/09/2018", '%d/%m/%Y') usd.autumn_end = datetime.strptime("29/01/2019", "%d/%m/%Y")
usd.autumn_end = datetime.strptime("29/01/2019", '%d/%m/%Y')
usd.useful_links = [ usd.useful_links = [
{"url": "https://memento.epfl.ch/academic-calendar", {
"description": "Site de l'EPFL"} "url": "https://memento.epfl.ch/academic-calendar",
"description": "Site de l'EPFL",
}
] ]
self.add_info_and_save(usd, self.admin) self.add_info_and_save(usd, self.admin)
...@@ -52,10 +52,10 @@ class LoadUniversityEx(LoadGeneric): ...@@ -52,10 +52,10 @@ class LoadUniversityEx(LoadGeneric):
title="Swiss European Mobility Programme", title="Swiss European Mobility Programme",
type="Bourse du gouvernement suisse", type="Bourse du gouvernement suisse",
currency=CHF, currency=CHF,
frequency='s', frequency="s",
amount_min=2200, amount_min=2200,
amount_max=2200, amount_max=2200,
comment="Bourse attribuée de manière automatique." comment="Bourse attribuée de manière automatique.",
) )
country_scholarship.save() country_scholarship.save()
country_scholarship.countries.add(SWITZERLAND) country_scholarship.countries.add(SWITZERLAND)
...@@ -66,6 +66,6 @@ class LoadUniversityEx(LoadGeneric): ...@@ -66,6 +66,6 @@ class LoadUniversityEx(LoadGeneric):
tag=ACCOMMODATION_TAG, tag=ACCOMMODATION_TAG,
title="C'est compliqué de trouver un logement", title="C'est compliqué de trouver un logement",
comment="Mon commentaire.", comment="Mon commentaire.",
importance_level='++' importance_level="++",
) )
self.add_info_and_save(univ_tag_1, self.admin) self.add_info_and_save(univ_tag_1, self.admin)
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -6,204 +6,208 @@ from django.db import migrations, models ...@@ -6,204 +6,208 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [("backend_app", "0001_initial")]
('backend_app', '0001_initial'),
]
operations = [ operations = [
migrations.AlterField( migrations.AlterField(
model_name='campus', model_name="campus",
name='comment', name="comment",
field=models.CharField(blank=True, default='', max_length=5000), field=models.CharField(blank=True, default="", max_length=5000),
), ),
migrations.AlterField( migrations.AlterField(
model_name='campus', model_name="campus",
name='name', name="name",
field=models.CharField(blank=True, default='', max_length=200), field=models.CharField(blank=True, default="", max_length=200),
), ),
migrations.AlterField( migrations.AlterField(
model_name='campus', model_name="campus",
name='title', name="title",