Example #1
0
def view_review(session, request, language, description_id):
    """ Show the review page for a description """
    lang = session.query(Languages).get(language)
    if not lang or not lang.enabled_ddtss:
        raise Http404()

    descr = session.query(Description).filter_by(description_id=description_id).first()
    if not descr:
        raise Http404()

    # Select FOR UPDATE, to avoid concurrency issues.
    trans = session.query(PendingTranslation).filter_by(language=lang, description_id=description_id).with_lockmode('update').first()
    if not trans:
        raise Http404()

    if trans.state == PendingTranslation.STATE_PENDING_TRANSLATION:
        session.commit()
        return show_message_screen(request, 'Still need to translated, redirecting to translation screen', 'ddtss_translate', language, description_id)

    if trans.state != PendingTranslation.STATE_PENDING_REVIEW:
        session.commit()
        return show_message_screen(request, 'Translation not ready for review', 'ddtss_index_lang', language)

    user = get_user(request, session)

    if not lang.translation_model.user_allowed(user, language, lang.translation_model.ACTION_REVIEW):
        return show_message_screen(request, 'User is not permitted to review', 'ddtss_index_lang', language)

    if request.method == 'POST':
        form = ReviewForm(request.POST)

        if not form.is_valid():
            # Maybe return HTTP 400 - Bad request?
            return show_message_screen(request, 'Bad request %r' % form.errors, 'ddtss_index_lang', language)

        if form.cleaned_data['timestamp'] != trans.lastupdate:
            return show_message_screen(request, 'The translation was updated before submission, please recheck changes', 'ddtss_forreview', language, description_id)

        if form.cleaned_data['nothing']:
            trans.comment = form.cleaned_data['comment']

            if trans.comment:
                message = Messages(
                        message=trans.comment,
                        to_user=None,
                        language=language,
                        for_description=description_id,
                        from_user=user.username,
                        in_reply_to=None,
                        timestamp=int(time.time()))
                session.add(message)

                trans.comment = None

            trans.lastupdate=int(time.time())
            session.commit()
            return show_message_screen(request, 'Changed comment only', 'ddtss_index_lang', language)

        if form.cleaned_data['accept']:
            trans.comment = form.cleaned_data['comment']
            # Owner can't review own description
            if user == trans.user:
                session.commit()
                return show_message_screen(request, 'Translation was translated by you', 'ddtss_index_lang', language)

            # Check if user has already reviewed it
            for r in trans.reviews:
                if r.username == user.username:
                    session.commit()
                    return show_message_screen(request, 'Translation was already reviewed', 'ddtss_index_lang', language)
            # Add to reviewers
            trans.reviews.append( PendingTranslationReview(username=user.username) )
            # count review
            user.countreviews += 1
            trans.lastupdate=int(time.time())

            message = ""
            if trans.comment:
                message = trans.comment
                trans.comment = None

            message = Messages(
                    message=message,
                    actionstring="reviewed",
                    to_user=None,
                    language=language,
                    for_description=description_id,
                    from_user=user.username,
                    in_reply_to=None,
                    timestamp=int(time.time()))
            session.add(message)

            if lang.translation_model.translation_accepted(trans):
                # Translation has been accepted, yay!
                trans.accept_translation()
                session.commit()
                return show_message_screen(request, 'Translation accepted', 'ddtss_index_lang', language)

            session.commit()
            return show_message_screen(request, 'Translation reviewed', 'ddtss_index_lang', language)

        if form.cleaned_data['submit']:
            trans.update_translation(form.cleaned_data['short'], form.cleaned_data['long'])
            trans.comment = form.cleaned_data['comment']
            trans.owner_username = user.username
            trans.lastupdate=int(time.time())
            # Clear reviews
            for review in trans.reviews:
                session.delete(review)

            message = ""
            if trans.comment:
                message = trans.comment
                trans.comment = None

            message = Messages(
                    message=message,
                    actionstring="text updated",
                    to_user=None,
                    language=language,
                    for_description=description_id,
                    from_user=user.username,
                    in_reply_to=None,
                    timestamp=int(time.time()))
            session.add(message)

            session.commit()
            return show_message_screen(request, 'Translation updated, review process restarted', 'ddtss_index_lang', language)


    if trans.comment is None:
        trans.comment = ""

    session.commit()

    diff_short = diff_long = None
    if trans.oldshort and trans.for_display(trans.oldshort) != trans.for_display(trans.short):
        diff_short = generate_line_diff(trans.for_display(trans.oldshort), trans.for_display(trans.short))
    if trans.oldlong and trans.for_display(trans.oldlong) != trans.for_display(trans.long):
        diff_long = generate_long_description_diff(trans.for_display(trans.oldlong), trans.for_display(trans.long))


    descr_messages = session.query(Messages) \
                         .filter(Messages.for_description==description_id) \
                         .filter(Messages.language==language) \
                         .order_by(Messages.timestamp) \
                         .all()

    olddiffs = list()
    for olddescr in descr.get_description_predecessors:
        oneolddiff = dict()
        oneolddiff['id'] = descr.description_id
        oneolddiff['short'] = descr.short()
        oneolddiff['long'] = descr.long()
        oneolddiff['transshort'], oneolddiff['translong'] = PendingTranslation.make_quick_suggestion(descr, language)
        oneolddiff['oldid'] = olddescr.description_id
        oneolddiff['oldshort'] = olddescr.short()
        oneolddiff['oldlong'] = olddescr.long()
        oneolddiff['oldtransshort'], oneolddiff['oldtranslong'] = PendingTranslation.make_quick_suggestion(olddescr, language)
        oneolddiff['diff_short'] = generate_line_diff(oneolddiff['oldshort'],oneolddiff['short'])
        oneolddiff['diff_transshort'] = generate_line_diff(oneolddiff['oldtransshort'],oneolddiff['transshort'])
        oneolddiff['diff_long'] = generate_line_diff(oneolddiff['oldlong'],oneolddiff['long'])
        oneolddiff['diff_translong'] = generate_line_diff(oneolddiff['oldtranslong'],oneolddiff['translong'])
        olddiffs.append(oneolddiff)

    return render_to_response("ddtss/translate.html", dict(
        forreview=True,
        diff_short=diff_short,
        diff_long=diff_long,
        lang=lang,
        descr=descr,
        trans=trans,
        olddiffs=olddiffs,
        descr_messages=descr_messages), context_instance=RequestContext(request))
