Commit b822f1d8 authored by Florent Chehab's avatar Florent Chehab

Merge branch 'db_agnostic' into 'master'

Db agnostic

See merge request chehabfl/outgoing_rex!30
parents 61493e5a b6f5aa64
......@@ -12,3 +12,5 @@ htmlcov
.pytest_cache
/backend/urls.py
/backend/admin.py
database.db
database.db-journal
\ No newline at end of file
......@@ -3,13 +3,21 @@ from django.contrib.auth.models import User
class LoadAdminUser(object):
def __init__(self):
self.admin = User.objects.get_or_create(
username="admin",
defaults={
'email': 'null@null.fr',
'password': 'admin',
'is_staff': True
})[0]
"""
Creating admin user by creating a user and setting is_staff is True
didn't enable access to the admin system for an unkown reason.
Need to use create_superuser.
"""
user = User.objects.filter(username="admin")
if len(user) > 0:
self.admin = user[0]
else:
User.objects.create_superuser(
username="admin",
email='null@null.fr',
password='admin',
)
self.admin = User.objects.filter(username="admin")[0]
def get(self):
return self.admin
This diff is collapsed.
# Generated by Django 2.0.3 on 2018-09-02 15:16
import backend.models.my_model.myModel
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='campus',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='campustaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='city',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='citytaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='country',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='countrydri',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='countryscholarship',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='countrytaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='currency',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='department',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='fortestingmoderation',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='fortestingversioning',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='offer',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='previousdeparture',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='previousdeparturefeedback',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='recommendation',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='recommendationlist',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='semester',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='specialty',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='tag',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='university',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universitydri',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universityinfo',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universityscholarship',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universitysemestersdates',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='universitytaggeditem',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
migrations.AlterField(
model_name='userdata',
name='obj_moderation_level',
field=models.SmallIntegerField(default=0, validators=[django.core.validators.MinValueValidator(0), backend.models.my_model.myModel.validate_obj_model_lv]),
),
]
# Generated by Django 2.0.3 on 2018-09-03 07:53
import backend.models.university.university
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20180902_1716'),
]
operations = [
migrations.AddField(
model_name='university',
name='utc_id',
field=models.IntegerField(default=1, unique=True),
preserve_default=False,
),
migrations.AlterField(
model_name='university',
name='logo',
field=models.URLField(blank=True, null=True, validators=[backend.models.university.university.validate_extension_django]),
),
migrations.AlterField(
model_name='university',
name='website',
field=models.URLField(blank=True, null=True),
),
]
from django.db import models
from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.models.tag.tagged_item_validation import validate_content_against_config
from backend.models.tag.tags_config import USEFULL_LINKS_CONFIG
......@@ -8,7 +8,7 @@ from backend.models.tag.tags_config import USEFULL_LINKS_CONFIG
class BasicModule(MyModelVersionned):
comment = models.TextField(blank=True)
usefull_links = JSONField(blank=True, default=list)
usefull_links = JSONField(default=list)
class Meta:
abstract = True
......
......@@ -7,6 +7,7 @@ from .myModel import MyModel
from .pendingModeration import PendingModerationSerializer
from backend.utils import get_user_level
from backend.permissions import is_moderation_required
from backend.models.tools import MySerializerWithJSON
CLEANED_MY_MODEL_DATA = {
'moderated_by': None,
......@@ -22,7 +23,7 @@ def override_data(old_data, new_data):
return old_data
class MyModelSerializer(serializers.ModelSerializer):
class MyModelSerializer(MySerializerWithJSON):
moderated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True)
updated_on = serializers.DateTimeField(
......
......@@ -7,6 +7,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.serializers.base import DeserializationError
from django.core import serializers as djangoSerializers
from backend.utils import get_viewset_permissions
from backend.models.tools import MySerializerWithJSON
class MyModelVersionned(MyModel):
......@@ -51,7 +52,7 @@ class MyModelVersionnedViewSet(MyModelViewSet):
serializer_class = MyModelVersionnedSerializer
class VersionSerializer(serializers.ModelSerializer):
class VersionSerializer(MySerializerWithJSON):
data = serializers.SerializerMethodField()
def get_data(self, obj):
......
......@@ -2,9 +2,10 @@ from django.db import models
from rest_framework import serializers, viewsets
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from django.contrib.auth.models import User
from backend.utils import get_viewset_permissions, get_model_config
from backend.models.tools import MySerializerWithJSON
class PendingModeration(models.Model):
......@@ -23,7 +24,7 @@ class PendingModeration(models.Model):
unique_together = ('content_type', 'object_id')
class PendingModerationSerializer(serializers.ModelSerializer):
class PendingModerationSerializer(MySerializerWithJSON):
content_type = serializers.CharField(read_only=True)
object_id = serializers.CharField(read_only=True)
......
from django.db import models
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.utils import get_viewset_permissions, get_model_config
......@@ -11,7 +11,7 @@ class Tag(MyModel):
model_config = get_model_config("Tag")
name = models.CharField(max_length=100, unique=True)
config = JSONField(blank=True, default=dict)
config = JSONField(default=dict)
class TagSerializer(MyModelSerializer):
......
......@@ -2,7 +2,7 @@ from django.db import models
from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from .tag import Tag
from .tagged_item_validation import tagged_item_validation
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
class TaggedItem(MyModelVersionned):
......
from .DictModeViewSet import DictModeViewSet # noqa: F401
from .jSONField import JSONField # noqa: F401
from .mySerializerWithJSON import MySerializerWithJSON # noqa: F401
# taken from : https://github.com/devkral/django-simple-jsonfield
from django.core.exceptions import ValidationError
from django.db import models
import json
class JSONField(models.TextField):
__qualname__ = "JSONField"
__name__ = "JSONField"
""" JSON field implementation on top of django textfield """
def to_dict(self, value):
""" convert json string to python dictionary """
try:
return json.loads(value)
except json.decoder.JSONDecodeError as e:
raise ValidationError(
message=str(e),
code="json_error"
)
def to_json(self, value):
""" convert python dictionary to json string """
return json.dumps(value)
def from_db_value(self, value, expression, connection):
""" convert string from db to python dictionary """
if value is None:
return value
return self.to_dict(value)
def to_python(self, value):
""" convert model input value to python dictionary """
if isinstance(value, (dict, list)):
return value
if value is None:
return value
return self.to_dict(value)
def get_prep_value(self, value):
"""convert python dictionary to string before writing to db"""
return self.to_json(value)
from .jSONField import JSONField
from rest_framework import serializers
field_mapping = serializers.ModelSerializer.serializer_field_mapping
field_mapping[JSONField] = serializers.JSONField
class MySerializerWithJSON(serializers.ModelSerializer):
"""
Simple class to add support for custom JSONField support
"""
serializer_field_mapping = field_mapping
......@@ -3,7 +3,7 @@ from backend.models.other_core.specialty import Specialty
from backend.models.other_core.semester import Semester
from backend.models.university import University
from django.contrib.auth.models import User
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.utils import get_model_config, get_viewset_permissions
......
......@@ -3,7 +3,7 @@ from django.core.validators import MaxValueValidator
from backend.models.user import UserRestrictedModule, UserRestrictedModuleSerializer, UserRestrictedModuleViewSet
from backend.models.user import PreviousDeparture
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.utils import get_model_config, get_viewset_permissions
......
from django.db import models
from rest_framework import serializers
from backend.models.university import University
from django.contrib.postgres.fields import JSONField
from backend.models.tools import JSONField
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from django.contrib.auth.models import User
from backend.utils import get_viewset_permissions, get_model_config
......
......@@ -222,7 +222,6 @@ class ModerationTestCase(WithUserTestCase):
data = {
"comment": "",
"usefull_links": [],
"custom_content": {},
"university": [c.pk]
}
api_end_point = "/api/" + \
......
......@@ -5,12 +5,39 @@ Installation
Petit guide pour installer proprement cette application `django` et pouvoir contribuer.
### Base de données
L'application est « agnostique » au type de base de donnée employée. Alors qu'il fallait au départ forcément PostgreSQL pour tirer partie des fonctionnalités de stockage des documents JSON, ils sont
aujourd'hui stockés sous forme de texte car aucune requête n'aurait besoin de les parcourir ; ainsi, seul des champs par défaut de django sont employés.
### postgreSQL
#### SQlite
Pour utiliser SQlite comme SGBD, il suffit de commenter le bloc qui concerne Postgresql dans le fichier de configuration visible ici : `./general/settings.py` :
#### Installation
```python
## POSTGRESQL
# If bellow fails, your need to add DATABASE_URL and ENV to your virutalenv
locals()['DATABASES']['default'] = dj_database_url.config(
conn_max_age=django_heroku.MAX_CONN_AGE, ssl_require=not dev_env)
```
et de décommenter celui-ci :
```python
## Sqlite
# PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(PROJECT_DIR, '../database.db'),
# }
# }
```
La base de donnée sera alors stockée à la racine du projet dans le fichier `database.db`.
Si vous avez choisi cette option, vous pouvez sauter la partie sur postgreSQL.
#### postgreSQL
##### Installation
L'installation de `postgreSQL` est peut être le plus gros challenge de cette partie. Pour ce faire, utiliser ~~Google~~ DuckDuckGo.
L'installation de `postgreSQL` est un plus gros challenge. Pour ce faire, utiliser ~~Google~~ DuckDuckGo.
Vous devez faire en sorte que `postgreSQL` soit accessible en local, c'est suffisant :
......@@ -29,7 +56,7 @@ host all all 127.0.0.1/32 trust
**Les contributions sont les bienvenues pour compléter cette partie d'installation/configuration de base.**
#### Initialisation
##### Initialisation
Pour un fonctionnement simplifié avec ce projet il faut procéder aux changement suivants.
......
......@@ -166,10 +166,24 @@ if dev_env:
ALLOWED_HOSTS = []
SECRET_KEY = '0a1kbtt%as4624u*9+r+c%j1tlq&dc08==c3w604(4igc+t6tw'
# DATABASE
# After you have read the documentation, uncomment the
# block with the database system you would like to use.
# POSTGRESQL
# If bellow fails, your need to add DATABASE_URL and ENV to your virutalenv
locals()['DATABASES']['default'] = dj_database_url.config(
conn_max_age=django_heroku.MAX_CONN_AGE, ssl_require=not dev_env)
# # Sqlite
# PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(PROJECT_DIR, '../database.db'),
# }
# }
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
......
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