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

Linted python files with black

Closes #44
parent 3a3bbbd2
......@@ -14,5 +14,8 @@
".pytest_cache": true,
},
"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]
# E501 line too long
ignore = E501
ignore = E501,W503
exclude =
env/*
node_modules/*
......
default_app_config = 'backend_app.apps.BackendAppConfig'
default_app_config = "backend_app.apps.BackendAppConfig"
......@@ -11,10 +11,12 @@ CLASSIC_MODELS = []
VERSIONNED_MODELS = []
for model in api_config:
if "model" in model and model['model']:
if "model" in model and model["model"]:
model = DotMap(model)
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:
VERSIONNED_MODELS.append(getattr(module, model.model))
else:
......@@ -31,6 +33,5 @@ for model in CLASSIC_MODELS:
for model in VERSIONNED_MODELS:
admin.site.register(model, CompareVersionAdmin)
if (model.get_serializer().Meta.model != model):
raise Exception(
"Get_serializer configuration incorrect in", str(model))
if model.get_serializer().Meta.model != model:
raise Exception("Get_serializer configuration incorrect in", str(model))
......@@ -2,9 +2,9 @@ from django.apps import AppConfig
class BackendAppConfig(AppConfig):
name = 'backend_app'
name = "backend_app"
def ready(self):
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.__create_univ_modules_post_save # 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.__create_univ_modules_post_save # noqa:F401
......@@ -6,12 +6,14 @@ class DictModeViewSet(viewsets.ModelViewSet):
ViewSet that renders data as dict with keys corresponding to the model
primary key. Instead of list.
"""
BYPASS_DICT_MODE = False
LIST_SHOULD_BE_DETAIL = False
def list(self, request, *args, **kwargs):
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 len(response.data) == 0:
......@@ -20,12 +22,11 @@ class DictModeViewSet(viewsets.ModelViewSet):
response.data = response.data[0]
else:
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:
pk_attr_name = self.serializer_class.Meta.model._meta.pk.name
response.data = {
d[pk_attr_name]: d for d in response.data
}
response.data = {d[pk_attr_name]: d for d in response.data}
return response
from .DictModeViewSet import DictModeViewSet # noqa: F401
from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401
from .DictModeViewSet import DictModeViewSet
from .mySerializerWithJSON import MySerializerWithJSON
__all__ = ["DictModeViewSet", "MySerializerWithJSON"]
......@@ -9,4 +9,5 @@ class MySerializerWithJSON(serializers.ModelSerializer):
"""
Simple class to add support for custom JSONField support
"""
serializer_field_mapping = field_mapping
......@@ -17,10 +17,7 @@ class JSONField(models.TextField):
try:
return json.loads(value)
except json.decoder.JSONDecodeError as e:
raise ValidationError(
message=str(e),
code="json_error"
)
raise ValidationError(message=str(e), code="json_error")
def to_json(self, value):
""" 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
from geopy.geocoders import Nominatim
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)
tmp = os.path.join(os.path.realpath(__file__),
'../../assets/destinations_extracted.csv')
tmp = os.path.join(
os.path.realpath(__file__), "../../assets/destinations_extracted.csv"
)
destinations_extracted_path = os.path.abspath(tmp)
if not os.path.isfile(destinations_path):
print(destinations_path)
raise Exception("Missing file containing country data")
with open(destinations_path, 'rt') as input:
with open(destinations_extracted_path, 'w') as output:
with open(destinations_path, "rt") as input:
with open(destinations_extracted_path, "w") as output:
print("ini")
reader = csv.reader(input)
spamwriter = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
......@@ -34,7 +35,7 @@ with open(destinations_path, 'rt') as input:
for row in reader:
# handle the header
if i == 0:
header = ['university', 'city', 'country', 'lat', 'lon']
header = ["university", "city", "country", "lat", "lon"]
spamwriter.writerow(header)
i += 1
else:
......@@ -50,7 +51,13 @@ with open(destinations_path, 'rt') as input:
if location is not None:
coord = (location.latitude, location.longitude)
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)
spamwriter.writerow(line)
else:
......
from .loadGroups import LoadGroups # noqa: F401
from .loadAdminUser import LoadAdminUser # noqa: F401
from .loadCountries import LoadCountries # noqa: F401
from .loadUniversities import LoadUniversities # noqa: F401
from .loadTags import LoadTags # noqa: F401
from .loadCurrencies import LoadCurrencies # noqa: F401
from .loadUniversityEx import LoadUniversityEx # noqa: F401
from .loadGroups import LoadGroups
from .loadAdminUser import LoadAdminUser
from .loadCountries import LoadCountries
from .loadUniversities import LoadUniversities
from .loadTags import LoadTags
from .loadCurrencies import LoadCurrencies
from .loadUniversityEx import LoadUniversityEx
__all__ = [
"LoadGroups",
"LoadAdminUser",
"LoadCountries",
"LoadUniversities",
"LoadTags",
"LoadCurrencies",
"LoadUniversityEx",
]
......@@ -13,9 +13,7 @@ class LoadAdminUser(object):
self.admin = user[0]
else:
User.objects.create_superuser(
username="admin",
email='null@null.fr',
password='admin',
username="admin", email="null@null.fr", password="admin"
)
self.admin = User.objects.filter(username="admin")[0]
......
......@@ -5,31 +5,30 @@ from .loadGeneric import LoadGeneric
class LoadCountries(LoadGeneric):
def __init__(self, admin):
self.admin = admin
def load(self):
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)
tmp = os.path.join(os.path.realpath(__file__),
'../../assets/alpha-conv-table.csv')
tmp = os.path.join(
os.path.realpath(__file__), "../../assets/alpha-conv-table.csv"
)
conv_alpha_file_loc = os.path.abspath(tmp)
country_pd = pd.read_csv(country_file_loc, sep=',', header=0,
dtype=object).fillna('')
country_pd = pd.read_csv(
country_file_loc, sep=",", header=0, dtype=object
).fillna("")
# Need to load the information for converting
# Countries alpha-3 code to alpha-2 code
data_conv = pd.read_csv(conv_alpha_file_loc, sep=',',
header=0, na_filter=False)
data_conv = pd.read_csv(conv_alpha_file_loc, sep=",", header=0, na_filter=False)
conv_alpha = {}
for index, row in data_conv.iterrows():
conv_alpha[row["alpha-3"]] = row["alpha-2"]
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
if Iso_3 in conv_alpha.keys():
code_alpha_2 = conv_alpha[Iso_3]
......
......@@ -6,13 +6,11 @@ from decimal import Decimal
class LoadCurrencies(LoadGeneric):
def __init__(self, admin):
self.admin = admin
def load(self):
tmp = os.path.join(os.path.realpath(__file__),
'../../assets/currencies.csv')
tmp = os.path.join(os.path.realpath(__file__), "../../assets/currencies.csv")
currencies_file_loc = os.path.abspath(tmp)
with open(currencies_file_loc) as csvfile:
......@@ -23,7 +21,7 @@ class LoadCurrencies(LoadGeneric):
code=r[0],
name=r[1],
symbol="",
one_EUR_in_this_currency=Decimal(r[2])
one_EUR_in_this_currency=Decimal(r[2]),
)
currency.save()
self.add_info_and_save(currency, self.admin)
......@@ -3,7 +3,6 @@ import reversion
class LoadGeneric(object):
@classmethod
def add_info_and_save(cls, obj, admin):
with reversion.create_revision():
......
......@@ -3,5 +3,5 @@ from django.contrib.auth.models import Group
class LoadGroups(object):
def __init__(self):
Group.objects.get_or_create(name='Moderators')
Group.objects.get_or_create(name='DRI')
Group.objects.get_or_create(name="Moderators")
Group.objects.get_or_create(name="DRI")
......@@ -6,18 +6,16 @@ from .loadGeneric import LoadGeneric
class LoadTags(LoadGeneric):
def __init__(self, admin):
self.admin = admin
def load(self):
tmp = os.path.join(os.path.realpath(__file__),
'../../assets/tags.json')
tmp = os.path.join(os.path.realpath(__file__), "../../assets/tags.json")
tags_path = os.path.abspath(tmp)
with open(tags_path) as f:
tags = json.load(f)
for tag in tags:
t = Tag(name=tag['name'], config=tag['config'])
t = Tag(name=tag["name"], config=tag["config"])
t.save()
self.add_info_and_save(t, self.admin)
......@@ -9,29 +9,29 @@ from .loadGeneric import LoadGeneric
class LoadUniversities(LoadGeneric):
def __init__(self, admin):
self.admin = admin
def load(self):
tmp = os.path.join(os.path.realpath(__file__),
'../../assets/destinations_extracted.csv')
tmp = os.path.join(
os.path.realpath(__file__), "../../assets/destinations_extracted.csv"
)
destinations_path = os.path.abspath(tmp)
data = pd.read_csv(destinations_path, sep=',', header=0,
dtype=object).fillna('')
data = pd.read_csv(destinations_path, sep=",", header=0, dtype=object).fillna(
""
)
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)
lon = round(float(lon), 6)
country = Country.objects.get(pk=country_code)
city = City(
name=city_name,
country=country
)
city = City(name=city_name, country=country)
city.save()
self.add_info_and_save(city, self.admin)
......@@ -41,8 +41,8 @@ class LoadUniversities(LoadGeneric):
"name": univ_name,
"acronym": acronym,
"website": website,
"logo": logo
}
"logo": logo,
},
)[0]
self.add_info_and_save(univ, self.admin)
......@@ -52,7 +52,7 @@ class LoadUniversities(LoadGeneric):
city=city,
university=univ,
lat=lat,
lon=lon
lon=lon,
)
main_campus.save()
self.add_info_and_save(main_campus, self.admin)
......@@ -14,19 +14,18 @@ from datetime import datetime
class LoadUniversityEx(LoadGeneric):
def __init__(self, admin):
self.admin = admin
def load(self):
EPFL = University.objects.get(acronym='EPFL')
CHF = Currency.objects.get(pk='CHF')
ACCOMMODATION_TAG = Tag.objects.get(name='accommodation')
EPFL = University.objects.get(acronym="EPFL")
CHF = Currency.objects.get(pk="CHF")
ACCOMMODATION_TAG = Tag.objects.get(name="accommodation")
SWITZERLAND = Country.objects.get(pk="CH")
univ_dri_1 = UniversityDri(
title="Cours en anglais",
importance_level='+',
importance_level="+",
comment="Les cours de master en computer science sont 100% en anglais",
)
univ_dri_1.save()
......@@ -38,13 +37,14 @@ class LoadUniversityEx(LoadGeneric):
univ_info.costs_currency = CHF
self.add_info_and_save(univ_info, self.admin)
usd = UniversitySemestersDates.objects.get(
university=EPFL)
usd.autumn_begin = datetime.strptime("17/09/2018", '%d/%m/%Y')
usd.autumn_end = datetime.strptime("29/01/2019", '%d/%m/%Y')
usd = UniversitySemestersDates.objects.get(university=EPFL)
usd.autumn_begin = datetime.strptime("17/09/2018", "%d/%m/%Y")
usd.autumn_end = datetime.strptime("29/01/2019", "%d/%m/%Y")
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)
......@@ -52,10 +52,10 @@ class LoadUniversityEx(LoadGeneric):
title="Swiss European Mobility Programme",
type="Bourse du gouvernement suisse",
currency=CHF,
frequency='s',
frequency="s",
amount_min=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.countries.add(SWITZERLAND)
......@@ -66,6 +66,6 @@ class LoadUniversityEx(LoadGeneric):
tag=ACCOMMODATION_TAG,
title="C'est compliqué de trouver un logement",
comment="Mon commentaire.",
importance_level='++'
importance_level="++",
)
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.
......@@ -5,19 +5,41 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend_app', '0002_auto_20180916_1555'),
]
dependencies = [("backend_app", "0002_auto_20180916_1555")]
operations = [
migrations.AlterField(
model_name='countryscholarship',
name='frequency',
field=models.CharField(blank=True, choices=[('w', 'week'), ('m', 'month'), ('s', 'semester'), ('y', 'year'), ('o', 'one_shot')], default='m', max_length=1, null=True),
model_name="countryscholarship",
name="frequency",
field=models.CharField(
blank=True,
choices=[
("w", "week"),
("m", "month"),
("s", "semester"),
("y", "year"),
("o", "one_shot"),
],
default="m",
max_length=1,
null=True,
),
),
migrations.AlterField(
model_name='universityscholarship',
name='frequency',
field=models.CharField(blank=True, choices=[('w', 'week'), ('m', 'month'), ('s', 'semester'), ('y', 'year'), ('o', 'one_shot')], default='m', max_length=1, null=True),
model_name="universityscholarship",
name="frequency",
field=models.CharField(
blank=True,
choices=[
("w", "week"),
("m", "month"),
("s", "semester"),
("y", "year"),
("o", "one_shot"),
],
default="m",
max_length=1,
null=True,
),
),
]
from .basicModule import BasicModule, BasicModuleSerializer, BasicModuleViewSet # noqa: F401
from .basicModule import BasicModule, BasicModuleSerializer, BasicModuleViewSet
__all__ = ["BasicModule", "BasicModuleSerializer", "BasicModuleViewSet"]
from django.db import models
from backend_app.models.abstract.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from backend_app.models.abstract.my_model import (
MyModelVersionned,
MyModelVersionnedSerializer,
MyModelVersionnedViewSet,
)
from backend_app.fields import JSONField
from backend_app.validators.tag import validate_content_against_config
from backend_app.validators.tag.tags_config import USEFULL_LINKS_CONFIG
IMPORTANCE_LEVEL = (
('-', 'normal'),
('+', 'important'),
('++', 'IMPORTANT')
)
IMPORTANCE_LEVEL = (("-", "normal"), ("+", "important"), ("++", "IMPORTANT"))
class BasicModule(MyModelVersionned):
title = models.CharField(default='', blank=True, max_length=150)
comment = models.CharField(default='', blank=True, max_length=5000)
title = models.CharField(default="", blank=True, max_length=150)
comment = models.CharField(default="", blank=True, max_length=5000)
useful_links = JSONField(default=list)
importance_level = models.CharField(
max_length=2,
choices=IMPORTANCE_LEVEL,
default='-',
max_length=2, choices=IMPORTANCE_LEVEL, default="-"
)
class Meta:
......@@ -26,16 +24,15 @@ class BasicModule(MyModelVersionned):
class BasicModuleSerializer(MyModelVersionnedSerializer):
def my_validate(self, attrs):
content = {'useful_links': attrs['useful_links']}
config = {'useful_links': USEFULL_LINKS_CONFIG}
content = {"useful_links": attrs["useful_links"]}
config = {"useful_links": USEFULL_LINKS_CONFIG}
validate_content_against_config(config, content)
return attrs
class Meta:
model = BasicModule
fields = '__all__'
fields = "__all__"
class BasicModuleViewSet(MyModelVersionnedViewSet):
......
from .myModel import MyModel # noqa: F401
from .myModelSerializer import MyModelSerializer # noqa: F401
from .myModelViewSet import MyModelViewSet # noqa: F401
from .pendingModeration import PendingModeration, PendingModerationSerializer, PendingModerationViewSet # noqa: F401
from .forTestingModeration import ForTestingModeration, ForTestingModerationSerializer, ForTestingModerationViewSet # noqa: F401
from .myModelVersionned import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet, VersionViewSet # noqa: F401
from .forTestingVersioning import ForTestingVersioning, ForTestingVersioningSerializer, ForTestingVersioningViewSet # noqa: F401
from .myModel import MyModel
from .myModelSerializer import MyModelSerializer
from .myModelViewSet import MyModelViewSet
from .pendingModeration import (
PendingModeration,
PendingModerationSerializer,
PendingModerationViewSet,
)
from .forTestingModeration import (
ForTestingModeration,
ForTestingModerationSerializer,
ForTestingModerationViewSet,
)
from .myModelVersionned import (
MyModelVersionned,
MyModelVersionnedSerializer,
MyModelVersionnedViewSet,
VersionViewSet,
)
from .forTestingVersioning import (
ForTestingVersioning,
ForTestingVersioningSerializer,
ForTestingVersioningViewSet,
)
__all__ = [
"MyModel",
"MyModelSerializer",
"MyModelViewSet",
"PendingModeration",
"PendingModerationSerializer",
"PendingModerationViewSet",
"ForTestingModeration",
"ForTestingModerationSerializer",
"ForTestingModerationViewSet",
"MyModelVersionned",
"MyModelVersionnedSerializer",
"MyModelVersionnedViewSet",
"VersionViewSet",
"ForTestingVersioning",
"ForTestingVersioningSerializer",
"ForTestingVersioningViewSet",
]
......@@ -9,6 +9,7 @@ class ForTestingModeration(MyModel):
"""
Simple model for testing purposes
"""
model_config = get_model_config("ForTestingModeration")
aaa = models.CharField(max_length=100)
......@@ -17,15 +18,17 @@ class ForTestingModerationSerializer(MyModelSerializer):
"""
Same as above
"""
class Meta:
model = ForTestingModeration
fields = '__all__'
fields = "__all__"
class ForTestingModerationViewSet(MyModelViewSet):
"""
Same as above
"""
permission_classes = get_viewset_permissions("ForTestingModerationViewSet")
serializer_class = ForTestingModerationSerializer
queryset = ForTestingModeration.objects.all()
from .myModelVersionned import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from .myModelVersionned import (
MyModelVersionned,
MyModelVersionnedSerializer,
MyModelVersionnedViewSet,
)