Example #2
0
def view_translate(session, request, language, description_id):
    """ Show the translation page for a description """
    lang = session.query(Languages).get(language)
    if not lang or not lang.enabled_ddtss:
        raise Http404()

    descr = session.query(Description).filter_by(description_id=description_id).first()
    if not descr:
        raise Http404()

    user = get_user(request, session)

    if not lang.translation_model.user_allowed(user, language, lang.translation_model.ACTION_REVIEW):
        return show_message_screen(request, 'User is not permitted to translate', 'ddtss_index_lang', language)

    # Select FOR UPDATE, to avoid concurrency issues.
    trans = session.query(PendingTranslation).filter_by(language=lang, description_id=description_id).with_lockmode('update').first()
    if not trans:
        message = Messages(
                message="",
                actionstring="fetched",
                to_user=None,
                language=language,
                for_description=description_id,
                from_user=user.username,
                in_reply_to=None,
                timestamp=int(time.time()))
        session.add(message)

        trans = PendingTranslation(
                description_id=description_id,
                language=lang,
                firstupdate=int(time.time()),
                lastupdate=int(time.time()),
                owner_username=user.username,
                owner_locktime=int(time.time()),
                iteration=0,
                state=PendingTranslation.STATE_PENDING_TRANSLATION)

        # Make a suggestion for the new translation
        trans.short, trans.long = PendingTranslation.make_suggestion(descr, language)
        session.add(trans)

    if trans.state != PendingTranslation.STATE_PENDING_TRANSLATION:
        session.commit()
        return show_message_screen(request, 'Already translated, redirecting to review screen', 'ddtss_forreview', language, description_id)

    # Try to lock the description, note sets the owner field
    if not trans.trylock(user):
        session.commit()
        return show_message_screen(request, 'Translation locked, try again later', 'ddtss_index_lang', language)

    if request.method == 'POST':
        form = TranslationForm(request.POST)

        if not form.is_valid():
            # Maybe return HTTP 400 - Bad request?
            return show_message_screen(request, 'Bad request %r' % form.errors, 'ddtss_index_lang', language)

        if form.cleaned_data['abandon']:
            trans.comment = form.cleaned_data['comment']
            trans.unlock()
            session.commit()
            return show_message_screen(request, 'Translation abandoned', 'ddtss_index_lang', language)

        if form.cleaned_data['submit']:
            trans.update_translation(form.cleaned_data['short'], form.cleaned_data['long'])
            trans.lastupdate=int(time.time())
            trans.comment = form.cleaned_data['comment']
            trans.unlock()

            # If no longer pending translation, add one to counter
            if trans.state != PendingTranslation.STATE_PENDING_TRANSLATION:
                user.counttranslations += 1

            message = ""
            if trans.comment:
                message = trans.comment
                trans.comment = None

            message = Messages(
                    message=message,
                    actionstring="text updated",
                    to_user=None,
                    language=language,
                    for_description=description_id,
                    from_user=user.username,
                    in_reply_to=None,
                    timestamp=int(time.time()))
            session.add(message)

            session.commit()
            return show_message_screen(request, 'Translation submitted', 'ddtss_index_lang', language)

    if trans.comment is None:
        trans.comment = ""
    if trans.short is None:
        trans.short, trans.long = PendingTranslation.make_suggestion(descr, language)

    descr_messages = session.query(Messages) \
                          .filter(Messages.language==language) \
                          .filter(Messages.for_description==description_id) \
                          .order_by(Messages.timestamp) \
                          .all()

    olddiffs = list()
    for olddescr in descr.get_description_predecessors:
        oneolddiff = dict()
        oneolddiff['id'] = descr.description_id
        oneolddiff['short'] = descr.short()
        oneolddiff['long'] = descr.long()
        oneolddiff['transshort'], oneolddiff['translong'] = PendingTranslation.make_quick_suggestion(descr, language)
        oneolddiff['oldid'] = olddescr.description_id
        oneolddiff['oldshort'] = olddescr.short()
        oneolddiff['oldlong'] = olddescr.long()
        oneolddiff['oldtransshort'], oneolddiff['oldtranslong'] = PendingTranslation.make_quick_suggestion(olddescr, language)
        oneolddiff['diff_short'] = generate_line_diff(oneolddiff['oldshort'],oneolddiff['short'])
        oneolddiff['diff_transshort'] = generate_line_diff(oneolddiff['oldtransshort'],oneolddiff['transshort'])
        oneolddiff['diff_long'] = generate_line_diff(oneolddiff['oldlong'],oneolddiff['long'])
        oneolddiff['diff_translong'] = generate_line_diff(oneolddiff['oldtranslong'],oneolddiff['translong'])
        olddiffs.append(oneolddiff)

    session.commit()

    return render_to_response("ddtss/translate.html", dict(
        forreview=False,
        lang=lang,
        descr=descr,
        trans=trans,
        olddiffs=olddiffs,
        descr_messages=descr_messages), context_instance=RequestContext(request))
