Commit 268a2e96 authored by Claude Paroz's avatar Claude Paroz

[vertimus] Use proxy models for Action classes

parent 59c2c6bf
......@@ -54,7 +54,7 @@ class PersonDetailView(DetailView):
def get_context_data(self, **kwargs):
context = super(PersonDetailView, self).get_context_data(**kwargs)
states = State.objects.filter(actiondb__person=self.object).distinct()
states = State.objects.filter(action__person=self.object).distinct()
all_languages = [(lg[0], LANG_INFO.get(lg[0], {'name_local': lg[1]})['name_local']) for lg in settings.LANGUAGES]
all_languages.sort(key=itemgetter(1))
context.update({
......
......@@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand
from people.models import Person
from teams.models import Role
from vertimus.models import ActionDbArchived
from vertimus.models import ActionArchived
from languages.views import clean_tar_files
class Command(BaseCommand):
......@@ -11,5 +11,5 @@ class Command(BaseCommand):
def handle(self, *args, **options):
Person.clean_unactivated_accounts()
Role.inactivate_unused_roles()
ActionDbArchived.clean_old_actions(365)
ActionArchived.clean_old_actions(365)
clean_tar_files()
......@@ -1497,7 +1497,7 @@ class Statistics(models.Model):
}
"""
# Import here to prevent a circular dependency
from vertimus.models import State, ActionDb
from vertimus.models import State, Action
if dtype.endswith('-part'):
dtype = dtype[:-5]
......@@ -1533,7 +1533,7 @@ class Statistics(models.Model):
vt_states_dict = dict([("%d-%d" % (vt.branch.id, vt.domain.id),vt) for vt in vt_states])
# Get comments from last action of State objects
actions = ActionDb.objects.filter(state_db__in=vt_states, comment__isnull=False).order_by('created')
actions = Action.objects.filter(state_db__in=vt_states, comment__isnull=False).order_by('created')
actions_dict = dict([(act.state_db_id, act) for act in actions])
for vt_state in vt_states_dict.values():
if vt_state.id in actions_dict:
......
......@@ -139,7 +139,7 @@ $(document).ready(function() {
{% endif %}
</div>
<a href="{{ action.person.get_absolute_url }}">{{ action.person.name }}</a>
, <strong>{{ action }}</strong>, {{ action.created|date:_("Y-m-d g:i a O") }}
, <strong>{{ action.description }}</strong>, {{ action.created|date:_("Y-m-d g:i a O") }}
</div>
<div class="vertimus_action_content">
{% if action.file %}
......
# -*- coding: utf-8 -*-
from django.contrib import admin
from vertimus.models import State, ActionDb
from vertimus.models import State, Action
class StateAdmin(admin.ModelAdmin):
raw_id_fields = ('branch', 'domain', 'person',)
class ActionDbAdmin(admin.ModelAdmin):
class ActionAdmin(admin.ModelAdmin):
list_display = ('__unicode__', 'state_db')
raw_id_fields = ('state_db', 'person')
search_fields = ('comment',)
admin.site.register(State, StateAdmin)
admin.site.register(ActionDb, ActionDbAdmin)
admin.site.register(Action, ActionAdmin)
......@@ -25,7 +25,7 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib.sites.models import Site
from languages.models import Language
from teams.models import Team
from vertimus.models import ActionDb, ActionDbArchived
from vertimus.models import Action, ActionArchived
from common.utils import imerge_sorted_by_field
class LatestActionsByLanguage(Feed):
......@@ -51,11 +51,11 @@ class LatestActionsByLanguage(Feed):
def items(self, obj):
# The Django ORM doesn't provide the UNION SQL feature :-(
# so we need to fetch twice more objects than required
actions_db = ActionDb.objects.filter(state_db__language=obj.id).select_related('state').order_by('-created')[:20]
archived_actions_db = ActionDbArchived.objects.filter(state_db__language=obj.id).select_related('state').order_by('-created')[:20]
actions = Action.objects.filter(state_db__language=obj.id).select_related('state').order_by('-created')[:20]
archived_actions = ActionArchived.objects.filter(state_db__language=obj.id).select_related('state').order_by('-created')[:20]
# islice avoid to fetch too many objects
return (action_db.get_action() for action_db in islice(imerge_sorted_by_field(actions_db, archived_actions_db, '-created'), 20))
return islice(imerge_sorted_by_field(actions, archived_actions, '-created'), 20)
def item_link(self, item):
link = urlresolvers.reverse('vertimus_by_names',
......@@ -95,13 +95,11 @@ class LatestActionsByTeam(Feed):
def items(self, obj):
# The Django ORM doesn't provide the UNION SQL feature :-(
# so we need to fetch twice more objects than required
actions_db = ActionDb.objects.filter(state_db__language__team=obj.id).select_related('state').order_by('-created')[:20]
archived_actions_db = ActionDbArchived.objects.filter(state_db__language__team=obj.id).select_related('state').order_by('-created')[:20]
actions = Action.objects.filter(state_db__language__team=obj.id).select_related('state').order_by('-created')[:20]
archived_actions = ActionArchived.objects.filter(state_db__language__team=obj.id).select_related('state').order_by('-created')[:20]
a = imerge_sorted_by_field(actions_db, archived_actions_db, '-created')
b = islice(imerge_sorted_by_field(actions_db, archived_actions_db, '-created'), 20)
# islice avoid to fetch too many objects
return (action_db.get_action() for action_db in islice(imerge_sorted_by_field(actions_db, archived_actions_db, '-created'), 20))
return islice(imerge_sorted_by_field(actions, archived_actions, '-created'), 20)
def item_link(self, item):
link = urlresolvers.reverse('vertimus_by_names',
......
......@@ -23,7 +23,7 @@ import os
from django import forms
from django.utils.translation import ugettext_lazy as _
from vertimus.models import ActionAbstract
from vertimus.models import Action
from stats.utils import po_file_stats
class ActionWidget(forms.Select):
......@@ -69,9 +69,9 @@ class ActionForm(forms.Form):
action_code = cleaned_data.get('action')
if action_code is None:
raise forms.ValidationError(_("Invalid action. Someone probably posted another action just before you."))
action = ActionAbstract.new_by_name(action_code)
comment = cleaned_data.get('comment')
file = cleaned_data.get('file')
action = Action.new_by_name(action_code, comment=comment, file=file)
if action.comment_is_required and not comment:
raise forms.ValidationError(_("A comment is needed for this action."))
......
This diff is collapsed.
......@@ -190,8 +190,8 @@ class VertimusTest(TeamsAndRolesTests):
state = StateNone(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('WC')
state.apply_action(action, self.pt, "Hi!", None)
action = Action.new_by_name('WC', person=self.pt, comment="Hi!")
action.apply_on(state)
# Test that submitting a comment without text generates a validation error
form = ActionForm([('WC', u'Write a comment')], QueryDict('action=WC&comment='))
self.assertTrue("A comment is needed" in str(form.errors))
......@@ -200,8 +200,8 @@ class VertimusTest(TeamsAndRolesTests):
state = StateNone(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pt, "Reserved!", None)
action = Action.new_by_name('RT', person=self.pt, comment="Reserved!")
action.apply_on(state)
self.assertTrue(isinstance(state, StateTranslating))
def test_action_ut(self):
......@@ -216,8 +216,8 @@ class VertimusTest(TeamsAndRolesTests):
test_file = ContentFile('test content')
test_file.name = 'mytestfile.po'
action = ActionAbstract.new_by_name('UT')
state.apply_action(action, self.pt, "Done by translator.", test_file)
action = Action.new_by_name('UT', person=self.pt, comment="Done by translator.", file=test_file)
action.apply_on(state)
self.assertTrue(isinstance(state, StateTranslated))
# Mail sent to mailing list
self.assertEquals(len(mail.outbox), 1)
......@@ -232,8 +232,8 @@ class VertimusTest(TeamsAndRolesTests):
state = StateTranslated(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('RP')
state.apply_action(action, self.pr, "Reserved by a reviewer!")
action = Action.new_by_name('RP', person=self.pr, comment="Reserved by a reviewer!")
action.apply_on(state)
self.assertTrue(isinstance(state, StateProofreading))
def test_action_up(self):
......@@ -243,24 +243,24 @@ class VertimusTest(TeamsAndRolesTests):
test_file = ContentFile('test content')
test_file.name = 'mytestfile.po'
action = ActionAbstract.new_by_name('UP')
state.apply_action(action, self.pr, "Done.", test_file)
action = Action.new_by_name('UP', person=self.pr, comment="Done.", file=test_file)
action.apply_on(state)
self.assertTrue(isinstance(state, StateProofread))
def test_action_tc(self):
state = StateProofread(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('TC')
state.apply_action(action, self.pr, "Ready!")
action = Action.new_by_name('TC', person=self.pr, comment="Ready!")
action.apply_on(state)
self.assertTrue(isinstance(state, StateToCommit))
def test_action_rc(self):
state = StateToCommit(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('RC')
state.apply_action(action, self.pc, "This work is mine!")
action = Action.new_by_name('RC', person=self.pc, comment="This work is mine!")
action.apply_on(state)
self.assertTrue(isinstance(state, StateCommitting))
def test_action_ic(self):
......@@ -271,84 +271,85 @@ class VertimusTest(TeamsAndRolesTests):
test_file = ContentFile('test content')
test_file.name = 'mytestfile.po'
action = ActionAbstract.new_by_name('UP')
state.apply_action(action, self.pr, "Done.", test_file)
action = Action.new_by_name('UP', person=self.pr, comment="Done.", file=test_file)
action.apply_on(state)
file_path = os.path.join(settings.MEDIA_ROOT, action.file.name)
self.assertTrue(os.access(file_path, os.W_OK))
action = ActionAbstract.new_by_name('TC')
state.apply_action(action, self.pc, "To commit.")
action = Action.new_by_name('TC', person=self.pc, comment="To commit.")
action.apply_on(state)
action = ActionAbstract.new_by_name('RC')
state.apply_action(action, self.pc, "Reserved commit.")
action = Action.new_by_name('RC', person=self.pc, comment="Reserved commit.")
action.apply_on(state)
action = ActionAbstract.new_by_name('IC')
state.apply_action(action, self.pc, "Committed.")
action = Action.new_by_name('IC', person=self.pc, comment="Committed.")
action.apply_on(state)
self.assertTrue(not os.access(file_path, os.F_OK), "%s not deleted" % file_path)
# Remove test file
action_db_archived = ActionDbArchived.objects.get(comment="Done.")
filename_archived = os.path.join(settings.MEDIA_ROOT, action_db_archived.file.name)
action_db_archived.delete()
action_archived = ActionArchived.objects.get(comment="Done.")
filename_archived = os.path.join(settings.MEDIA_ROOT, action_archived.file.name)
action_archived.delete()
self.assertTrue(not os.access(filename_archived, os.F_OK), "%s not deleted" % filename_archived)
def test_action_tr(self):
state = StateTranslated(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('TR')
state.apply_action(action, self.pc, "Bad work :-/")
action = Action.new_by_name('TR', person=self.pc, comment="Bad work :-/")
action.apply_on(state)
self.assertTrue(isinstance(state, StateToReview))
def test_action_ba(self):
def test_action_aa(self):
state = StateCommitted(branch=self.b, domain=self.d, language=self.l, person=self.pr)
state.save()
action = ActionAbstract.new_by_name('AA')
state.apply_action(action, self.pc, comment="I don't want to disappear :)")
action = Action.new_by_name('AA', person=self.pc, comment="I don't want to disappear :)")
action.apply_on(state)
state = State.objects.get(branch=self.b, domain=self.d, language=self.l)
self.assertTrue(isinstance(state, StateNone))
self.assertEquals(state.action_set.count(), 0)
def test_action_undo(self):
state = StateNone(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pt, "Reserved!")
action = Action.new_by_name('RT', person=self.pt, comment="Reserved!")
action.apply_on(state)
action = ActionAbstract.new_by_name('UNDO')
state.apply_action(action, self.pt, "Ooops! I don't want to do that. Sorry.")
action = Action.new_by_name('UNDO', person=self.pt, comment="Ooops! I don't want to do that. Sorry.")
action.apply_on(state)
self.assertEqual(state.name, 'None')
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pt, "Translating")
action = Action.new_by_name('RT', person=self.pt, comment="Translating")
action.apply_on(state)
action = ActionAbstract.new_by_name('UT')
state.apply_action(action, self.pt, "Translated")
action = Action.new_by_name('UT', person=self.pt, comment="Translated")
action.apply_on(state)
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pt, "Reserved!")
action = Action.new_by_name('RT', person=self.pt, comment="Reserved!")
action.apply_on(state)
action = ActionAbstract.new_by_name('UNDO')
state.apply_action(action, self.pt, "Ooops! I don't want to do that. Sorry.")
action = Action.new_by_name('UNDO', person=self.pt, comment="Ooops! I don't want to do that. Sorry.")
action.apply_on(state)
self.assertEqual(state.name, 'Translated')
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pt, "Translating 1")
action = Action.new_by_name('RT', person=self.pt, comment="Translating 1")
action.apply_on(state)
action = ActionAbstract.new_by_name('UNDO')
state.apply_action(action, self.pt, "Undo 1")
action = Action.new_by_name('UNDO', person=self.pt, comment="Undo 1")
action.apply_on(state)
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pt, "Translating 2")
action = Action.new_by_name('RT', person=self.pt, comment="Translating 2")
action.apply_on(state)
action = ActionAbstract.new_by_name('UNDO')
state.apply_action(action, self.pt, "Undo 2")
action = Action.new_by_name('UNDO', person=self.pt, comment="Undo 2")
action.apply_on(state)
self.assertEqual(state.name, 'Translated')
......@@ -388,29 +389,29 @@ class VertimusTest(TeamsAndRolesTests):
state = StateNone(branch=self.b, domain=self.d, language=self.l)
state.save()
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pr, "Reserved!")
action = Action.new_by_name('RT', person=self.pr, comment="Reserved!")
action.apply_on(state)
action = ActionAbstract.new_by_name('UNDO')
state.apply_action(action, self.pr, "Ooops! I don't want to do that. Sorry.")
action = Action.new_by_name('UNDO', person=self.pr, comment="Ooops! I don't want to do that. Sorry.")
action.apply_on(state)
action = ActionAbstract.new_by_name('RT')
state.apply_action(action, self.pr, "Translating")
action = Action.new_by_name('RT', person=self.pr, comment="Translating")
action.apply_on(state)
action = ActionAbstract.new_by_name('UT')
state.apply_action(action, self.pr, "Translated")
action = Action.new_by_name('UT', person=self.pr, comment="Translated")
action.apply_on(state)
action = ActionAbstract.new_by_name('RP')
state.apply_action(action, self.pr, "Proofreading")
action = Action.new_by_name('RP', person=self.pr, comment="Proofreading")
action.apply_on(state)
action = ActionAbstract.new_by_name('UNDO')
state.apply_action(action, self.pr, "Ooops! I don't want to do that. Sorry.")
action = Action.new_by_name('UNDO', person=self.pr, comment="Ooops! I don't want to do that. Sorry.")
action.apply_on(state)
actions_db = ActionDb.objects.filter(state_db__id=state.id).exclude(name='WC').order_by('-id')
actions_db = Action.objects.filter(state_db__id=state.id).exclude(name='WC').order_by('-id')
# So the last action is UNDO
self.assert_(isinstance(actions_db[0].get_action(), ActionUNDO))
self.assert_(isinstance(actions_db[0], ActionUNDO))
# Here be dragons! A call to len() workaround the Django/MySQL bug!
len(actions_db)
self.assert_(isinstance(actions_db[0].get_action(), ActionUNDO))
self.assert_(isinstance(actions_db[0], ActionUNDO))
......@@ -27,7 +27,7 @@ from django.utils.translation import ugettext as _
from stats.models import Statistics, Module, Branch, Domain, Language
from stats.utils import is_po_reduced
from vertimus.models import State, ActionDb, ActionDbArchived, ActionAbstract
from vertimus.models import State, Action, ActionArchived
from vertimus.forms import ActionForm
def vertimus_by_stats_id(request, stats_id, lang_id):
......@@ -80,10 +80,10 @@ def vertimus(request, branch, domain, language, stats=None, level="0"):
if level == 0:
# Current actions
action_history = ActionDb.get_action_history(state)
action_history = Action.get_action_history(state=state)
else:
sequence = state.get_action_sequence_from_level(level)
action_history = ActionDbArchived.get_action_history(sequence)
action_history = ActionArchived.get_action_history(sequence=sequence)
# Get the sequence of the grandparent to know if exists a previous action
# history
......@@ -105,9 +105,9 @@ def vertimus(request, branch, domain, language, stats=None, level="0"):
action = action_form.cleaned_data['action']
comment = action_form.cleaned_data['comment']
action = ActionAbstract.new_by_name(action)
state.apply_action(action, person, comment,
request.FILES.get('file', None))
action = Action.new_by_name(action, person=person, comment=comment,
file=request.FILES.get('file', None))
action.apply_on(state)
return HttpResponseRedirect(
urlresolvers.reverse('vertimus_by_names',
......@@ -143,10 +143,10 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
"""Show a diff between current action po file and previous file"""
import difflib
if int(level) != 0:
ActionDbReal = ActionDbArchived
ActionReal = ActionArchived
else:
ActionDbReal = ActionDb
action_1 = get_object_or_404(ActionDbReal, pk=action_id_1).get_action()
ActionReal = Action
action_1 = get_object_or_404(ActionReal, pk=action_id_1)
state = action_1.state
file_path_1 = action_1.merged_file()['path'] or action_1.file.path
......@@ -161,7 +161,7 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
'date': action_1.created }
if action_id_2 not in (None, "0"):
# 1) id_2 specified in URL
action_2 = get_object_or_404(ActionDbReal, pk=action_id_2).get_action()
action_2 = get_object_or_404(ActionReal, pk=action_id_2)
file_path_2 = action_2.merged_file()['path'] or action_2.file.path
descr_2 = _("Uploaded file by %(name)s on %(date)s") % { 'name': action_2.person.name,
'date': action_2.created }
......@@ -204,11 +204,11 @@ def latest_uploaded_po(request, module_name, branch_name, domain_name, locale_na
branch = get_object_or_404(Branch, module__name=module_name, name=branch_name)
domain = get_object_or_404(Domain, module__name=module_name, name=domain_name)
lang = get_object_or_404(Language, locale=locale_name)
latest_upload = ActionDb.objects.filter(state_db__branch=branch,
state_db__domain=domain,
state_db__language=lang,
file__endswith=".po").order_by('-created')[:1]
latest_upload = Action.objects.filter(state_db__branch=branch,
state_db__domain=domain,
state_db__language=lang,
file__endswith=".po").order_by('-created')[:1]
if not latest_upload:
raise Http404
merged_file = latest_upload[0].get_action().merged_file()
merged_file = latest_upload[0].merged_file()
return HttpResponseRedirect(merged_file['url'])
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