<commit_before># Amara, universalsubtitles.org
#
# Copyright (C) 2014 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

"""
SubtitleWorkflow classes for old-style teams
"""

from django.conf import settings
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from localeurl.utils import universal_url
from messages.models import Message
from subtitles.signals import subtitles_published
from teams.models import Task, BillingRecord
from teams.permissions import can_add_version, can_assign_task
from teams.workflows.notes import TeamEditorNotes
from teams.workflows.subtitleworkflows import TeamSubtitlesWorkflow
from utils import send_templated_email
from utils import translation
from utils.text import fmt
from videos.tasks import video_changed_tasks
import subtitles.workflows

def _publish_subtitles_if_needed(subtitle_language, version):
    public_tip = subtitle_language.get_tip(public=True)
    if public_tip:
        subtitles_published.send(subtitle_language, version=version)
        BillingRecord.objects.insert_record(public_tip)

def _complete_task(user, video, subtitle_language, saved_version, approved):
    team_video = video.get_team_video()
    task = (team_video.task_set
            .incomplete_review_or_approve()
            .get(language=subtitle_language.language_code))
    if task.assignee is None and can_assign_task(task, user):
        task.assignee = user
    elif task.assignee != user:
        raise ValueError("Task not assigned to user")
    task.new_subtitle_version = subtitle_language.get_tip()
    task.approved = approved
    task.complete()
    if saved_version is None:
        if saved_version is None:
            version_id = None
        else:
            version_id = saved_version.id
            video_changed_tasks.delay(team_video.video_id, version_id)

class Complete(subtitles.workflows.Action):
    """Used when the initial transcriber/translator completes their work """
    name = 'complete'
    label = ugettext_lazy('Complete')
    in_progress_text = ugettext_lazy('Saving')
    visual_class = 'endorse'
    subtitle_visibility = 'private'
    complete = True

    def perform(self, user, video, subtitle_language, saved_version):
        try:
            task = (video.get_team_video().task_set
                    .incomplete_subtitle_or_translate()
                    .filter(language=subtitle_language.language_code).get())
        except Task.DoesNotExist:
            # post publish edit, no task is available
            return
        else:
            task.complete()
        _publish_subtitles_if_needed(subtitle_language, saved_version)

class Approve(subtitles.workflows.Action):
    name = 'approve'
    label = ugettext_lazy('Approve')
    in_progress_text = ugettext_lazy('Approving')
    visual_class = 'endorse'
    subtitle_visibility = 'private'
    complete = True

    def perform(self, user, video, subtitle_language, saved_version):
        _complete_task(user, video, subtitle_language, saved_version,
                       Task.APPROVED_IDS['Approved'])
        _publish_subtitles_if_needed(subtitle_language, saved_version)

class SendBack(subtitles.workflows.Action):
    name = 'send-back'
    label = ugettext_lazy('Send Back')
    in_progress_text = ugettext_lazy('Sending back')
    visual_class = 'send-back'
    subtitle_visibility = 'private'
    complete = False

    def perform(self, user, video, subtitle_language, saved_version):
        _complete_task(user, video, subtitle_language, saved_version,
                       Task.APPROVED_IDS['Rejected'])
        _publish_subtitles_if_needed(subtitle_language, saved_version)

class TaskSaveDraft(subtitles.workflows.SaveDraft):
    subtitle_visibility = 'private'

class TaskTeamEditorNotes(TeamEditorNotes):
    def __init__(self, team_video, language_code):
        super(TaskTeamEditorNotes, self).__init__(team_video.team,
                                                  team_video.video,
                                                  language_code)
        self.team_video = team_video

    def post(self, user, body):
        note = super(TaskTeamEditorNotes, self).post(user, body)
        email_to = [u for u in self.all_assignees() if u != note.user]
        self.send_messages(note, email_to)
        return note

    def all_assignees(self):
        task_qs = (self.team_video.task_set
                   .filter(language=self.language_code,
                           assignee__isnull=False)
                   .select_related('assignee'))
        return set(task.assignee for task in task_qs)

    def send_messages(self, note, user_list):
        subject = fmt(
            _(u'%(user)s added a note while editing %(title)s'),
            user=unicode(note.user), title=self.video.title_display())

        tasks_url = universal_url('teams:team_tasks', kwargs={
            'slug': self.team.slug,
        })
        filter_query = '?team_video={0}&assignee=anyone&language_code={1}'<commit_msg>Blind fix for #2436<commit_after># Amara, universalsubtitles.org
#
# Copyright (C) 2014 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