Example #3
0
def view_index_lang(session, request, language):
    """ Does the main index page for a single language in DDTSS """
    lang = session.query(Languages).get(language)
    if not lang or not lang.enabled_ddtss:
        raise Http404()

    user = get_user(request, session)

    if user.lastlanguage != lang:
        user.lastlanguage = lang
    user.lastseen = int(time.time())

    if request.method == 'POST':
        form = FetchForm(request.POST)

        if not form.is_valid():
            # Maybe return HTTP 400 - Bad request?
            return show_message_screen(request, 'Bad request %r' % form.errors, 'ddtss_index_lang', language)

        if form.cleaned_data['package']:
            pack = form.cleaned_data['package']
            force = form.cleaned_data['force']

            if re.match('^\d+$', pack) :
                description_id=pack
            else :
                # FIXME error, ist package name is not found!
                packageversion=session.query(PackageVersion).filter(PackageVersion.package==pack).\
                        join(ActiveDescription, ActiveDescription.description_id == PackageVersion.description_id).limit(1).first()

                if not packageversion :
                    return show_message_screen(request, 'No Package %s found' % pack, 'ddtss_index_lang', language)

                description_id=packageversion.description_id

            description=session.query(Description).filter(Description.description_id==description_id).first()

            if not description :
                return show_message_screen(request, 'No description-id %s found' % str(description_id), 'ddtss_index_lang', language)

            if language not in description.translation or force:
                trans = session.query(PendingTranslation).filter_by(language=lang, description_id=description_id).with_lockmode('update').first()
                if not trans:
                    message = Messages(
                            message="",
                            actionstring="fetched",
                            to_user=None,
                            language=language,
                            for_description=description_id,
                            from_user=user.username,
                            in_reply_to=None,
                            timestamp=int(time.time()))
                    session.add(message)

                    trans = PendingTranslation(
                            description_id=description_id,
                            language=lang,
                            firstupdate=int(time.time()),
                            lastupdate=int(time.time()),
                            owner_username=user.username,
                            owner_locktime=int(time.time()),
                            iteration=0,
                            state=0)
                    trans.short, trans.long = PendingTranslation.make_suggestion(description, language)
                    session.add(trans)
                    session.commit()
                    return show_message_screen(request, 'Fetch Package %s (%s)' % (description.package,str(description_id)), 'ddtss_index_lang', language)

            return show_message_screen(request, 'Don\'t fetch Package %s' % (pack), 'ddtss_index_lang', language)

    session.commit()

    # TODO: Don't load actual descriptions
    translations = session.query(PendingTranslation,
                                 func.sum(expression.case([(PendingTranslationReview.username==user.username, 1)], else_=0)).label('reviewed'),
                                 func.count().label('reviews')) \
                          .outerjoin(PendingTranslationReview) \
                          .filter(PendingTranslation.language_ref==language) \
                          .group_by(PendingTranslation) \
                          .options(subqueryload(PendingTranslation.reviews)) \
                          .options(subqueryload(PendingTranslation.description)) \
                          .options(subqueryload('description.milestones')) \
                          .all()

    pending_translations = []
    pending_review = []
    reviewed = []

    for trans, reviewed_by_me, reviews in translations:
        if trans.state == PendingTranslation.STATE_PENDING_REVIEW:
            if reviewed_by_me or trans.owner_username == user.username:
                reviewed.append(trans)
            else:
                pending_review.append(trans)
        elif trans.state == PendingTranslation.STATE_PENDING_TRANSLATION:
            pending_translations.append(trans)

    reviewed.sort(key=lambda t: t.lastupdate, reverse=True)
    pending_review.sort(key=lambda t: t.lastupdate, reverse=False)
    pending_translations.sort(key=lambda t: t.firstupdate, reverse=False)

    global_messages = Messages.global_messages(session) \
                          .order_by(Messages.timestamp.desc()) \
                          .limit(20) \
                          .all()

    team_messages = Messages.team_messages(session, language) \
                          .order_by(Messages.timestamp.desc()) \
                          .limit(20) \
                          .all()

    user_messages = Messages.user_messages(session, user.username) \
                          .order_by(Messages.timestamp.desc()) \
                          .limit(20) \
                          .all()

    team_mile_hight = session.query(DescriptionMilestone.milestone,func.count(Translation.description_id)). \
                        join(Translation, DescriptionMilestone.description_id == Translation.description_id).\
                        filter(Translation.language==lang.language).\
                        group_by(DescriptionMilestone.milestone).order_by(DescriptionMilestone.milestone).all()

    team_mile_hight1 = session.query(DescriptionMilestone.milestone,func.count(PendingTranslation.description_id)). \
                        join(PendingTranslation, DescriptionMilestone.description_id == PendingTranslation.description_id).\
                        filter(PendingTranslation.language_ref == lang.language). \
                        group_by(DescriptionMilestone.milestone).order_by(DescriptionMilestone.milestone).all()

    team_mile_hight2 = session.query(DescriptionMilestone.milestone,func.count(DescriptionMilestone.description_id)). \
                        filter(or_(DescriptionMilestone.milestone==lang.milestone_high,\
                        DescriptionMilestone.milestone==lang.milestone_medium,\
                        DescriptionMilestone.milestone==lang.milestone_low,\
                        DescriptionMilestone.milestone==user.milestone)).\
                        group_by(DescriptionMilestone.milestone).order_by(DescriptionMilestone.milestone).all()

    resultdict = dict(team_mile_hight)
    resultdict1 = dict(team_mile_hight1)

    stat_user_milestone = session.query(DescriptionMilestone).filter(DescriptionMilestone.milestone==user.milestone).all();
    stat_lang_milestone_high = session.query(DescriptionMilestone).filter(DescriptionMilestone.milestone==lang.milestone_high).all();
    stat_lang_milestone_medium = session.query(DescriptionMilestone).filter(DescriptionMilestone.milestone==lang.milestone_medium).all();
    stat_lang_milestone_low = session.query(DescriptionMilestone).filter(DescriptionMilestone.milestone==lang.milestone_low).all();

    newmilestones = dict()
    for r in team_mile_hight2:
        if r[0] == user.milestone:
            newmilestones['user_milestone']=dict()
            newmilestones['user_milestone']['type']='user_milestone'
            newmilestones['user_milestone']['name']=r[0]
            newmilestones['user_milestone']['total']=r[1]
            newmilestones['user_milestone']['pending']=resultdict1.get(r[0],0)
            newmilestones['user_milestone']['translated']=resultdict.get(r[0],0)
            newmilestones['user_milestone']['percent']=resultdict.get(r[0],0)*100/r[1]
            newmilestones['user_milestone']['flot']=stat_user_milestone[0].Get_flot_data(language);
        if r[0] == lang.milestone_high:
            newmilestones['lang_milestone_high']=dict()
            newmilestones['lang_milestone_high']['type']='lang_milestone_high'
            newmilestones['lang_milestone_high']['name']=r[0]
            newmilestones['lang_milestone_high']['total']=r[1]
            newmilestones['lang_milestone_high']['pending']=resultdict1.get(r[0],0)
            newmilestones['lang_milestone_high']['translated']=resultdict.get(r[0],0)
            newmilestones['lang_milestone_high']['percent']=resultdict.get(r[0],0)*100/r[1]
            newmilestones['lang_milestone_high']['flot']=stat_lang_milestone_high[0].Get_flot_data(language);
        if r[0] == lang.milestone_medium:
            newmilestones['lang_milestone_medium']=dict()
            newmilestones['lang_milestone_medium']['type']='lang_milestone_medium'
            newmilestones['lang_milestone_medium']['name']=r[0]
            newmilestones['lang_milestone_medium']['total']=r[1]
            newmilestones['lang_milestone_medium']['pending']=resultdict1.get(r[0],0)
            newmilestones['lang_milestone_medium']['translated']=resultdict.get(r[0],0)
            newmilestones['lang_milestone_medium']['percent']=resultdict.get(r[0],0)*100/r[1]
            newmilestones['lang_milestone_medium']['flot']=stat_lang_milestone_medium[0].Get_flot_data(language);
        if r[0] == lang.milestone_low:
            newmilestones['lang_milestone_low']=dict()
            newmilestones['lang_milestone_low']['type']='lang_milestone_low'
            newmilestones['lang_milestone_low']['name']=r[0]
            newmilestones['lang_milestone_low']['total']=r[1]
            newmilestones['lang_milestone_low']['pending']=resultdict1.get(r[0],0)
            newmilestones['lang_milestone_low']['translated']=resultdict.get(r[0],0)
            newmilestones['lang_milestone_low']['percent']=resultdict.get(r[0],0)*100/r[1]
            newmilestones['lang_milestone_low']['flot']=stat_lang_milestone_low[0].Get_flot_data(language);

    # now sort it
    milestones = list()
    if 'user_milestone' in newmilestones:
        milestones.append(newmilestones['user_milestone'])
    if 'lang_milestone_high' in newmilestones:
        milestones.append(newmilestones['lang_milestone_high'])
    if 'lang_milestone_medium' in newmilestones:
        milestones.append(newmilestones['lang_milestone_medium'])
    if 'lang_milestone_low' in newmilestones:
        milestones.append(newmilestones['lang_milestone_low'])

    involveddescriptions = [x for x, in Messages.involveddescriptions(session, user.username).all()]

    response = render_to_response("ddtss/index_lang.html", dict(
        lang=lang,
        user=user,
        auth=user.get_authority(language),
        pending_translations=pending_translations,
        pending_review=pending_review,
        reviewed=reviewed,
        involveddescriptions=involveddescriptions,
        milestones=milestones,
        global_messages=global_messages,
        team_messages=team_messages,
        user_messages=user_messages), context_instance=RequestContext(request))

    return save_user(response, user)
