Skip to content

Commit

Permalink
Merge pull request #3073 from pierre-24/betafix_zep_12_commit_autors2
Browse files Browse the repository at this point in the history
[betafix 15.9] Corrige les erreurs sur `get_commit_author()`
  • Loading branch information
Eskimon committed Oct 14, 2015
2 parents 563a39b + 792ae4c commit 2b12d7d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
4 changes: 2 additions & 2 deletions zds/tutorialv2/management/commands/migrate_to_zep12.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from zds.tutorialv2.models.models_database import PublishableContent, ContentReaction, ContentRead, PublishedContent,\
Validation

from zds.tutorialv2.utils import publish_content
from zds.tutorialv2.utils import publish_content, get_commit_author
from django.core.management.base import BaseCommand
from django.db import transaction
from zds.gallery.models import Gallery, UserGallery, Image
Expand Down Expand Up @@ -218,7 +218,7 @@ def copy_and_clean_repo(path_from, path_to):

if len(to_delete) != 0:
old_repo.index.remove(to_delete)
sha = old_repo.index.commit('Nettoyage pré-migratoire')
sha = old_repo.index.commit('Nettoyage pré-migratoire', **get_commit_author())

# then clone it to new repo
old_repo.clone(path_to)
Expand Down
40 changes: 39 additions & 1 deletion zds/tutorialv2/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.test.utils import override_settings
from zds.forum.models import Topic
from zds.settings import BASE_DIR
from django.core.urlresolvers import reverse

from zds.member.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.factories import PublishableContentFactory, ContainerFactory, LicenceFactory, ExtractFactory, \
Expand All @@ -18,7 +19,7 @@
from zds.tutorialv2.models.models_versioned import Container
from zds.tutorialv2.utils import get_target_tagged_tree_for_container, publish_content, unpublish_content, \
get_target_tagged_tree_for_extract, retrieve_and_update_images_links, last_participation_is_old, \
InvalidSlugError, BadManifestError, get_content_from_json
InvalidSlugError, BadManifestError, get_content_from_json, get_commit_author
from zds.tutorialv2.models.models_database import PublishableContent, PublishedContent, ContentReaction, ContentRead
from django.core.management import call_command
from zds.tutorial.factories import BigTutorialFactory, MiniTutorialFactory, PublishedMiniTutorial, NoteFactory, \
Expand Down Expand Up @@ -636,6 +637,43 @@ def testParseBadManifest(self):
get_content_from_json, dictionary, None, '',
max_title_len=PublishableContent._meta.get_field('title').max_length)

def test_get_commit_author(self):
"""Ensure the behavior of `get_commit_author()` :
- `git.Actor` use the pk of the bot account when no one is connected
- `git.Actor` use the pk (and the email) of the connected account when available
(Implementation of `git.Actor` is there :
https://github.com/gitpython-developers/GitPython/blob/master/git/util.py#L312)
"""

# 1. With user connected
self.assertEqual(
self.client.login(
username=self.user_author.username,
password='hostel77'),
True)

# go to whatever page, if not, `get_current_user()` does not work at all
result = self.client.get(reverse('zds.pages.views.index'))
self.assertEqual(result.status_code, 200)

actor = get_commit_author()
self.assertEqual(actor['committer'].name, str(self.user_author.pk))
self.assertEqual(actor['author'].name, str(self.user_author.pk))
self.assertEqual(actor['committer'].email, self.user_author.email)
self.assertEqual(actor['author'].email, self.user_author.email)

# 2. Without connected user
self.client.logout()

# as above ...
result = self.client.get(reverse('zds.pages.views.index'))
self.assertEqual(result.status_code, 200)

actor = get_commit_author()
self.assertEqual(actor['committer'].name, str(self.mas.pk))
self.assertEqual(actor['author'].name, str(self.mas.pk))

def tearDown(self):
if os.path.isdir(settings.ZDS_APP['content']['repo_private_path']):
shutil.rmtree(settings.ZDS_APP['content']['repo_private_path'])
Expand Down
15 changes: 11 additions & 4 deletions zds/tutorialv2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from zds.utils import slugify as old_slugify
from zds.utils.models import Licence
from zds.utils.templatetags.emarkdown import emarkdown
from zds.member.models import User


def all_is_string_appart_from_children(dict_representation):
Expand Down Expand Up @@ -1012,26 +1013,32 @@ def init_new_repo(db_object, introduction_text, conclusion_text, commit_message=


def get_commit_author():
"""get a dictionary that represent the commit author with ``author`` and ``comitter`` key
"""get a dictionary that represent the commit author with ``author`` and ``comitter`` key. If there is no users,
bot account pk is used.
:return: correctly formatted commit author for ``repo.index.commit()``
:rtype: dict
"""
user = get_current_user()

if user:
if user and user.is_authenticated():
aut_user = str(user.pk)
aut_email = None

if hasattr(user, 'email'):
aut_email = user.email

else:
aut_user = ZDS_APP['member']['bot_account']
try:
aut_user = str(User.objects.filter(username=settings.ZDS_APP['member']['bot_account']).first().pk)
except AttributeError:
aut_user = '0'

aut_email = None

if aut_email is None or aut_email.strip() == "":
aut_email = "inconnu@{}".format(settings.ZDS_APP['site']['dns'])
aut_email = _(u"inconnu@{}").format(settings.ZDS_APP['site']['dns'])

return {'author': Actor(aut_user, aut_email), 'committer': Actor(aut_user, aut_email)}


Expand Down

0 comments on commit 2b12d7d

Please sign in to comment.