Skip to content

Commit

Permalink
Rend le code des tutos plus pythonique
Browse files Browse the repository at this point in the history
et j'en profite pour régler le problème de l'import json.

Pour ce qui est des tests, j'ai commencé à travailler à les factoriser un peu pour qu'ils soient plus lisibles et donc plus maintenables et fiables. Pour l'instant j'ai factorisé que le tearDown mais j'ajouterai des choses après.
  • Loading branch information
artragis committed Nov 2, 2017
1 parent 85c0f0a commit 58755f8
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 177 deletions.
2 changes: 1 addition & 1 deletion doc/source/back-end/contents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ Si vous souhaitez implémenter votre propre convertisseur, voici l'algorithme ut
.. sourcecode:: python

with open(_file, "r") as json_file:
data = json_reader.load(json_file)
data = json_handler.load(json_file)
_type = "TUTORIAL"
if "type" not in data:
_type = "ARTICLE"
Expand Down
10 changes: 10 additions & 0 deletions zds/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
try:
import ujson as json_handler
except ImportError:
try:
import simplejson as json_handler
except ImportError:
import json as json_handler
import logging
logging.debug('json is loaded, module is %s', json_handler.__name__) # this allows to know which one we loaded
# and avoid pep8 warning.
4 changes: 2 additions & 2 deletions zds/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def form_valid(self, form):

@method_decorator(login_required)
@method_decorator(can_write_and_read_now)
def dispatch(self, *args, **kwargs):
return super(AssocSubscribeView, self).dispatch(*args, **kwargs)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)

def get_success_url(self):
return reverse('pages-assoc-subscribe')
Expand Down
8 changes: 4 additions & 4 deletions zds/searchv2/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8

import os
import json
from zds import json_handler
import shutil
import datetime

Expand Down Expand Up @@ -152,7 +152,7 @@ def test_get_similar_topics(self):
# 1. Should not get any result
result = self.client.get(reverse('search:similar') + '?q=est', follow=False)
self.assertEqual(result.status_code, 200)
content = json.loads(result.content.decode('utf-8'))
content = json_handler.loads(result.content.decode('utf-8'))
self.assertEqual(len(content['results']), 0)

# index
Expand All @@ -165,13 +165,13 @@ def test_get_similar_topics(self):
# 2. Should get exactly one result
result = self.client.get(reverse('search:similar') + '?q=mange', follow=False)
self.assertEqual(result.status_code, 200)
content = json.loads(result.content.decode('utf-8'))
content = json_handler.loads(result.content.decode('utf-8'))
self.assertEqual(len(content['results']), 1)

# 2. Should get exactly two results
result = self.client.get(reverse('search:similar') + '?q=Clem', follow=False)
self.assertEqual(result.status_code, 200)
content = json.loads(result.content.decode('utf-8'))
content = json_handler.loads(result.content.decode('utf-8'))
self.assertEqual(len(content['results']), 2)

def test_hidden_post_are_not_result(self):
Expand Down
4 changes: 2 additions & 2 deletions zds/searchv2/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
import json
from zds import json_handler
import operator

from elasticsearch_dsl import Search
Expand Down Expand Up @@ -68,7 +68,7 @@ def get(self, request, *args, **kwargs):
results.append(result)

data = {'results': results}
return HttpResponse(json.dumps(data), content_type='application/json')
return HttpResponse(json_handler.dumps(data), content_type='application/json')


class SearchView(ZdSPagingListView):
Expand Down
5 changes: 2 additions & 3 deletions zds/tutorialv2/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
'Image: ![SVG qui existe pas](example.com/test.svg)\n\n' \
'Image: ![GIF qui existe](http://upload.wikimedia.org/wikipedia/commons/2/27/AnimatedStar.gif)\n\n' \
'Image: ![GIF qui existe pas](example.com/test.gif)\n\n' \
'Image: ![Image locale qui existe](fixtures/image_test.jpg)\n\n' \
'Image: ![Image locale qui existe pas](does-not-exist/test.png)\n\n' \
'Image: ![Bonus: image bizarre](https://s.qwant.com/thumbr/?u=http%3A%2F%2Fwww.blogoergosum.com%2Fwp-content%2F' \
'uploads%2F2010%2F02%2Fwikipedia-logo.jpg&h=338&w=600)\n\n' \
Expand All @@ -37,8 +36,8 @@ class PublishableContentFactory(factory.DjangoModelFactory):
class Meta:
model = PublishableContent

