Skip to content

Commit

Permalink
Merge pull request #1197 from scieloorg/beta
Browse files Browse the repository at this point in the history
Incorporação de códigos estáveis
  • Loading branch information
gustavofonseca committed Aug 19, 2015
2 parents 72449a4 + 100f4c5 commit 61539d1
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 215 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ webassets==0.7.1
yuicompressor
jsonfield
django-tastypie==0.9.16
-e git+git://github.com/scieloorg/django-htmlmin.git#egg=django-htmlmin
django-htmlmin==0.7.0
-e git+git://github.com/scieloorg/django-cache-machine.git#egg=django-cache-machine
packtools
Celery
Expand Down
4 changes: 2 additions & 2 deletions scielomanager/editorialmanager/tests/tests_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class IssueBoardMessageTests(TestCase):
ACTIONS = [
ACTIONS = [
'issue_add_no_replicated_board',
'issue_add_replicated_board',
]
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_each_action_with_disable_notifications_for_editor(self):


class BoardMembersMessageTests(TestCase):
ACTIONS = [
ACTIONS = [
'board_add_member',
'board_edit_member',
'board_delete_member',
Expand Down
30 changes: 1 addition & 29 deletions scielomanager/journalmanager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,33 +730,6 @@ def has_issues(self, issues):

return issues_to_test.issubset(bound_issues)

def reorder_issues(self, new_order, publication_year, volume=None):
"""
Make persistent the ordering received as a list of ``pk``,
to all the issues in a given ``publication_year`` and ``volume``.
The lenght of ``new_order`` must match with the subset of
issues by ``publication_year`` and ``volume``.
"""
filters = {'publication_year': publication_year}
if volume:
filters['volume'] = volume

issues = self.issue_set.filter(**filters)

issues_count = issues.count()
new_order_count = len(new_order)

if new_order_count != issues_count:
raise ValueError('new_order lenght does not match. %s:%s' % (new_order_count, issues_count))

with transaction.commit_on_success():
for i, pk in enumerate(new_order):
order = i + 1
issue = issues.get(pk=pk)
issue.order = order
issue.save()

@property
def scielo_pid(self):
"""
Expand Down Expand Up @@ -1050,8 +1023,7 @@ class Issue(caching.base.CachingMixin, models.Model):

class Meta:
ordering = ('created', 'id')
permissions = (("list_issue", "Can list Issues"),
("reorder_issue", "Can Reorder Issues"))
permissions = (("list_issue", "Can list Issues"), )

@property
def scielo_pid(self):
Expand Down
46 changes: 36 additions & 10 deletions scielomanager/journalmanager/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
import threading
import contextlib
import datetime
import operator
from copy import deepcopy

from lxml import isoschematron, etree
from django.db.models import Q
from django.db import IntegrityError
from celery.utils.log import get_task_logger

from scielomanager.celery import app
from scielomanager import connectors
from . import models


logger = logging.getLogger(__name__)
logger = get_task_logger(__name__)


ARTICLE_SAVE_MUTEX = threading.Lock()
Expand Down Expand Up @@ -94,7 +96,7 @@ def submit_to_elasticsearch(article_pk):
do Elasticsearch.
"""
try:
article = models.Article.objects.get(pk=article_pk)
article = models.Article.nocacheobjects.get(pk=article_pk)
except models.Article.DoesNotExist:
logger.error('Cannot find Article with pk: %s. Skipping the submission to elasticsearch.', article_pk)
return None
Expand All @@ -121,13 +123,37 @@ def link_article_to_journal(article_pk):
return None

try:
journal = models.Journal.objects.get(
Q(print_issn=article.issn_ppub) | Q(eletronic_issn=article.issn_epub))
query_params = []
if article.issn_ppub:
query_params.append(Q(print_issn=article.issn_ppub))

if article.issn_epub:
query_params.append(Q(eletronic_issn=article.issn_epub))

if not query_params:
logger.error('Missing attributes issn_ppub and issn_epub in Article wit pk: %s.', article_pk)
return None

query_expr = reduce(operator.or_, query_params)
journal = models.Journal.objects.get(query_expr)

except models.Journal.DoesNotExist:
# Pode ser que os ISSNs do XML estejam invertidos...
try:
journal = models.Journal.objects.get(
Q(print_issn=article.issn_epub) | Q(eletronic_issn=article.issn_ppub))
query_params = []
if article.issn_ppub:
query_params.append(Q(eletronic_issn=article.issn_ppub))

if article.issn_epub:
query_params.append(Q(print_issn=article.issn_epub))

if not query_params:
logger.error('Missing attributes issn_ppub and issn_epub in Article wit pk: %s.', article_pk)
return None

query_expr = reduce(operator.or_, query_params)
journal = models.Journal.objects.get(query_expr)

except models.Journal.DoesNotExist:
logger.info('Cannot find parent Journal for Article with pk: %s. Skipping the linking task.', article_pk)
return None
Expand All @@ -147,7 +173,7 @@ def link_article_to_issue(article_pk):
""" Tenta associar o artigo ao seu número.
"""
try:
article = models.Article.objects.get(pk=article_pk, issue=None, is_aop=False)
article = models.Article.nocacheobjects.get(pk=article_pk, issue=None, is_aop=False)
except models.Article.DoesNotExist:
logger.info('Cannot find unlinked Article with pk: %s. Skipping the linking task.', article_pk)
return None
Expand Down Expand Up @@ -179,7 +205,7 @@ def link_article_to_issue(article_pk):
def process_orphan_articles():
""" Tenta associar os artigos órfãos com periódicos e fascículos.
"""
orphans = models.Article.objects.filter(issue=None)
orphans = models.Article.nocacheobjects.only('pk', 'journal').filter(issue=None)

for orphan in orphans:
if orphan.journal is None:
Expand All @@ -192,7 +218,7 @@ def process_orphan_articles():
def process_dirty_articles():
""" Task (periódica) que garanta a indexação dos artigos sujos.
"""
dirties = models.Article.objects.filter(es_is_dirty=True)
dirties = models.Article.nocacheobjects.only('pk').filter(es_is_dirty=True)

for dirty in dirties:
submit_to_elasticsearch.delay(dirty.pk)
Expand Down Expand Up @@ -262,7 +288,7 @@ def rebuild_articles_domain_key():
a qualquer momento.
https://github.com/scieloorg/scielo-manager/issues/1183
"""
articles = models.Article.objects.all()
articles = models.Article.nocacheobjects.only('pk').all()

for article in articles:
rebuild_article_domain_key.delay(article.pk)
Expand Down
16 changes: 16 additions & 0 deletions scielomanager/journalmanager/tests/modelfactories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
import os

import factory
import datetime
Expand All @@ -7,6 +8,13 @@
from django.contrib.auth.models import Group


_HERE = os.path.dirname(os.path.abspath(__file__))


with open(os.path.join(_HERE, 'xml_samples', '0034-8910-rsp-48-2-0216.xml')) as xml_file:
SAMPLE_XML = xml_file.read()


class UserFactory(factory.Factory):
FACTORY_FOR = models.User

Expand Down Expand Up @@ -85,6 +93,7 @@ class JournalFactory(factory.Factory):
frequency = u'Q'
scielo_issn = u'print'
print_issn = factory.Sequence(lambda n: '1234-%04d' % int(n))
eletronic_issn = factory.Sequence(lambda n: '4321-%04d' % int(n))
init_vol = u'1'
title = u'ABCD. Arquivos Brasileiros de Cirurgia Digestiva (São Paulo)'
title_iso = u'ABCD. Arquivos B. de C. D. (São Paulo)'
Expand Down Expand Up @@ -212,3 +221,10 @@ class PressReleaseArticleFactory(factory.Factory):

press_release = factory.SubFactory(RegularPressReleaseFactory)
article_pid = factory.Sequence(lambda n: 'S0102-311X201300030000%s' % n)


class ArticleFactory(factory.Factory):
FACTORY_FOR = models.Article

xml = SAMPLE_XML

53 changes: 0 additions & 53 deletions scielomanager/journalmanager/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,59 +446,6 @@ def test_journal_has_issues_must_be_false(self):

self.assertFalse(journal.has_issues(issues))

def test_issues_reordering(self):
journal = JournalFactory.create()
issues = []
for i in range(5):
issue = IssueFactory.create(volume=9, publication_year=2012)
journal.issue_set.add(issue)

issues.append(issue.pk)

issues[2], issues[3] = issues[3], issues[2] # reordering
expected_order = issues

journal.reorder_issues(expected_order, volume=9, publication_year=2012)

ordered_issues = [issue.pk for issue in journal.issue_set.order_by('order')]

self.assertEqual(expected_order, ordered_issues)

def test_issues_reordering_must_accept_pks_as_string(self):
journal = JournalFactory.create()
issues = []
for i in range(5):
issue = IssueFactory.create(volume=9, publication_year=2012)
journal.issue_set.add(issue)

issues.append(issue.pk)

issues[2], issues[3] = issues[3], issues[2] # reordering
expected_order = [str(i_pk) for i_pk in issues]

journal.reorder_issues(expected_order, volume=9, publication_year=2012)

ordered_issues = [str(issue.pk) for issue in journal.issue_set.order_by('order')]

self.assertEqual(expected_order, ordered_issues)

def test_issues_reordering_lenght_must_match(self):
journal = JournalFactory.create()
issues = []
for i in range(5):
issue = IssueFactory.create(volume=9, publication_year=2012)
journal.issue_set.add(issue)

issues.append(issue.pk)

expected_order = [1, 2, 4]

self.assertRaises(
ValueError,
lambda: journal.reorder_issues(expected_order,
volume=9,
publication_year=2012))

def test_scielo_pid_when_print(self):
journal = JournalFactory.create(scielo_issn=u'print',
print_issn='1234-4321',
Expand Down
67 changes: 0 additions & 67 deletions scielomanager/journalmanager/tests/tests_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,73 +382,6 @@ def test_user_access_issue_list_without_itens(self):

self.assertTrue('There are no items.' in response.body)

def test_user_reordering_issues_without_permissions(self):
"""
Asserts that unpriviledged users can't reorder Issues
"""
perm_issue_list = _makePermission(
perm='list_issue',
model='issue',
app_label='journalmanager')
self.user.user_permissions.add(perm_issue_list)

response = self.app.get(reverse('issue.reorder.ajax', args=[self.journal.pk]), user=self.user).follow()

response.mustcontain('not authorized to access')
self.assertTemplateUsed(response, 'accounts/unauthorized.html')

def test_user_reordering_unknown_issues(self):
"""
The server must respond a http 500 error code when
it is requested to reorder issues that do not match
the journal.
"""
perm1 = _makePermission(
perm='list_issue',
model='issue',
app_label='journalmanager')
perm2 = _makePermission(
perm='reorder_issue',
model='issue',
app_label='journalmanager')
self.user.user_permissions.add(perm1)
self.user.user_permissions.add(perm2)

params = 'numbers=num%5B%5D%3D8036%26num%5B%5D%3D8035&issues_set=numbers-2005%7CNone'

response = self.app.get(
reverse('issue.reorder.ajax', args=[self.journal.pk]) + '?' + params,
headers={'x-requested-with': 'XMLHttpRequest'},
user=self.user,
expect_errors=True
)

self.assertEqual(response.status_code, 500)

def test_user_reordering_without_passing_params(self):
"""
The server must respond a http 500 code and do nothing.
"""
perm1 = _makePermission(
perm='list_issue',
model='issue',
app_label='journalmanager')
perm2 = _makePermission(
perm='reorder_issue',
model='issue',
app_label='journalmanager')
self.user.user_permissions.add(perm1)
self.user.user_permissions.add(perm2)

response = self.app.get(
reverse('issue.reorder.ajax', args=[self.journal.pk]),
headers={'x-requested-with': 'XMLHttpRequest'},
user=self.user,
expect_errors=True
)

self.assertEqual(response.status_code, 500)


class SectionLookupForTranslationsTests(WebTest):
def setUp(self):
Expand Down
Loading

0 comments on commit 61539d1

Please sign in to comment.