Commit f1d6cc0c authored by Florent Chehab's avatar Florent Chehab

Merge branch 'backend_simplification' into 'master'

Backend simplification

See merge request chehabfl/outgoing_rex!22
parents 1e57c4ac 7c38befd
[run]
omit =
*migrations*
tests/*
*/tests/*
source = .
......@@ -5,3 +5,4 @@ exclude =
env/*
node_modules/*
backend/urls.py
backend/admin.py
......@@ -11,3 +11,4 @@ htmlcov
.vscode
.pytest_cache
/backend/urls.py
/backend/admin.py
......@@ -6,11 +6,12 @@
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/__pycache__":true,
"**/*.pyc":true,
"htmlcov":false,
"env":true,
".coverage":true
"**/__pycache__": true,
"**/*.pyc": true,
"htmlcov": false,
"env": true,
".coverage": true,
".pytest_cache": true,
},
"cSpell.language": "en,fr-FR,fr",
"python.linting.flake8Enabled": true
......
from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
from backend.models.location import Country, City
from backend.models.location import CountryCulture
from backend.models.location import CountryHealthInsurance
from backend.models.location import CountryOtherInsurance
from backend.models.location import CountryTransport
from backend.models.location import CountryTourism
from backend.models.location import CountryOtherStuff
from backend.models.location import CountryVisaAdministrative
from backend.models.location import CountryPhoto
from backend.models.location import CountryScholarship
from backend.models.location import CountryDri
from backend.models.location import CityTransport
from backend.models.location import CityTourism
from backend.models.location import CityOtherStuff
from backend.models.university import University
from backend.models.university import Campus
from backend.models.university import UniversityScholarship
from backend.models.university import UniversityInfo
from backend.models.university import UniversitySemestersDates
from backend.models.university import UniversityInsurance
from backend.models.university import UniversityCourses
from backend.models.university import UniversityCulture
from backend.models.university import UniversityOtherStuff
from backend.models.university import UniversitySpecialOffer
from backend.models.university import UniversityPhoto
from backend.models.university import UniversityDri
from backend.models.university import CampusTransport
from backend.models.university import CampusAccommodation
from backend.models.university import CampusCultureAndStudentLife
from backend.models.university import CampusOtherStuff
from backend.models.module import Currency
from backend.models.otherCore import Department
from backend.models.otherCore import Offer
from backend.models.otherCore import Semester
from backend.models.otherCore import Specialty
from backend.models.user import Recommendation
from backend.models.user import RecommendationList
from backend.models.user import PreviousDeparture
from backend.models.user import PreviousDepartureFeedback
from backend.models.user import UserData
from backend.models.my_model import PendingModeration
CLASSIC_MODELS = [
Country,
City,
Currency,
University,
Department,
Offer,
Semester,
Specialty,
Recommendation,
RecommendationList,
PreviousDeparture,
PreviousDepartureFeedback,
UserData,
PendingModeration
]
VERSIONNED_MODELS = [
Campus,
UniversityScholarship,
UniversityInfo,
UniversitySemestersDates,
UniversityInsurance,
UniversityCulture,
UniversityCourses,
UniversityOtherStuff,
UniversitySpecialOffer,
UniversityPhoto,
CountryCulture,
CountryHealthInsurance,
CountryOtherInsurance,
CountryTransport,
CountryTourism,
CountryOtherStuff,
CountryVisaAdministrative,
CountryPhoto,
CountryScholarship,
CityTransport,
CityTourism,
CityOtherStuff,
CampusTransport,
CampusAccommodation,
CampusCultureAndStudentLife,
CampusOtherStuff,
CountryDri,
UniversityDri,
]
for model in CLASSIC_MODELS:
admin.site.register(model)
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))
......@@ -8,6 +8,35 @@
import_location: location
api_end_point: city
- model: Tag
viewset: TagViewSet
import_location: tag
api_end_point: tag
- model: UniversityTaggedItem
viewset: UniversityTaggedItemViewSet
import_location: university
api_end_point: university_more_tmp
versionned: true
- model: CampusTaggedItem
viewset: CampusTaggedItemViewSet
import_location: university
api_end_point: campus_more_tmp
versionned: true
- model: CountryTaggedItem
viewset: CountryTaggedItemViewSet
import_location: location
api_end_point: country_more_tmp
versionned: true
- model: CityTaggedItem
viewset: CityTaggedItemViewSet
import_location: location
api_end_point: city_more_tmp
versionned: true
- model: Currency
viewset: CurrencyViewSet
import_location: module
......@@ -15,96 +44,35 @@
- model: Department
viewset: DepartmentViewSet
import_location: otherCore
import_location: other_core
api_end_point: other/department
- model: Specialty
viewset: SpecialtyViewSet
import_location: otherCore
import_location: other_core
api_end_point: other/specialty
- model: Semester
viewset: SemesterViewSet
import_location: otherCore
import_location: other_core
api_end_point: other/semester
- model: Offer
viewset: OfferViewSet
import_location: otherCore
import_location: other_core
api_end_point: other/offer
- model: CountryCulture
viewset: CountryCultureViewSet
import_location: location
api_end_point: country_more/culture
- model: CountryHealthInsurance
viewset: CountryHealthInsuranceViewSet
import_location: location
api_end_point: country_more/health_insurance
- model: CountryOtherInsurance
viewset: CountryOtherInsuranceViewSet
import_location: location
api_end_point: country_more/other_insurance
- model: CountryTransport
viewset: CountryTransportViewSet
import_location: location
api_end_point: country_more/transport
- model: CountryTourism
viewset: CountryTourismViewSet
import_location: location
api_end_point: country_more/tourism
- model: CountryOtherStuff
viewset: CountryOtherStuffViewSet
import_location: location
api_end_point: country_more/other_stuff
- model: CountryVisaAdministrative
viewset: CountryVisaAdministrativeViewSet
import_location: location
api_end_point: country_more/visa_administrative
- model: CountryPhoto
viewset: CountryPhotoViewSet
import_location: location
api_end_point: country_more/photos
- model: CountryScholarship
viewset: CountryScholarshipViewSet
import_location: location
api_end_point: country_more/scholarship
versionned: true
- model: CountryDri
viewset: CountryDriViewSet
import_location: location
api_end_point: country_more/dri
- model: CityTransport
viewset: CityTransportViewSet
import_location: location
api_end_point: city_more/transport
- model: CityTourism
viewset: CityTourismViewSet
import_location: location
api_end_point: city_more/tourism
- model: CityOtherStuff
viewset: CityOtherStuffViewSet
import_location: location
api_end_point: city_more/other_stuff
- model: CityPhoto
viewset: CityPhotoViewSet
import_location: location
api_end_point: city_more/photos
versionned: true
- model: University
viewset: UniversityViewSet
......@@ -115,8 +83,9 @@
viewset: CampusViewSet
import_location: university
api_end_point: campus
versionned: true
- model: Campus
- model: null
viewset: MainCampusViewSet
import_location: university
api_end_point: main_campus
......@@ -126,79 +95,33 @@
viewset: UniversityScholarshipViewSet
import_location: university
api_end_point: university_more/scholarship
versionned: true
- model: UniversityInfo
viewset: UniversityInfoViewSet
import_location: university
api_end_point: university_more/info
versionned: true
- model: UniversitySemestersDates
viewset: UniversitySemestersDatesViewSet
import_location: university
api_end_point: university_more/semesters_dates
- model: UniversityInsurance
viewset: UniversityInsuranceViewSet
import_location: university
api_end_point: university_more/insurance
- model: UniversityCourses
viewset: UniversityCoursesViewSet
import_location: university
api_end_point: university_more/courses
- model: UniversityCulture
viewset: UniversityCultureViewSet
import_location: university
api_end_point: university_more/culture
- model: UniversityPhoto
viewset: UniversityPhotoViewSet
import_location: university
api_end_point: university_more/photo
- model: UniversityOtherStuff
viewset: UniversityOtherStuffViewSet
import_location: university
api_end_point: university_more/other_stuff
- model: UniversitySpecialOffer
viewset: UniversitySpecialOfferViewSet
import_location: university
api_end_point: university_more/special_offer
versionned: true
- model: UniversityDri
viewset: UniversityDriViewSet
import_location: university
api_end_point: university_more/dri
versionned: true
- model: UniversityModules
- model: null
viewset: UniversityModulesViewSet
import_location: university
api_end_point: university_more/all
api_attr: (?P<univ_id>[0-9]+)
api_name: university_details
- model: CampusTransport
viewset: CampusTransportViewSet
import_location: university
api_end_point: campus_more/transport
- model: CampusAccommodation
viewset: CampusAccommodationViewSet
import_location: university
api_end_point: campus_more/accomodation
- model: CampusCultureAndStudentLife
viewset: CampusCultureAndStudentLifeViewSet
import_location: university
api_end_point: campus_more/culture_and_sl
- model: CampusOtherStuff
viewset: CampusOtherStuffViewSet
import_location: university
api_end_point: campus_more/other_tuff
- model: Recommendation
viewset: RecommendationViewSet
import_location: user
......@@ -236,6 +159,7 @@
api_end_point: version
api_attr: (?P<content_type_id>[0-9]+)/(?P<object_pk>[0-9A-Za-z]+)
api_name: versions-list
ignore_in_admin: true
- model: ForTestingModeration
viewset: ForTestingModerationViewSet
......@@ -248,10 +172,4 @@
import_location: my_model
api_end_point: test/versioning
requires_testing: true
# requires_testing
# viewset
# import_location
# api_end_point
# api_attr
\ No newline at end of file
# versionned: true don't put it here, it is manually handled with @register decorator
#####
# This python file is used to generate js files for redux
import os
from django import template
import yaml
from os.path import join, realpath, dirname
############
# Need to do this first so that Django template engine is working
......@@ -30,37 +30,39 @@ def write_file(file, data):
the_file.write(data)
current_dir = os.path.dirname(os.path.realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = os.path.realpath(current_dir + "/../")
# if not os.path.exists(saving_dir):
# os.makedirs(saving_dir)
templates = [
'urls',
]
# API_BASE = "http://127.0.0.1:8000/api/"
contexts = yaml.load(read_file(os.path.join(current_dir, 'api_config.yml')))
# def convert(name):
# s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
# return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
def render_and_save(template_path, context, output_path):
t = template.Template(read_file(template_path))
c = template.Context({'data': context})
output = t.render(c)
write_file(output_path, output)
# for c in contexts:
# c['NAME'] = convert(c['name'])
# # print(contexts)
current_dir = dirname(realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = realpath(current_dir + "/../")
for filename in templates:
t = template.Template(
read_file(os.path.join(templates_dir, filename + '.tpl')))
c = template.Context({'data': contexts})
output = t.render(c)
# API_BASE = "http://127.0.0.1:8000/api/"
write_file(os.path.join(saving_dir, filename + '.py'), output)
api_config = yaml.load(read_file(join(current_dir, 'api_config.yml')))
# Render urls.py
template_path = join(templates_dir, 'urls.tpl')
output_path = join(saving_dir, 'urls.py')
render_and_save(template_path, api_config, output_path)
# Render admin.py
data = []
for obj in api_config:
if 'model' in obj and obj['model']:
if 'requires_testing' in obj and obj['requires_testing']:
continue # we don't want testing models to be register in admin
if 'ignore_in_admin' in obj and obj['ignore_in_admin']:
continue
if 'versionned' not in obj:
obj['versionned'] = False
data.append(obj)
template_path = join(templates_dir, 'admin.tpl')
output_path = join(saving_dir, 'admin.py')
render_and_save(template_path, data, output_path)
{% autoescape off %}
# WARNING
# THIS FILE HAS BEEN AUTOMATICALLY GENERATED
# WITH /backend/generate/generate_all.py
# MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
# THIS WARNING DOESN'T APPLY TO .tpl FILES...
from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
{% spaceless %}
{% for model in data %}
from backend.models.{{model.import_location}} import {{model.model}}
{% endfor %}
{% endspaceless %}
CLASSIC_MODELS = [
{% for model in data %}{% if not model.versionned %}
{{model.model}},
{% endif %}{% endfor %}
]
VERSIONNED_MODELS = [
{% for model in data %}{% if model.versionned %}
{{model.model}},
{% endif %}{% endfor %}
]
for model in CLASSIC_MODELS:
admin.site.register(model)
try:
model.get_serializer()
raise Exception("CLASSIX MODEL SHOULDN'T have this method")
except AttributeError:
pass
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))
{% endautoescape %}
[
{
"name": "photos",
"config": {
"photos": {
"type": "photos",
"required": true
}
}
}
]
\ No newline at end of file
from .loading_scripts import LoadAdminUser
from .loading_scripts import LoadCountries
from .loading_scripts import LoadUniversities
from .loading_scripts import LoadTags
def load_all():
admin = LoadAdminUser().get()
LoadCountries(admin).load()
LoadUniversities(admin).load()
LoadTags(admin).load()
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 backend.models.tag import Tag
import os
import json
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')
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.save()
self.add_info(t, self.admin)
This diff is collapsed.
# Generated by Django 2.0.3 on 2018-08-24 07:10
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='campus',
name='university',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='univ_campus', to='backend.University'),
),
]
# Generated by Django 2.0.3 on 2018-09-01 08:00
import django.contrib.postgres.fields.jsonb
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AddField(