Commit 1ff60054 authored by Florent Chehab's avatar Florent Chehab

Bug corrected ! Youhou !

parent ca658632
Pipeline #26728 passed with stages
in 2 minutes and 9 seconds
......@@ -3,8 +3,6 @@
from django import template
import yaml
from os.path import join, realpath, dirname
from backend.utils import read_file, write_file
############
# Need to do this first so that Django template engine is working
......@@ -22,6 +20,16 @@ django.setup()
##########
def read_file(file):
with open(file, "r") as myfile:
return myfile.read()
def write_file(file, data):
with open(file, 'w') as the_file:
the_file.write(data)
def render_and_save(template_path, context, output_path):
t = template.Template(read_file(template_path))
c = template.Context({'data': context})
......
......@@ -8,6 +8,20 @@ from .myModel import MyModel
from .pendingModeration import PendingModerationSerializer
CLEANED_MY_MODEL_DATA = {
'moderated_by': None,
'moderated_on': None,
'updated_by': None,
'updated_on': None,
}
def override_data(old_data, new_data):
for key in new_data:
old_data[key] = new_data[key]
return old_data
class MyModelSerializer(serializers.ModelSerializer):
moderated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True)
......@@ -29,7 +43,6 @@ class MyModelSerializer(serializers.ModelSerializer):
def get_pending_moderation(self, obj):
if self.context['view'].action != 'list':
print(self.context['view'].action)
return PendingModerationSerializer(obj.pending_moderation, many=True, read_only=True, context=self.context).data
return None
......@@ -70,12 +83,7 @@ class MyModelSerializer(serializers.ModelSerializer):
})
def clean_validated_data(self):
self.override_validated_data({
'moderated_by': None,
'moderated_on': None,
'updated_by': None,
'updated_on': None,
})
self.override_validated_data(CLEANED_MY_MODEL_DATA)
def get_pending_models(self):
ct = ContentType.objects.get_for_model(self.Meta.model)
......@@ -107,19 +115,29 @@ class MyModelSerializer(serializers.ModelSerializer):
objs_pending_db = self.get_pending_models()
self.clean_validated_data() # Make that it is done...
data_to_save = dict()
for key in self.validated_data:
try:
# retreive the submitted data
data_to_save[key] = self.initial_data[key]
except KeyError:
pass
data_to_save = override_data(data_to_save, CLEANED_MY_MODEL_DATA)
if len(objs_pending_db) > 0:
pending_instance = objs_pending_db[0]
pending_instance.updated_on = timezone.now()
pending_instance.updated_by = user
pending_instance.new_object = self.validated_data
pending_instance.new_object = data_to_save
else:
# there is no pending instance, we have to create one
pending_instance = PendingModeration(
referenced_object=self.instance,
updated_by=user,
updated_on=timezone.now(),
new_object=self.validated_data
new_object=data_to_save
)
pending_instance.save()
return self.instance
......
......@@ -27,7 +27,6 @@ class ModerationTestCase(WithUserTestCase):
data,
format='json'
)
print(response)
self.assertEqual(response.status_code, 201)
return response
......@@ -228,7 +227,6 @@ class ModerationTestCase(WithUserTestCase):
}
api_end_point = "/api/" + \
find_api_end_point_for_viewset("UniversityDriViewSet") + "/"
print(api_end_point)
self._submit_post_test(self.authenticated_client,
data, api_end_point)
# instance = self._test_retreive_instance(data_1)
......
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