title = factory.Sequence(lambda n: 'Mon contenu No{0}'.format(n))
description = factory.Sequence(lambda n: 'Description du contenu No{0}'.format(n))
title = factory.Sequence('Mon contenu No{0}'.format)
description = factory.Sequence('Description du contenu No{0}'.format)
type = 'TUTORIAL'
creation_date = datetime.now()
pubdate = datetime.now()
Expand Down
2 changes: 1 addition & 1 deletion zds/tutorialv2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class ImportNewContentForm(ImportContentForm):
)

def __init__(self, *args, **kwargs):
super(ImportContentForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

self.helper = FormHelper()
self.helper.form_class = 'content-wrapper'
Expand Down
11 changes: 2 additions & 9 deletions zds/tutorialv2/management/commands/upgrade_manifest_to_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
from zds.utils.models import Licence
from uuslug import slugify
import os

try:
import ujson as json_reader
except ImportError:
try:
import simplejson as json_reader
except ImportError:
import json as json_reader
from zds import json_handler


class Command(BaseCommand):
Expand All @@ -23,7 +16,7 @@ def handle(self, *args, **options):
_file = options['manifest_path']
if os.path.isfile(_file) and _file[-5:] == '.json':
with open(_file, 'r') as json_file:
data = json_reader.load(json_file)
data = json_handler.load(json_file)
_type = 'TUTORIAL'
if data['type'].lower() == 'article':
_type = 'ARTICLE'
Expand Down
13 changes: 3 additions & 10 deletions zds/tutorialv2/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
from datetime import datetime

from zds.tutorialv2.models.mixins import TemplatableContentModelMixin, OnlineLinkableContentMixin

try:
import ujson as json_reader
except ImportError:
try:
import simplejson as json_reader
except:
import json as json_reader
from zds import json_handler

from math import ceil
import shutil
Expand Down Expand Up @@ -358,7 +351,7 @@ def load_version(self, sha=None, public=None):
raise NotAPublicVersion

with open(os.path.join(path, 'manifest.json'), 'r', encoding='utf-8') as manifest:
json = json_reader.loads(manifest.read())
json = json_handler.loads(manifest.read())
versioned = get_content_from_json(
json,
public.sha_public,
Expand All @@ -377,7 +370,7 @@ def load_version(self, sha=None, public=None):
repo = Repo(path)
data = get_blob(repo.commit(sha).tree, 'manifest.json')
try:
json = json_reader.loads(data)
json = json_handler.loads(data)
except ValueError:
raise BadManifestError(
_('Une erreur est survenue lors de la lecture du manifest.json, est-ce du JSON ?'))
Expand Down
8 changes: 3 additions & 5 deletions zds/tutorialv2/models/versioned.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# coding: utf-8

import json as json_writer
from zds import json_handler
from git import Repo
import os
import shutil
Expand Down Expand Up @@ -1199,7 +1197,7 @@ def get_json(self):
:rtype: str
"""
dct = export_content(self)
data = json_writer.dumps(dct, indent=4, ensure_ascii=False)
data = json_handler.dumps(dct, indent=4, ensure_ascii=False)
return data

def dump_json(self, path=None):
Expand Down Expand Up @@ -1290,7 +1288,7 @@ def __init__(self, current_version, _type, title, slug):
:param slug: slug of the content
"""

Container.__init__(self, title, slug)
super().__init__(current_version, _type, title, slug)
self.current_version = current_version
self.type = _type
self.PUBLIC = True # this is a public version
Expand Down
18 changes: 18 additions & 0 deletions zds/tutorialv2/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.conf import settings
import os
import shutil


class TutorialTestMixin:
def clean_media_dir(self):
if os.path.isdir(self.overridden_zds_app['content']['repo_private_path']):
shutil.rmtree(self.overridden_zds_app['content']['repo_private_path'])
if os.path.isdir(self.overridden_zds_app['content']['repo_public_path']):
shutil.rmtree(self.overridden_zds_app['content']['repo_public_path'])
if os.path.isdir(self.overridden_zds_app['content']['extra_content_watchdog_dir']):
shutil.rmtree(self.overridden_zds_app['content']['extra_content_watchdog_dir'])
if os.path.isdir(settings.MEDIA_ROOT):
shutil.rmtree(settings.MEDIA_ROOT)

def tearDown(self):
self.clean_media_dir()
28 changes: 5 additions & 23 deletions zds/tutorialv2/tests/tests_feeds.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# coding: utf-8

import os
import shutil

from django.conf import settings
from django.test import TestCase
Expand All @@ -16,6 +13,7 @@
from zds.tutorialv2.factories import LicenceFactory, SubCategoryFactory, PublishableContentFactory, ContainerFactory, \
ExtractFactory
from zds.tutorialv2.publication_utils import publish_content
from zds.tutorialv2.tests import TutorialTestMixin
from copy import deepcopy

overridden_zds_app = deepcopy(settings.ZDS_APP)
Expand All @@ -25,10 +23,10 @@

@override_settings(MEDIA_ROOT=os.path.join(settings.BASE_DIR, 'media-test'))
@override_settings(ZDS_APP=overridden_zds_app)
class LastTutorialsFeedRSSTest(TestCase):
class LastTutorialsFeedRSSTest(TestCase, TutorialTestMixin):

def setUp(self):

settings.overridden_zds_app = overridden_zds_app
# don't build PDF to speed up the tests
overridden_zds_app['content']['build_pdf_when_published'] = False

Expand Down Expand Up @@ -139,20 +137,12 @@ def test_get_item_link(self):
ret = self.tutofeed.item_link(item=tuto)
self.assertEqual(ret, ref)

def tearDown(self):
if os.path.isdir(overridden_zds_app['content']['repo_private_path']):
shutil.rmtree(overridden_zds_app['content']['repo_private_path'])
if os.path.isdir(overridden_zds_app['content']['repo_public_path']):
shutil.rmtree(overridden_zds_app['content']['repo_public_path'])
if os.path.isdir(settings.MEDIA_ROOT):
shutil.rmtree(settings.MEDIA_ROOT)


@override_settings(ZDS_APP=overridden_zds_app)
class LastArticlesFeedRSSTest(TestCase):
class LastArticlesFeedRSSTest(TestCase, TutorialTestMixin):

def setUp(self):

settings.overridden_zds_app = overridden_zds_app
# don't build PDF to speed up the tests
overridden_zds_app['content']['build_pdf_when_published'] = False

Expand Down Expand Up @@ -260,11 +250,3 @@ def test_get_item_link(self):
article = list(self.articlefeed.items())[0]
ret = self.articlefeed.item_link(item=article)
self.assertEqual(ret, ref)

def tearDown(self):
if os.path.isdir(overridden_zds_app['content']['repo_private_path']):
shutil.rmtree(overridden_zds_app['content']['repo_private_path'])
if os.path.isdir(overridden_zds_app['content']['repo_public_path']):
shutil.rmtree(overridden_zds_app['content']['repo_public_path'])
if os.path.isdir(settings.MEDIA_ROOT):
shutil.rmtree(settings.MEDIA_ROOT)
16 changes: 3 additions & 13 deletions zds/tutorialv2/tests/tests_lists.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding: utf-8
from django.contrib.auth.models import Group

import os
import shutil
import datetime
from django.conf import settings
from django.test import TestCase
Expand All @@ -12,6 +10,7 @@
from zds.member.factories import ProfileFactory, StaffProfileFactory, UserFactory
from zds.tutorialv2.factories import PublishableContentFactory, ContainerFactory, ExtractFactory, LicenceFactory, \
SubCategoryFactory, PublishedContentFactory, ValidationFactory
from zds.tutorialv2.tests import TutorialTestMixin
from zds.gallery.factories import UserGalleryFactory
from zds.forum.factories import ForumFactory, CategoryFactory
from copy import deepcopy
Expand All @@ -24,9 +23,9 @@
@override_settings(MEDIA_ROOT=os.path.join(settings.BASE_DIR, 'media-test'))
@override_settings(ZDS_APP=overridden_zds_app)
@override_settings(ES_ENABLED=False)
class ContentTests(TestCase):
class ContentTests(TestCase, TutorialTestMixin):
def setUp(self):

self.overridden_zds_app = overridden_zds_app
# don't build PDF to speed up the tests
overridden_zds_app['content']['build_pdf_when_published'] = False

Expand Down Expand Up @@ -202,12 +201,3 @@ def test_validation_list(self):
self.assertEqual(len(validations), 1) # 1 content with this category

self.assertEqual(validations[0].content, article_reserved) # the right content

def tearDown(self):

if os.path.isdir(overridden_zds_app['content']['repo_private_path']):
shutil.rmtree(overridden_zds_app['content']['repo_private_path'])
if os.path.isdir(overridden_zds_app['content']['repo_public_path']):
shutil.rmtree(overridden_zds_app['content']['repo_public_path'])
if os.path.isdir(settings.MEDIA_ROOT):
shutil.rmtree(settings.MEDIA_ROOT)
28 changes: 10 additions & 18 deletions zds/tutorialv2/tests/tests_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# coding: utf-8
import unittest

from django.core.urlresolvers import reverse
from datetime import datetime, timedelta
import os
import shutil

from django.conf import settings
from django.test import TestCase
Expand All @@ -18,6 +16,7 @@
from zds.gallery.factories import UserGalleryFactory
from zds.tutorialv2.models.database import PublishableContent, PublishedContent
from zds.tutorialv2.publication_utils import publish_content
from zds.tutorialv2.tests import TutorialTestMixin
from zds.utils.models import Tag
from django.template.defaultfilters import date
from copy import deepcopy
Expand All @@ -30,10 +29,10 @@
@override_settings(MEDIA_ROOT=os.path.join(settings.BASE_DIR, 'media-test'))
@override_settings(ZDS_APP=overridden_zds_app)
@override_settings(ES_ENABLED=False)
class ContentTests(TestCase):
class ContentTests(TestCase, TutorialTestMixin):

def setUp(self):

self.overridden_zds_app = overridden_zds_app
# don't build PDF to speed up the tests
overridden_zds_app['content']['build_pdf_when_published'] = False

Expand Down Expand Up @@ -125,13 +124,14 @@ def test_ensure_unique_slug_2(self):
slugs = [new_version.children[-1].slug]

for i in range(0, 2): # will add 3 new container
version = versioned.repo_add_container(title, random, random)
new_version = self.tuto.load_version(sha=version)
self.assertEqual(new_version.children[-1].slug, versioned.children[-1].slug)
self.assertTrue(new_version.children[-1].slug not in slugs) # slug is different
self.assertTrue(versioned.children[-1].slug not in slugs)
with self.subTest('subcontainer {}'.format(i)):
version = versioned.repo_add_container(title, random, random)
new_version = self.tuto.load_version(sha=version)
self.assertEqual(new_version.children[-1].slug, versioned.children[-1].slug)
self.assertTrue(new_version.children[-1].slug not in slugs) # slug is different
self.assertTrue(versioned.children[-1].slug not in slugs)

slugs.append(new_version.children[-1].slug)
slugs.append(new_version.children[-1].slug)

# add extracts
extract_title = "On va changer de titre (parce qu'on sais jamais) !"
Expand Down Expand Up @@ -577,11 +577,3 @@ def test_ensure_gallery(self):
content.save()
content.ensure_author_gallery()
self.assertEqual(UserGallery.objects.filter(gallery__pk=content.gallery.pk).count(), content.authors.count())

def tearDown(self):
if os.path.isdir(overridden_zds_app['content']['repo_private_path']):
shutil.rmtree(overridden_zds_app['content']['repo_private_path'])
if os.path.isdir(overridden_zds_app['content']['repo_public_path']):
shutil.rmtree(overridden_zds_app['content']['repo_public_path'])
if os.path.isdir(settings.MEDIA_ROOT):
shutil.rmtree(settings.MEDIA_ROOT)
Loading

0 comments on commit 58755f8

Please sign in to comment.