"""
SubtitleWorkflow classes for old-style teams
"""

from django.conf import settings
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from localeurl.utils import universal_url
from messages.models import Message
from subtitles.signals import subtitles_published
from teams.models import Task, BillingRecord
from teams.permissions import can_add_version, can_assign_task
from teams.workflows.notes import TeamEditorNotes
from teams.workflows.subtitleworkflows import TeamSubtitlesWorkflow
from utils import send_templated_email
from utils import translation
from utils.text import fmt
from videos.tasks import video_changed_tasks
import subtitles.workflows

def _publish_subtitles_if_needed(subtitle_language, version):
    public_tip = subtitle_language.get_tip(public=True)
    if public_tip:
        subtitles_published.send(subtitle_language, version=version)
        BillingRecord.objects.insert_record(public_tip)

def _complete_task(user, video, subtitle_language, saved_version, approved):
    team_video = video.get_team_video()
    task = (team_video.task_set
            .incomplete_review_or_approve()
            .get(language=subtitle_language.language_code))
    if task.assignee is None and can_assign_task(task, user):
        task.assignee = user
    elif task.assignee != user:
        raise ValueError("Task not assigned to user")
    task.new_subtitle_version = subtitle_language.get_tip()
    task.approved = approved
    task.complete()
    if saved_version is None:
        if saved_version is None:
            version_id = None
        else:
            version_id = saved_version.id
            video_changed_tasks.delay(team_video.video_id, version_id)

class Complete(subtitles.workflows.Action):
    """Used when the initial transcriber/translator completes their work """
    name = 'complete'
    label = ugettext_lazy('Complete')
    in_progress_text = ugettext_lazy('Saving')
    visual_class = 'endorse'
    subtitle_visibility = 'private'
    complete = True

    def perform(self, user, video, subtitle_language, saved_version):
        try:
            task = (video.get_team_video().task_set
                    .incomplete_subtitle_or_translate()
                    .filter(language=subtitle_language.language_code).get())
        except Task.DoesNotExist:
            # post publish edit, no task is available
            return
        else:
            task.complete()
        _publish_subtitles_if_needed(subtitle_language, saved_version)

class Approve(subtitles.workflows.Action):
    name = 'approve'
    label = ugettext_lazy('Approve')
    in_progress_text = ugettext_lazy('Approving')
    visual_class = 'endorse'
    subtitle_visibility = 'private'
    complete = True

    def perform(self, user, video, subtitle_language, saved_version):
        _complete_task(user, video, subtitle_language, saved_version,
                       Task.APPROVED_IDS['Approved'])
        _publish_subtitles_if_needed(subtitle_language, saved_version)

class SendBack(subtitles.workflows.Action):
    name = 'send-back'
    label = ugettext_lazy('Send Back')
    in_progress_text = ugettext_lazy('Sending back')
    visual_class = 'send-back'
    subtitle_visibility = 'private'
    complete = False

    def perform(self, user, video, subtitle_language, saved_version):
        _complete_task(user, video, subtitle_language, saved_version,
                       Task.APPROVED_IDS['Rejected'])
        _publish_subtitles_if_needed(subtitle_language, saved_version)

class TaskSaveDraft(subtitles.workflows.SaveDraft):
    subtitle_visibility = 'private'

class TaskTeamEditorNotes(TeamEditorNotes):
    def __init__(self, team_video, language_code):
        super(TaskTeamEditorNotes, self).__init__(team_video.team,
                                                  team_video.video,
                                                  language_code)
        self.team_video = team_video

    def post(self, user, body):
        note = super(TaskTeamEditorNotes, self).post(user, body)
        email_to = [u for u in self.all_assignees() if u != note.user]
        self.send_messages(note, email_to)
        return note

    def all_assignees(self):
        task_qs = (self.team_video.task_set
                   .filter(language=self.language_code,
                           assignee__isnull=False)
                   .select_related('assignee'))
        return set(task.assignee for task in task_qs)

    def send_messages(self, note, user_list):
        subject = fmt(
            _(u'%(user)s added a note while editing %(title)s'),
            user=unicode(note.user), title=self.video.title_display())

        tasks_url = universal_url('teams:team_tasks', kwargs={
            'slug': self.team.slug,
        })
        filter_query = '?team_video={0}&assignee=anyone&lang={1}'
{'fraction_non_alphanumeric': 0.07881730685857972, 'fraction_numerical': 0.001907569582935923, 'mean_word_length': 3.281365998515219, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968492', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3270, 'n_words': 842}