Example #4
0
def view_review(session, request, language, description_id):
    """ Show the review page for a description """
    lang = session.query(Languages).get(language)
    if not lang or not lang.enabled_ddtss:
        raise Http404()

    descr = session.query(Description).filter_by(description_id=description_id).first()
    if not descr:
        raise Http404()

    # Select FOR UPDATE, to avoid concurrency issues.
    trans = session.query(PendingTranslation).filter_by(language=lang, description_id=description_id).with_lockmode('update').first()
    if not trans:
        raise Http404()

    if trans.state == PendingTranslation.STATE_PENDING_TRANSLATION:
        session.commit()
        return show_message_screen(request, 'Still need to translated, redirecting to translation screen', 'ddtss_translate', language, description_id)

    if trans.state != PendingTranslation.STATE_PENDING_REVIEW:
        session.commit()
        return show_message_screen(request, 'Translation not ready for review', 'ddtss_index_lang', language)

    user = get_user(request, session)

    if not lang.translation_model.user_allowed(user, language, lang.translation_model.ACTION_REVIEW):
        return show_message_screen(request, 'User is not permitted to review', 'ddtss_index_lang', language)

    if request.method == 'POST':
        form = ReviewForm(request.POST)

        if not form.is_valid():
            # Maybe return HTTP 400 - Bad request?
            return show_message_screen(request, 'Bad request %r' % form.errors, 'ddtss_index_lang', language)

        if form.cleaned_data['timestamp'] != trans.lastupdate:
            return show_message_screen(request, 'The translation was updated before submission, please recheck changes', 'ddtss_forreview', language, description_id)

        if form.cleaned_data['nothing']:
            trans.comment = form.cleaned_data['comment']

            if trans.comment:
                message = Messages(
                        message=trans.comment,
                        to_user=None,
                        language=language,
                        for_description=description_id,
                        from_user=user.username,
                        in_reply_to=None,
                        timestamp=int(time.time()))
                session.add(message)

                trans.comment = None

            trans.lastupdate = int(time.time())
            session.commit()
            return show_message_screen(request, 'Changed comment only', 'ddtss_index_lang', language)

        if form.cleaned_data['accept']:
            trans.comment = form.cleaned_data['comment']
            # Owner can't review own description
            if user == trans.user:
                session.commit()
                return show_message_screen(request, 'Translation was translated by you', 'ddtss_index_lang', language)

            # Check if user has already reviewed it
            for r in trans.reviews:
                if r.username == user.username:
                    session.commit()
                    return show_message_screen(request, 'Translation was already reviewed', 'ddtss_index_lang', language)
            # Add to reviewers
            trans.reviews.append(PendingTranslationReview(username=user.username))
            # count review
            user.countreviews += 1
            trans.lastupdate = int(time.time())

            message = ""
            if trans.comment:
                message = trans.comment
                trans.comment = None

            message = Messages(
                    message=message,
                    actionstring="reviewed",
                    to_user=None,
                    language=language,
                    for_description=description_id,
                    from_user=user.username,
                    in_reply_to=None,
                    timestamp=int(time.time()))
            session.add(message)

            if lang.translation_model.translation_accepted(trans):
                # Translation has been accepted, yay!
                trans.accept_translation()
                session.commit()
                return show_message_screen(request, 'Translation accepted', 'ddtss_index_lang', language)

            session.commit()
            return show_message_screen(request, 'Translation reviewed', 'ddtss_index_lang', language)

        if form.cleaned_data['submit']:
            trans.update_translation(form.cleaned_data['short'], form.cleaned_data['long'])
            trans.comment = form.cleaned_data['comment']
            trans.owner_username = user.username
            trans.lastupdate = int(time.time())
            # Clear reviews
            for review in trans.reviews:
                session.delete(review)

            message = ""
            if trans.comment:
                message = trans.comment
                trans.comment = None

            message = Messages(
                    message=message,
                    actionstring="text updated",
                    to_user=None,
                    language=language,
                    for_description=description_id,
                    from_user=user.username,
                    in_reply_to=None,
                    timestamp=int(time.time()))
            session.add(message)

            session.commit()
            return show_message_screen(request, 'Translation updated, review process restarted', 'ddtss_index_lang', language)


    if trans.comment is None:
        trans.comment = ""

    session.commit()

    diff_short = diff_long = None
    if trans.oldshort and trans.for_display(trans.oldshort) != trans.for_display(trans.short):
        diff_short = generate_line_diff(trans.for_display(trans.oldshort), trans.for_display(trans.short))
    if trans.oldlong and trans.for_display(trans.oldlong) != trans.for_display(trans.long):
        diff_long = generate_long_description_diff(trans.for_display(trans.oldlong), trans.for_display(trans.long))


    descr_messages = Messages.description_messages(session, description_id) \
                             .order_by(Messages.timestamp) \
                             .all()
    trans_messages = Messages.translation_messages(session, language, description_id) \
                             .order_by(Messages.timestamp) \
                             .all()

    olddiffs = list()
    for olddescr in descr.get_description_predecessors:
        oneolddiff = dict()
        oneolddiff['id'] = descr.description_id
        oneolddiff['short'] = descr.short()
        oneolddiff['long'] = descr.long()
        oneolddiff['transshort'], oneolddiff['translong'] = PendingTranslation.make_quick_suggestion(descr, language)
        oneolddiff['oldid'] = olddescr.description_id
        oneolddiff['oldshort'] = olddescr.short()
        oneolddiff['oldlong'] = olddescr.long()
        oneolddiff['oldtransshort'], oneolddiff['oldtranslong'] = PendingTranslation.make_quick_suggestion(olddescr, language)
        oneolddiff['diff_short'] = generate_line_diff(oneolddiff['oldshort'], oneolddiff['short'])
        oneolddiff['diff_transshort'] = generate_line_diff(oneolddiff['oldtransshort'], oneolddiff['transshort'])
        oneolddiff['diff_long'] = generate_line_diff(oneolddiff['oldlong'], oneolddiff['long'])
        oneolddiff['diff_translong'] = generate_line_diff(oneolddiff['oldtranslong'], oneolddiff['translong'])
        olddiffs.append(oneolddiff)

    return render_to_response("ddtss/translate.html", dict(
        user=user,
        forreview=True,
        diff_short=diff_short,
        diff_long=diff_long,
        lang=lang,
        descr=descr,
        trans=trans,
        olddiffs=olddiffs,
        descr_messages=descr_messages,
        trans_messages=trans_messages), context_instance=RequestContext(request))
Example #5
0
def view_translate(session, request, language, description_id):
    """ Show the translation page for a description """
    lang = session.query(Languages).get(language)
    if not lang or not lang.enabled_ddtss:
        raise Http404()

    descr = session.query(Description).filter_by(description_id=description_id).first()
    if not descr:
        raise Http404()

    user = get_user(request, session)

    if not lang.translation_model.user_allowed(user, language, lang.translation_model.ACTION_TRANSLATE):
        return show_message_screen(request, 'User is not permitted to translate', 'ddtss_index_lang', language)

    # Select FOR UPDATE, to avoid concurrency issues.
    trans = session.query(PendingTranslation).filter_by(language=lang, description_id=description_id).with_lockmode('update').first()
    if not trans:
        message = Messages(
                message="",
                actionstring="fetched",
                to_user=None,
                language=language,
                for_description=description_id,
                from_user=user.username,
                in_reply_to=None,
                timestamp=int(time.time()))
        session.add(message)

        trans = PendingTranslation(
                description_id=description_id,
                language=lang,
                firstupdate=int(time.time()),
                lastupdate=int(time.time()),
                owner_username=user.username,
                owner_locktime=int(time.time()),
                iteration=0,
                state=PendingTranslation.STATE_PENDING_TRANSLATION)

        # Make a suggestion for the new translation
        trans.short, trans.long = PendingTranslation.make_suggestion(descr, language)
        session.add(trans)

    if trans.state != PendingTranslation.STATE_PENDING_TRANSLATION:
        session.commit()
        return show_message_screen(request, 'Already translated, redirecting to review screen', 'ddtss_forreview', language, description_id)

    # Try to lock the description, note sets the owner field
    if not trans.trylock(user):
        session.commit()
        return show_message_screen(request, 'Translation locked, try again later', 'ddtss_index_lang', language)

    if request.method == 'POST':
        form = TranslationForm(request.POST)

        if not form.is_valid():
            # Maybe return HTTP 400 - Bad request?
            return show_message_screen(request, 'Bad request %r' % form.errors, 'ddtss_index_lang', language)

        if form.cleaned_data['abandon']:
            trans.comment = form.cleaned_data['comment']
            trans.unlock()
            session.commit()
            return show_message_screen(request, 'Translation abandoned', 'ddtss_index_lang', language)

        if form.cleaned_data['submit']:
            trans.update_translation(form.cleaned_data['short'], form.cleaned_data['long'])
            trans.lastupdate = int(time.time())
            trans.comment = form.cleaned_data['comment']
            trans.unlock()

            # If no longer pending translation, add one to counter
            if trans.state != PendingTranslation.STATE_PENDING_TRANSLATION:
                user.counttranslations += 1

            message = ""
            if trans.comment:
                message = trans.comment
                trans.comment = None

            message = Messages(
                    message=message,
                    actionstring="text updated",
                    to_user=None,
                    language=language,
                    for_description=description_id,
                    from_user=user.username,
                    in_reply_to=None,
                    timestamp=int(time.time()))
            session.add(message)

            session.commit()
            if trans.state != PendingTranslation.STATE_PENDING_TRANSLATION:
                return show_message_screen(request, 'Translation submitted for review', 'ddtss_index_lang', language)
            else:
                return show_message_screen(request, 'Translation saved for further translation', 'ddtss_index_lang', language)

    if trans.comment is None:
        trans.comment = ""
    if trans.short is None:
        trans.short, trans.long = PendingTranslation.make_suggestion(descr, language)

    descr_messages = Messages.description_messages(session, description_id) \
                             .order_by(Messages.timestamp) \
                             .all()
    trans_messages = Messages.translation_messages(session, language, description_id) \
                             .order_by(Messages.timestamp) \
                             .all()

    olddiffs = list()
    for olddescr in descr.get_description_predecessors:
        oneolddiff = dict()
        oneolddiff['id'] = descr.description_id
        oneolddiff['short'] = descr.short()
        oneolddiff['long'] = descr.long()
        oneolddiff['transshort'], oneolddiff['translong'] = PendingTranslation.make_quick_suggestion(descr, language)
        oneolddiff['oldid'] = olddescr.description_id
        oneolddiff['oldshort'] = olddescr.short()
        oneolddiff['oldlong'] = olddescr.long()
        oneolddiff['oldtransshort'], oneolddiff['oldtranslong'] = PendingTranslation.make_quick_suggestion(olddescr, language)
        oneolddiff['diff_short'] = generate_line_diff(oneolddiff['oldshort'], oneolddiff['short'])
        oneolddiff['diff_transshort'] = generate_line_diff(oneolddiff['oldtransshort'], oneolddiff['transshort'])
        oneolddiff['diff_long'] = generate_line_diff(oneolddiff['oldlong'], oneolddiff['long'])
        oneolddiff['diff_translong'] = generate_line_diff(oneolddiff['oldtranslong'], oneolddiff['translong'])
        olddiffs.append(oneolddiff)

    session.commit()

    return render_to_response("ddtss/translate.html", dict(
        user=user,
        forreview=False,
        lang=lang,
        descr=descr,
        trans=trans,
        olddiffs=olddiffs,
        descr_messages=descr_messages,
        trans_messages=trans_messages), context_instance=RequestContext(request))
Example #6
0
def view_index_lang(session, request, language):
    """ Does the main index page for a single language in DDTSS """
    lang = session.query(Languages).get(language)
    if not lang or not lang.enabled_ddtss:
        raise Http404()

    user = get_user(request, session)

    if user.lastlanguage != lang:
        user.lastlanguage = lang
    user.lastseen = int(time.time())

    if request.method == 'POST':
        form = FetchForm(request.POST)

        if not form.is_valid():
            # Maybe return HTTP 400 - Bad request?
            return show_message_screen(request, 'Bad request %r' % form.errors, 'ddtss_index_lang', language)

        if not lang.translation_model.user_allowed(user, language, lang.translation_model.ACTION_TRANSLATE):
            return show_message_screen(request, 'User is not permitted to translate', 'ddtss_index_lang', language)

        pack = form.cleaned_data['package'].strip()
        force = form.cleaned_data['force']

        if pack == '':
            description_id = lang.get_next_to_translate(session)
        elif re.match('^\d+$', pack):
            description_id = int(pack)
        else:
            packageversion = session.query(PackageVersion).filter(PackageVersion.package == pack). \
                                     join(ActiveDescription, ActiveDescription.description_id == PackageVersion.description_id).first()

            if not packageversion:
                return show_message_screen(request, 'No Package %s found' % pack, 'ddtss_index_lang', language)

            description_id = packageversion.description_id

        description = session.query(Description).filter(Description.description_id == description_id).first()

        if not description:
            return show_message_screen(request, 'No description-id %s found' % str(description_id), 'ddtss_index_lang', language)

        if language not in description.translation or force:
            trans = session.query(PendingTranslation).filter_by(language=lang, description_id=description_id).with_lockmode('update').first()
            if not trans:
                message = Messages(
                        message="",
                        actionstring="fetched",
                        to_user=None,
                        language=language,
                        for_description=description_id,
                        from_user=user.username,
                        in_reply_to=None,
                        timestamp=int(time.time()))
                session.add(message)

                trans = PendingTranslation(
                        description_id=description_id,
                        language=lang,
                        firstupdate=int(time.time()),
                        lastupdate=int(time.time()),
                        owner_username=user.username,
                        owner_locktime=int(time.time()),
                        iteration=0,
                        state=0)
                trans.short, trans.long = PendingTranslation.make_suggestion(description, language)
                session.add(trans)
                session.commit()
                return show_message_screen(request, 'Fetched package %s (%s)' % (description.package, str(description_id)), 'ddtss_translate', language, str(description_id))

        return show_message_screen(request, 'Package %s already translated (and not forced)' % (pack), 'ddtss_index_lang', language)

    session.commit()

    # TODO: Don't load actual descriptions
    translations = session.query(PendingTranslation,
                                 func.sum(expression.case([(PendingTranslationReview.username == user.username, 1)], else_=0)).label('reviewed'),
                                 func.count().label('reviews')) \
                          .outerjoin(PendingTranslationReview) \
                          .filter(PendingTranslation.language_ref == language) \
                          .group_by(PendingTranslation) \
                          .options(subqueryload(PendingTranslation.reviews)) \
                          .options(subqueryload(PendingTranslation.description)) \
                          .options(subqueryload('description.milestones')) \
                          .all()

    pending_translations = []
    pending_review = []
    reviewed = []

    for trans, reviewed_by_me, reviews in translations:
        if trans.state == PendingTranslation.STATE_PENDING_REVIEW:
            if reviewed_by_me or trans.owner_username == user.username:
                reviewed.append(trans)
            else:
                pending_review.append(trans)
        elif trans.state == PendingTranslation.STATE_PENDING_TRANSLATION:
            pending_translations.append(trans)

    reviewed.sort(key=lambda t: t.lastupdate, reverse=True)
    pending_review.sort(key=lambda t: t.lastupdate, reverse=False)
    pending_translations.sort(key=lambda t: t.firstupdate, reverse=False)

    global_messages = Messages.global_messages(session) \
                          .order_by(Messages.timestamp.desc()) \
                          .limit(20) \
                          .all()

    team_messages = Messages.team_messages(session, language) \
                          .order_by(Messages.timestamp.desc()) \
                          .limit(20) \
                          .all()

    user_messages = Messages.user_messages(session, user.username) \
                          .order_by(Messages.timestamp.desc()) \
                          .limit(20) \
                          .all()

    milestones = []
    for type, name, milestone in (('user_milestone', 'User', user.milestone),
                                  ('lang_milestone_high', 'Team high', lang.milestone_high),
                                  ('lang_milestone_medium', 'Team medium', lang.milestone_medium),
                                  ('lang_milestone_low', 'Team low', lang.milestone_low)):
        if not milestone:
            continue
        m = session.query(DescriptionMilestone).filter(DescriptionMilestone.milestone == milestone).first()
        if not m:
            continue

        info = m.info_language(lang)
        info['type'] = type
        info['typename'] = name
        milestones.append(info)

    involveddescriptions = [x for x, in Messages.involveddescriptions(session, user.username).all()]
    recently_translated = Messages.recently_translated(session, language).limit(10).all()

    response = render_to_response("ddtss/index_lang.html", dict(
        lang=lang,
        user=user,
        auth=user.get_authority(language),
        pending_translations=pending_translations,
        pending_review=pending_review,
        reviewed=reviewed,
        involveddescriptions=involveddescriptions,
        milestones=milestones,
        recently_translated=recently_translated,
        global_messages=global_messages,
        team_messages=team_messages,
        user_messages=user_messages), context_instance=RequestContext(request))

    return save_user(response, user)