示例#1
0
文件: views.py 项目: actsgo/pootle
def update_user_rates(request):
    form = UserRatesForm(request.POST)

    if form.is_valid():
        try:
            User = get_user_model()
            user = User.objects.get(username=form.cleaned_data['username'])
        except User.DoesNotExist:
            error_text = _("User %s not found" % form.cleaned_data['username'])

            return HttpResponseNotFound(jsonify({'msg': error_text}),
                                        content_type="application/json")

        user.currency = form.cleaned_data['currency']
        user.rate = form.cleaned_data['rate']
        user.review_rate = form.cleaned_data['review_rate']
        user.hourly_rate = form.cleaned_data['hourly_rate']

        scorelog_filter = {'user': user}
        paid_task_filter = scorelog_filter.copy()
        if form.cleaned_data['effective_from'] is not None:
            effective_from = form.cleaned_data['effective_from']
            scorelog_filter.update({
                'creation_time__gte': effective_from
            })
            paid_task_filter.update({
                'datetime__gte': effective_from
            })

        scorelog_query = ScoreLog.objects.filter(**scorelog_filter)
        scorelog_count = scorelog_query.count()

        paid_task_query = PaidTask.objects.filter(**paid_task_filter)
        paid_task_count = paid_task_query.count()

        scorelog_query.update(rate=user.rate, review_rate=user.review_rate)

        def get_task_rate_for(user, task_type):
            return {
                PaidTaskTypes.TRANSLATION: user.rate,
                PaidTaskTypes.REVIEW: user.review_rate,
                PaidTaskTypes.HOURLY_WORK: user.hourly_rate,
                PaidTaskTypes.CORRECTION: 1,
            }.get(task_type, 0)

        for task in paid_task_query:
            task.rate = get_task_rate_for(user, task.task_type)
            task.save()

        user.save()

        return HttpResponse(
            jsonify({
                'scorelog_count': scorelog_count,
                'paid_task_count': paid_task_count
            }), content_type="application/json")

    return HttpResponseBadRequest(jsonify({'errors': form.errors}),
                                  content_type="application/json")
示例#2
0
def add_paid_task(request):
    form = PaidTaskForm(request.POST)
    if form.is_valid():
        form.save()

        return HttpResponse(jsonify({'result': form.instance.id}),
                            content_type="application/json")

    return HttpResponseBadRequest(jsonify({'errors': form.errors}),
                                  content_type="application/json")
示例#3
0
文件: views.py 项目: actsgo/pootle
def add_paid_task(request):
    form = PaidTaskForm(request.POST)
    if form.is_valid():
        form.save()
        obj = form.instance
        log('%s\t%s\t%s' % (request.user.username, PAID_TASK_ADDED, obj))
        return HttpResponse(jsonify({'result': obj.id}),
                            content_type="application/json")

    return HttpResponseBadRequest(jsonify({'errors': form.errors}),
                                  content_type="application/json")
示例#4
0
def remove_paid_task(request, task_id=None):
    if request.method == 'DELETE':
        tasks = PaidTask.objects.filter(id=task_id)
        count = tasks.count()
        tasks.delete()

        return HttpResponse(jsonify({'removed': count}),
                            content_type="application/json")

    return HttpResponseBadRequest(
        jsonify({'error': _('Invalid request method')}),
        content_type="application/json"
    )
示例#5
0
文件: views.py 项目: julen/pootle
def get_failing_checks(request, pathobj):
    """
    Gets a list of failing checks for the current object.

    @return: JSON string representing action status and depending on success,
    returns an error message or a list containing the the name and number of
    failing checks.
    """
    json = {}
    checkopts = []
    # Borrowed from pootle_app.views.language.item_dict.getcheckdetails
    property_stats = pathobj.getcompletestats()
    quick_stats = pathobj.getquickstats()
    total = quick_stats["total"]
    keys = property_stats.keys()
    keys.sort()
    for checkname in keys:
        checkcount = property_stats[checkname]
        if total and checkcount:
            stats = ungettext(
                "%(checkname)s (%(checks)d)",
                "%(checkname)s (%(checks)d)",
                checkcount,
                {"checks": checkcount, "checkname": checkname},
            )
            checkopt = {"name": checkname, "text": stats}
            checkopts.append(checkopt)
            json["checks"] = checkopts
    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#6
0
文件: views.py 项目: Chipcius/pootle
def ajax_edit_goal(request, goal):
    """Edit a goal through a form using AJAX."""

    form = GoalForm(request.POST, instance=goal)
    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        if goal.description:
            response["description"] = goal.description
        else:
            response["description"] = (u'<p class="placeholder muted">%s</p>' %
                                       _(u"No description yet."))
    context = {
        'form': form,
        'form_action': reverse('pootle-tagging-ajax-edit-goal',
                               args=[goal.slug]),
    }
    t = loader.get_template('admin/general_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response), status=rcode,
                        mimetype="application/json")
示例#7
0
文件: views.py 项目: AmesianX/pootle
def project_settings_edit(request, project):
    from pootle_project.forms import DescriptionForm
    form = DescriptionForm(request.POST, instance=project)

    response = {}
    status = 400

    if form.is_valid():
        form.save()
        status = 200

        if project.description:
            the_html = project.description
        else:
            the_html = u"".join([
                u'<p class="placeholder muted">',
                _(u"No description yet."),
                u"</p>",
            ])

        response["description"] = the_html

    ctx = {
        "form": form,
        "form_action": reverse('pootle-project-admin-settings',
                               args=[project.code]),
    }

    template = loader.get_template('admin/_settings_form.html')
    response['form'] = template.render(RequestContext(request, ctx))

    return HttpResponse(jsonify(response), status=status,
                        mimetype="application/json")
示例#8
0
def overview(request, language):
    translation_projects = language.children \
                                   .order_by('project__fullname')
    user_tps = filter(lambda x: x.is_accessible_by(request.user),
                      translation_projects)
    items = (make_project_item(tp) for tp in user_tps)

    table_fields = ['name', 'progress', 'total', 'need-translation',
                    'suggestions', 'critical', 'last-updated', 'activity']
    table = {
        'id': 'language',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items,
    }

    ctx = get_overview_context(request)
    ctx.update({
        'language': {
          'code': language.code,
          'name': tr_lang(language.fullname),
        },
        'table': table,
        'stats': jsonify(request.resource_obj.get_stats()),

        'browser_extends': 'languages/base.html',
    })

    response = render(request, 'browser/overview.html', ctx)
    response.set_cookie('pootle-language', language.code)

    return response
示例#9
0
def edit_settings(request):
    """Saves the site's general settings."""
    siteconfig = load_site_config()
    form = GeneralSettingsForm(siteconfig, data=request.POST)

    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        the_html = u"".join([
            u"<div>", form.cleaned_data['DESCRIPTION'], "</div>"
        ])

        response["description"] = the_html

    context = {
        "form": form,
        "form_action": "/admin/edit_settings.html"
    }
    t = loader.get_template('admin/general_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response), status=rcode,
                        mimetype="application/json")
示例#10
0
def reject_suggestion(request, unit, suggid):
    json = {}
    translation_project = request.translation_project

    json["udbid"] = unit.id
    json["sugid"] = suggid
    if request.POST.get('reject'):
        try:
            sugg = unit.suggestion_set.get(id=suggid)
        except ObjectDoesNotExist:
            raise Http404

        if (not check_permission('review', request) and
            (not request.user.is_authenticated() or sugg and
                 sugg.user != request.profile)):
            raise PermissionDenied(_("You do not have rights to access "
                                     "review mode."))

        success = unit.reject_suggestion(suggid)
        if sugg is not None and success:
            # FIXME: we need a totally different model for tracking stats, this
            # is just lame
            suggstat, created = SuggestionStat.objects.get_or_create(
                    translation_project=translation_project,
                    suggester=sugg.user,
                    state='pending',
                    unit=unit.id,
            )
            suggstat.reviewer = request.profile
            suggstat.state = 'rejected'
            suggstat.save()

    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#11
0
def accept_suggestion(request, unit, suggid):
    json = {
        'udbid': unit.id,
        'sugid': suggid,
    }

    if request.POST.get('accept'):
        try:
            suggestion = unit.suggestion_set.get(id=suggid)
        except ObjectDoesNotExist:
            raise Http404

        unit.accept_suggestion(suggestion, request.translation_project,
                               request.profile)

        json['user_score'] = request.profile.public_score
        json['newtargets'] = [highlight_whitespace(target)
                              for target in unit.target.strings]
        json['newdiffs'] = {}
        for sugg in unit.get_suggestions():
            json['newdiffs'][sugg.id] = \
                    [highlight_diffs(unit.target.strings[i], target)
                     for i, target in enumerate(sugg.target.strings)]

    response = jsonify(json)
    return HttpResponse(response, content_type="application/json")
示例#12
0
文件: views.py 项目: Chipcius/pootle
def project_settings_edit(request, project):
    from pootle_project.forms import DescriptionForm
    form = DescriptionForm(request.POST, instance=project)

    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        if project.description:
            the_html = project.description
        else:
            the_html = u"".join([
                u'<p class="placeholder muted">',
                _(u"No description yet."),
                u"</p>",
            ])

        response["description"] = the_html

    context = {
        "form": form,
        "form_action": project.pootle_path + "edit_settings.html",
    }
    t = loader.get_template('admin/general_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response), status=rcode,
                        mimetype="application/json")
示例#13
0
def overview(request, project, dir_path, filename):
    """Languages overview for a given project."""
    item_func = (make_xlanguage_item if dir_path or filename
                                     else make_language_item)
    items = [item_func(item) for item in request.resource_obj.children]
    items.sort(lambda x, y: locale.strcoll(x['title'], y['title']))

    table_fields = ['name', 'progress', 'total', 'need-translation',
                    'suggestions', 'critical', 'last-updated', 'activity']
    table = {
        'id': 'project',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items,
    }

    ctx = get_overview_context(request)
    ctx.update({
        'project': project,
        'table': table,
        'stats': jsonify(request.resource_obj.get_stats()),

        'browser_extends': 'projects/base.html',
    })

    return render(request, 'browser/overview.html', ctx)
示例#14
0
文件: views.py 项目: AmesianX/pootle
def legal_agreement(request):
    """Displays the pending documents to be agreed by the current user."""
    pending_pages = LegalPage.objects.pending_user_agreement(request.user)
    form_class = agreement_form_factory(pending_pages, request.user)

    rcode = 200
    agreed = False

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

        if form.is_valid():
            # The user agreed, let's record the specific agreements
            agreed = True
            form.save()
        else:
            rcode = 400
    else:
        form = form_class()

    response = {'agreed': agreed}
    if not agreed:
        ctx = {
            'form': form,
        }
        template = loader.get_template('staticpages/agreement.html')
        response['form'] = template.render(RequestContext(request, ctx))

    return HttpResponse(jsonify(response), status=rcode,
                        mimetype='application/json')
示例#15
0
def edit_settings(request, translation_project):
    from pootle.core.url_helpers import split_pootle_path

    form = DescriptionForm(request.POST, instance=translation_project)
    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        if translation_project.description:
            response["description"] = translation_project.description
        else:
            response["description"] = (u'<p class="placeholder muted">%s</p>' %
                                       _(u"No description yet."))

    path_args = split_pootle_path(translation_project.pootle_path)[:2]
    context = {
        "form": form,
        "form_action": reverse('pootle-tp-admin-settings', args=path_args),
    }
    t = loader.get_template('admin/_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response),
                        status=rcode,
                        mimetype="application/json")
示例#16
0
def tp_settings_edit(request, translation_project):
    request.permissions = get_matching_permissions(
            get_profile(request.user), translation_project.directory
    )
    if not check_permission('administrate', request):
        raise PermissionDenied

    from pootle_translationproject.forms import DescriptionForm
    form = DescriptionForm(request.POST, instance=translation_project)
    response = {}
    if form.is_valid():
        form.save()
        response = {
                "intro": form.cleaned_data['description'],
                "valid": True,
        }
    context = {
            "form": form,
            "form_action": translation_project.pootle_path + "edit_settings.html",
    }
    t = loader.get_template('admin/general_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response), mimetype="application/json")
示例#17
0
def legal_agreement(request):
    """Displays the pending documents to be agreed by the current user."""
    pending_pages = LegalPage.objects.pending_user_agreement(request.user)
    form_class = agreement_form_factory(pending_pages, request.user)

    rcode = 200
    agreed = False

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

        if form.is_valid():
            # The user agreed, let's record the specific agreements
            agreed = True
            form.save()
        else:
            rcode = 400
    else:
        form = form_class()

    response = {'agreed': agreed}
    if not agreed:
        ctx = {
            'form': form,
        }
        template = loader.get_template('staticpages/agreement.html')
        response['form'] = template.render(RequestContext(request, ctx))

    return HttpResponse(jsonify(response),
                        status=rcode,
                        content_type='application/json')
示例#18
0
文件: views.py 项目: qdinar/pootle
def language_settings_edit(request, language):
    from pootle_language.forms import DescriptionForm
    form = DescriptionForm(request.POST, instance=language)

    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        if language.description:
            the_html = language.description
        else:
            the_html = u"".join([
                u'<p class="placeholder muted">',
                _(u"No description yet."), u"</p>"
            ])

        response["description"] = the_html

    context = {
        "form": form,
        "form_action": reverse('pootle-language-admin-settings',
                               args=[language.code]),
    }
    t = loader.get_template('admin/_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response), status=rcode,
                        mimetype="application/json")
示例#19
0
def get_view_units(request, units_queryset, store, limit=0):
    """Gets source and target texts excluding the editing unit.

    :return: An object in JSON notation that contains the source and target
             texts for units that will be displayed before and after editing
             unit.

             If asked by using the ``meta`` and ``pager`` parameters,
             metadata and pager information will be calculated and returned
             too.
    """
    current_unit = None
    json = {}

    try:
        limit = int(limit)
    except ValueError:
        limit = None

    if not limit:
        limit = request.profile.get_unit_rows()

    step_queryset = get_step_query(request, units_queryset)

    # Return metadata it has been explicitely requested
    if request.GET.get('meta', False):
        tp = request.translation_project
        json["meta"] = {"source_lang": tp.project.source_language.code,
                        "source_dir": tp.project.source_language.get_direction(),
                        "target_lang": tp.language.code,
                        "target_dir": tp.language.get_direction(),
                        "project_style": tp.project.checkstyle}

    # Maybe we are trying to load directly a specific unit, so we have
    # to calculate its page number
    uid = request.GET.get('uid', None)
    if uid:
        current_unit = units_queryset.get(id=uid)
        preceding = _get_index_in_qs(step_queryset, current_unit, store)
        page = preceding / limit + 1
    else:
        page = None

    pager = paginate(request, step_queryset, items=limit, page=page)

    json["units"] = _build_units_list(pager.object_list)

    # Return paging information if requested to do so
    if request.GET.get('pager', False):
        json["pager"] = _build_pager_dict(pager)
        if not current_unit:
            try:
                json["uid"] = json["units"][0]["id"]
            except IndexError:
                pass
        else:
            json["uid"] = current_unit.id

    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#20
0
def projects_overview(request, project_set):
    """Page listing all projects"""
    items = [make_project_list_item(project)
             for project in project_set.children]
    items.sort(lambda x, y: locale.strcoll(x['title'], y['title']))

    table_fields = ['name', 'progress', 'total', 'need-translation',
                    'suggestions', 'critical', 'last-updated', 'activity']
    table = {
        'id': 'projects',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items,
    }

    ctx = get_overview_context(request)
    ctx.update({
        'table': table,
        'stats': jsonify(request.resource_obj.get_stats()),

        'browser_extends': 'projects/all/base.html',
    })

    response = render(request, 'browser/overview.html', ctx)
    response.set_cookie('pootle-language', 'projects')

    return response
示例#21
0
文件: views.py 项目: SinSiXX/pootle
def submit(request, unit):
    """Processes translation submissions and stores them in the database.

    :return: An object in JSON notation that contains the previous and last
             units for the unit next to unit ``uid``.
    """
    json = {}

    cantranslate = check_permission("translate", request)
    if not cantranslate:
        raise PermissionDenied(_("You do not have rights to access "
                                 "translation mode."))

    translation_project = request.translation_project
    language = translation_project.language

    if unit.hasplural():
        snplurals = len(unit.source.strings)
    else:
        snplurals = None

    # Update current unit instance's attributes
    unit.submitted_by = request.profile
    unit.submitted_on = datetime.utcnow()

    form_class = unit_form_factory(language, snplurals, request)
    form = form_class(request.POST, instance=unit)

    if form.is_valid():
        if form.updated_fields:
            # Store creation time so that it is the same for all submissions
            creation_time=datetime.utcnow()
            for field, old_value, new_value in form.updated_fields:
                sub = Submission(
                        creation_time=creation_time,
                        translation_project=translation_project,
                        submitter=request.profile,
                        unit=unit,
                        field=field,
                        type=SubmissionTypes.NORMAL,
                        old_value=old_value,
                        new_value=new_value,
                )
                sub.save()

            form.save()
            translation_submitted.send(
                    sender=translation_project,
                    unit=form.instance,
                    profile=request.profile,
            )

        rcode = 200
    else:
        # Form failed
        #FIXME: we should display validation errors here
        rcode = 400
        json["msg"] = _("Failed to process submission.")
    response = jsonify(json)
    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#22
0
def view(request):
    siteconfig = load_site_config()
    response = {}
    if request.POST:
        setting_form = GeneralSettingsForm(siteconfig, data=request.POST)
        if setting_form.is_valid():
            setting_form.save()
            load_site_config()
            response = {
                    "intro": setting_form.cleaned_data['DESCRIPTION'],
                    "valid": True,
            }
        if request.is_ajax():
            context = {
                    "form": setting_form,
                    "form_action": "/admin/general.html"
            }
            t = loader.get_template('admin/general_settings_form.html')
            c = RequestContext(request, context)
            response['form'] = t.render(c)

            return HttpResponse(jsonify(response), mimetype="application/json")
    else:
        setting_form = GeneralSettingsForm(siteconfig)

    template = 'admin/admin_general_settings.html'
    template_vars = {
        'form': setting_form,
        }
    return render_to_response(template, template_vars, context_instance=RequestContext(request))
示例#23
0
 def _ajax_error(self, rcode, msg):
     error_body = {
         'msg': msg,
     }
     response = jsonify(error_body)
     return HttpResponse(response, status=rcode,
                         mimetype="application/json")
示例#24
0
文件: views.py 项目: qdinar/pootle
def comment(request, unit):
    """Stores a new comment for the given ``unit``.

    :return: If the form validates, the cleaned comment is returned.
             An error message is returned otherwise.
    """
    # Update current unit instance's attributes
    unit.commented_by = request.profile
    unit.commented_on = timezone.now()

    language = request.translation_project.language
    form = unit_comment_form_factory(language)(request.POST, instance=unit,
                                               request=request)

    if form.is_valid():
        form.save()

        context = {
            'unit': unit,
            'language': language,
        }
        t = loader.get_template('editor/units/xhr_comment.html')
        c = RequestContext(request, context)

        json = {'comment': t.render(c)}
        rcode = 200
    else:
        json = {'msg': _("Comment submission failed.")}
        rcode = 400

    response = jsonify(json)

    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#25
0
文件: views.py 项目: atassumer/pootle
def edit_settings(request, translation_project):
    form = DescriptionForm(request.POST, instance=translation_project)
    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        if translation_project.description:
            response["description"] = translation_project.description
        else:
            response["description"] = (u'<p class="placeholder muted">%s</p>' %
                                       _(u"No description yet."))

    path_args = split_pootle_path(translation_project.pootle_path)[:2]
    context = {
        "form": form,
        "form_action": reverse('pootle-tp-admin-settings', args=path_args),
    }
    t = loader.get_template('admin/_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response), status=rcode,
                        mimetype="application/json")
示例#26
0
文件: views.py 项目: julen/pootle
def process_submit(request, unit, type):
    """
    Processes submissions and suggestions and stores them in the database.

    @return: An object in JSON notation that contains the previous
    and last units for the unit next to unit C{uid}.
    """
    json = {}
    cantranslate = check_permission("translate", request)
    cansuggest = check_permission("suggest", request)
    if type == 'submission' and not cantranslate or type == 'suggestion' and not cansuggest:
        raise PermissionDenied(
            _("You do not have rights to access translation mode."))

    translation_project = request.translation_project
    language = translation_project.language

    if unit.hasplural():
        snplurals = len(unit.source.strings)
    else:
        snplurals = None

    form_class = unit_form_factory(language, snplurals, request)
    form = form_class(request.POST, instance=unit)

    if form.is_valid():
        if type == 'submission':
            if form.instance._target_updated or \
               form.instance._translator_comment_updated or \
               form.instance._state_updated:
                form.save()
                translation_submitted.send(sender=translation_project,
                                           unit=form.instance,
                                           profile=request.profile)

                sub = Submission(translation_project=translation_project,
                                 submitter=request.profile)
                sub.save()
        elif type == 'suggestion':
            if form.instance._target_updated:
                #HACKISH: django 1.2 stupidly modifies instance on
                # model form validation, reload unit from db
                unit = Unit.objects.get(id=unit.id)
                sugg = unit.add_suggestion(form.cleaned_data['target_f'],
                                           request.profile)
                if sugg:
                    SuggestionStat.objects.get_or_create(
                        translation_project=translation_project,
                        suggester=request.profile,
                        state='pending',
                        unit=unit.id)
        rcode = 200
    else:
        # Form failed
        #FIXME: we should display validation errors here
        rcode = 400
        json["msg"] = _("Failed to process submit.")
    response = jsonify(json)
    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#27
0
def accept_suggestion(request, unit, suggid):
    json = {
        'udbid': unit.id,
        'sugid': suggid,
    }
    translation_project = request.translation_project

    if request.POST.get('accept'):
        try:
            suggestion = unit.suggestion_set.get(id=suggid)
        except ObjectDoesNotExist:
            raise Http404

        old_target = unit.target
        success = unit.accept_suggestion(suggid)

        json['newtargets'] = [highlight_whitespace(target)
                              for target in unit.target.strings]
        json['newdiffs'] = {}
        for sugg in unit.get_suggestions():
            json['newdiffs'][sugg.id] = \
                    [highlight_diffs(unit.target.strings[i], target)
                     for i, target in enumerate(sugg.target.strings)]

        if suggestion is not None and success:
            if suggestion.user:
                translation_submitted.send(sender=translation_project,
                                           unit=unit, profile=suggestion.user)

            # FIXME: we need a totally different model for tracking stats, this
            # is just lame
            suggstat, created = SuggestionStat.objects.get_or_create(
                    translation_project=translation_project,
                    suggester=suggestion.user,
                    state='pending',
                    unit=unit.id,
            )
            suggstat.reviewer = request.profile
            suggstat.state = 'accepted'
            suggstat.save()

            # For now assume the target changed
            # TODO: check all fields for changes
            creation_time = timezone.now()
            sub = Submission(
                    creation_time=creation_time,
                    translation_project=translation_project,
                    submitter=suggestion.user,
                    from_suggestion=suggstat,
                    unit=unit,
                    field=SubmissionFields.TARGET,
                    type=SubmissionTypes.SUGG_ACCEPT,
                    old_value=old_target,
                    new_value=unit.target,
            )
            sub.save()

    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#28
0
文件: views.py 项目: AmesianX/pootle
def get_overview_stats(request, path_obj, **kwargs):
    try:
        goal = Goal.objects.get(slug=request.GET.get('goalSlug', ''))
    except Goal.DoesNotExist:
        goal = None
    stats = request.resource_obj.get_stats(goal=goal)
    response = jsonify(stats)
    return HttpResponse(response, mimetype="application/json")
示例#29
0
 def _ajax_error(self, rcode, msg):
     error_body = {
         'msg': msg,
     }
     response = jsonify(error_body)
     return HttpResponse(response,
                         status=rcode,
                         content_type="application/json")
示例#30
0
文件: views.py 项目: cboylan/pootle
def get_overview_stats(request, path_obj, **kwargs):
    try:
        goal = Goal.objects.get(slug=request.GET.get('goalSlug', ''))
    except Goal.DoesNotExist:
        goal = None
    stats = request.resource_obj.get_stats(goal=goal)
    response = jsonify(stats)
    return HttpResponse(response, mimetype="application/json")
示例#31
0
def remove_paid_task(request, task_id=None):
    if request.method == 'DELETE':
        try:
            obj = PaidTask.objects.get(id=task_id)
            str = '%s\t%s\t%s' % (request.user.username, PAID_TASK_DELETED,
                                  obj)
            obj.delete()
            log(str)
            return HttpResponse(jsonify({'removed': 1}),
                                content_type="application/json")

        except PaidTask.DoesNotExist:
            return HttpResponseNotFound({}, content_type="application/json")

    return HttpResponseBadRequest(jsonify(
        {'error': _('Invalid request method')}),
                                  content_type="application/json")
示例#32
0
def users(request):
    User = get_user_model()
    json = list(
        User.objects.hide_meta().select_related('evernote_account').values(
            'id', 'username', 'full_name'))
    response = jsonify(json)

    return HttpResponse(response, content_type="application/json")
示例#33
0
文件: views.py 项目: cboylan/pootle
def get_units(request):
    """Gets source and target texts and its metadata.

    :return: A JSON-encoded string containing the source and target texts
        grouped by the store they belong to.

        The optional `count` GET parameter defines the chunk size to
        consider. The user's preference will be used by default.

        When the `initial` GET parameter is present, a sorted list of
        the result set ids will be returned too.
    """
    pootle_path = request.GET.get('path', None)
    if pootle_path is None:
        raise Http400(_('Arguments missing.'))

    units_qs = Unit.objects.get_for_path(pootle_path, request.user)
    step_queryset = get_step_query(request, units_qs)

    is_initial_request = request.GET.get('initial', False)
    chunk_size = request.GET.get("count", request.user.unit_rows)
    uids_param = filter(None, request.GET.get('uids', '').split(u','))
    uids = filter(None, map(to_int, uids_param))

    units = None
    unit_groups = []
    uid_list = []

    if is_initial_request:
        uid_list = list(step_queryset.values_list('id', flat=True))

        if len(uids) == 1:
            try:
                uid = uids[0]
                index = uid_list.index(uid)
                begin = max(index - chunk_size, 0)
                end = min(index + chunk_size + 1, len(uid_list))
                uids = uid_list[begin:end]
            except ValueError:
                raise Http404  # `uid` not found in `uid_list`
        else:
            count = 2 * chunk_size
            units = step_queryset[:count]

    if units is None and uids:
        units = step_queryset.filter(id__in=uids)

    units_by_path = groupby(units, lambda x: x.store.pootle_path)
    for pootle_path, units in units_by_path:
        unit_groups.append(_path_units_with_meta(pootle_path, units))

    response = {
        'unitGroups': unit_groups,
    }
    if uid_list:
        response['uIds'] = uid_list

    return HttpResponse(jsonify(response), mimetype="application/json")
示例#34
0
文件: views.py 项目: lexx777/pootle
def get_units(request):
    """Gets source and target texts and its metadata.

    :return: A JSON-encoded string containing the source and target texts
        grouped by the store they belong to.

        The optional `count` GET parameter defines the chunk size to
        consider. The user's preference will be used by default.

        When the `initial` GET parameter is present, a sorted list of
        the result set ids will be returned too.
    """
    pootle_path = request.GET.get('path', None)
    if pootle_path is None:
        raise Http400(_('Arguments missing.'))

    units_qs = Unit.objects.get_for_path(pootle_path, request.user)
    step_queryset = get_step_query(request, units_qs)

    is_initial_request = request.GET.get('initial', False)
    chunk_size = request.GET.get("count", request.user.unit_rows)
    uids_param = filter(None, request.GET.get('uids', '').split(u','))
    uids = filter(None, map(to_int, uids_param))

    units = None
    unit_groups = []
    uid_list = []

    if is_initial_request:
        uid_list = list(step_queryset.values_list('id', flat=True))

        if len(uids) == 1:
            try:
                uid = uids[0]
                index = uid_list.index(uid)
                begin = max(index - chunk_size, 0)
                end = min(index + chunk_size + 1, len(uid_list))
                uids = uid_list[begin:end]
            except ValueError:
                raise Http404  # `uid` not found in `uid_list`
        else:
            count = 2 * chunk_size
            units = step_queryset[:count]

    if units is None and uids:
        units = step_queryset.filter(id__in=uids)

    units_by_path = groupby(units, lambda x: x.store.pootle_path)
    for pootle_path, units in units_by_path:
        unit_groups.append(_path_units_with_meta(pootle_path, units))

    response = {
        'unitGroups': unit_groups,
    }
    if uid_list:
        response['uIds'] = uid_list

    return HttpResponse(jsonify(response), mimetype="application/json")
示例#35
0
文件: views.py 项目: julen/pootle
def get_view_units(request, units_queryset, limit=0):
    """
    @return: An object in JSON notation that contains the source and target
    texts for units that will be displayed before and after editing unit.

    If asked by using the 'meta' and 'pager' parameters, metadata and pager
    information will be calculated and returned too.
    """
    current_unit = None
    json = {}

    try:
        limit = int(limit)
    except ValueError:
        limit = None

    if not limit:
        limit = request.profile.get_unit_rows()

    step_queryset = get_step_query(request, units_queryset)

    # Return metadata it has been explicitely requested
    if request.GET.get('meta', False):
        tp = request.translation_project
        json["meta"] = {
            "source_lang": tp.project.source_language.code,
            "source_dir": tp.project.source_language.get_direction(),
            "target_lang": tp.language.code,
            "target_dir": tp.language.get_direction()
        }

    # Maybe we are trying to load directly a specific unit, so we have
    # to calculate its page number
    uid = request.GET.get('uid', None)
    if uid:
        current_unit = units_queryset.get(id=uid)
        preceding = _get_index_in_qs(step_queryset, current_unit)
        page = preceding / limit + 1
    else:
        page = None

    pager = paginate(request, step_queryset, items=limit, page=page)

    json["units"] = _build_units_list(pager.object_list)

    # Return paging information if requested to do so
    if request.GET.get('pager', False):
        json["pager"] = _build_pager_dict(pager)
        if not current_unit:
            try:
                json["uid"] = json["units"][0]["id"]
            except IndexError:
                pass
        else:
            json["uid"] = current_unit.id

    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#36
0
def users(request):
    json = list(
        User.objects.hide_defaults()
                    .select_related('evernote_account')
                    .values('id', 'username', 'first_name', 'last_name')
    )
    response = jsonify(json)

    return HttpResponse(response, mimetype="application/json")
示例#37
0
文件: views.py 项目: actsgo/pootle
def remove_paid_task(request, task_id=None):
    if request.method == 'DELETE':
        try:
            obj = PaidTask.objects.get(id=task_id)
            str = '%s\t%s\t%s' % (request.user.username,
                                  PAID_TASK_DELETED, obj)
            obj.delete()
            log(str)
            return HttpResponse(jsonify({'removed': 1}),
                                content_type="application/json")

        except PaidTask.DoesNotExist:
            return HttpResponseNotFound({}, content_type="application/json")

    return HttpResponseBadRequest(
        jsonify({'error': _('Invalid request method')}),
        content_type="application/json"
    )
示例#38
0
文件: views.py 项目: julen/pootle
def process_submit(request, unit, type):
    """
    Processes submissions and suggestions and stores them in the database.

    @return: An object in JSON notation that contains the previous
    and last units for the unit next to unit C{uid}.
    """
    json = {}
    cantranslate = check_permission("translate", request)
    cansuggest = check_permission("suggest", request)
    if type == "submission" and not cantranslate or type == "suggestion" and not cansuggest:
        raise PermissionDenied(_("You do not have rights to access translation mode."))

    translation_project = request.translation_project
    language = translation_project.language

    if unit.hasplural():
        snplurals = len(unit.source.strings)
    else:
        snplurals = None

    form_class = unit_form_factory(language, snplurals, request)
    form = form_class(request.POST, instance=unit)

    if form.is_valid():
        if type == "submission":
            if (
                form.instance._target_updated
                or form.instance._translator_comment_updated
                or form.instance._state_updated
            ):
                form.save()
                translation_submitted.send(sender=translation_project, unit=form.instance, profile=request.profile)

                sub = Submission(translation_project=translation_project, submitter=request.profile)
                sub.save()
        elif type == "suggestion":
            if form.instance._target_updated:
                # HACKISH: django 1.2 stupidly modifies instance on
                # model form validation, reload unit from db
                unit = Unit.objects.get(id=unit.id)
                sugg = unit.add_suggestion(form.cleaned_data["target_f"], request.profile)
                if sugg:
                    SuggestionStat.objects.get_or_create(
                        translation_project=translation_project,
                        suggester=request.profile,
                        state="pending",
                        unit=unit.id,
                    )
        rcode = 200
    else:
        # Form failed
        # FIXME: we should display validation errors here
        rcode = 400
        json["msg"] = _("Failed to process submit.")
    response = jsonify(json)
    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#39
0
文件: views.py 项目: actsgo/pootle
def users(request):
    User = get_user_model()
    json = list(
        User.objects.hide_meta()
                    .select_related('evernote_account')
                    .values('id', 'username', 'full_name')
    )
    response = jsonify(json)

    return HttpResponse(response, content_type="application/json")
示例#40
0
def qualitycheck_stats(request, translation_project, dir_path, filename=None):
    directory = request.directory
    store = request.store
    resource_obj = store or directory

    qc_stats = {}
    if resource_obj:
        qc_stats = resource_obj.get_checks()

    return HttpResponse(jsonify(qc_stats), mimetype="application/json")
示例#41
0
def get_units(request):
    """Gets source and target texts and its metadata.

    :return: A JSON-encoded object containing the source and target texts
        grouped by the store they belong to.

        When the ``pager`` GET parameter is present, pager information
        will be returned too.
    """
    pootle_path = request.GET.get('path', None)
    if pootle_path is None:
        raise Http400(_('Arguments missing.'))

    page = None

    request.profile = get_profile(request.user)
    limit = request.profile.get_unit_rows()

    units_qs = Unit.objects.get_for_path(pootle_path, request.profile)
    step_queryset = get_step_query(request, units_qs)

    # Maybe we are trying to load directly a specific unit, so we have
    # to calculate its page number
    uid = request.GET.get('uid', None)
    if uid is not None:
        try:
            # XXX: Watch for performance, might want to drop into raw SQL
            # at some stage
            uid_list = list(step_queryset.values_list('id', flat=True))
            preceding = uid_list.index(int(uid))
            page = preceding / limit + 1
        except ValueError:
            pass  # uid wasn't a number or not present in the results

    pager = paginate(request, step_queryset, items=limit, page=page)

    unit_groups = []
    units_by_path = groupby(pager.object_list, lambda x: x.store.pootle_path)
    for pootle_path, units in units_by_path:
        unit_groups.append(_path_units_with_meta(pootle_path, units))

    response = {
        'unit_groups': unit_groups,
    }

    if request.GET.get('pager', False):
        response['pager'] = {
            'count': pager.paginator.count,
            'number': pager.number,
            'num_pages': pager.paginator.num_pages,
            'per_page': pager.paginator.per_page,
        }

    return HttpResponse(jsonify(response), mimetype="application/json")
示例#42
0
def get_failing_checks(request, pathobj):
    """Gets a list of failing checks for the current object.

    :return: JSON string with a list of failing check categories which
             include the actual checks that are failing.
    """
    stats = get_raw_stats(pathobj)
    failures = get_quality_check_failures(pathobj, stats, include_url=False)

    response = jsonify(failures)

    return HttpResponse(response, mimetype="application/json")
示例#43
0
def reject_qualitycheck(request, unit, check_id):
    json = {}
    json["udbid"] = unit.id
    json["checkid"] = check_id
    if request.POST.get('reject'):
        try:
            unit.reject_qualitycheck(check_id)
        except ObjectDoesNotExist:
            raise Http404

    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#44
0
文件: views.py 项目: julen/pootle
def get_more_context(request, unit):
    """
    @return: An object in JSON notation that contains the source and target
    texts for units that are in context of unit C{uid}.
    """
    store = request.store
    json = {}
    gap = int(request.GET.get('gap', 0))

    json["ctxt"] = _filter_ctxt_units(store.units, unit, 2, gap)
    rcode = 200
    response = jsonify(json)
    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#45
0
def toggle_qualitycheck(request, unit, check_id):
    json = {}
    json["udbid"] = unit.id
    json["checkid"] = check_id

    try:
        unit.toggle_qualitycheck(check_id, bool(request.POST.get('mute')),
                                 request.profile)
    except ObjectDoesNotExist:
        raise Http404

    response = jsonify(json)
    return HttpResponse(response, content_type="application/json")
示例#46
0
def user_date_prj_activity(request):
    username = request.GET.get('username', None)
    month = request.GET.get('month', None)

    try:
        User = get_user_model()
        user = User.objects.get(username=username)
    except:
        user = ''

    json = get_activity_data(request, user, month)
    response = jsonify(json)

    return HttpResponse(response, content_type="application/json")
示例#47
0
def get_more_context(request, unit):
    """Retrieves more context units.

    :return: An object in JSON notation that contains the source and target
             texts for units that are in the context of unit ``uid``.
    """
    store = request.store
    json = {}
    gap = int(request.GET.get('gap', 0))
    qty = int(request.GET.get('qty', 1))

    json["ctx"] = _filter_ctx_units(store.units, unit, qty, gap)
    rcode = 200
    response = jsonify(json)
    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#48
0
def vote_up(request, unit, suggid):
    json = {}
    json["suggid"] = suggid
    if request.POST.get('up'):
        try:
            suggestion = unit.suggestion_set.get(id=suggid)
            from voting.models import Vote
            # Why can't it just return the vote object?
            Vote.objects.record_vote(suggestion, request.user, +1)
            json["voteid"] = Vote.objects.get_for_user(suggestion,
                                                       request.user).id
        except ObjectDoesNotExist:
            raise Http404(_("The suggestion or vote is not valid any more."))
    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#49
0
def clear_vote(request, voteid):
    json = {}
    json["voteid"] = voteid
    if request.POST.get('clear'):
        try:
            from voting.models import Vote
            vote = Vote.objects.get(pk=voteid)
            if vote.user != request.user:
                # No i18n, will not go to UI
                raise PermissionDenied("Users can only remove their own votes")
            vote.delete()
        except ObjectDoesNotExist:
            raise Http404
    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#50
0
文件: views.py 项目: cboylan/pootle
def reject_suggestion(request, unit, suggid):
    if request.POST.get('reject'):
        try:
            sugg = unit.suggestion_set.get(id=suggid)
        except ObjectDoesNotExist:
            raise Http404

        unit.reject_suggestion(sugg, request.translation_project, request.user)

    json = {
        'udbid': unit.id,
        'sugid': suggid,
    }
    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#51
0
def reject_qualitycheck(request, unit, checkid):
    json = {}
    json["udbid"] = unit.id
    json["checkid"] = checkid
    if request.POST.get('reject'):
        try:
            check = unit.qualitycheck_set.get(id=checkid)
            check.false_positive = True
            check.save()
            # update timestamp
            unit.save()
        except ObjectDoesNotExist:
            raise Http404

    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#52
0
def suggest(request, unit):
    """Processes translation suggestions and stores them in the database.

    :return: An object in JSON notation that contains the previous and last
             units for the unit next to unit ``uid``.
    """
    json = {}

    translation_project = request.translation_project
    language = translation_project.language

    if unit.hasplural():
        snplurals = len(unit.source.strings)
    else:
        snplurals = None

    form_class = unit_form_factory(language, snplurals, request)
    form = form_class(request.POST, instance=unit, request=request)

    if form.is_valid():
        if form.instance._target_updated:
            # TODO: Review if this hackish method is still necessary
            #HACKISH: django 1.2 stupidly modifies instance on
            # model form validation, reload unit from db
            unit = Unit.objects.get(id=unit.id)
            unit.add_suggestion(
                form.cleaned_data['target_f'],
                user=request.profile,
                similarity=form.cleaned_data['similarity'],
                mt_similarity=form.cleaned_data['mt_similarity'],
            )

            json['user_score'] = request.profile.public_score

        rcode = 200
    else:
        # Form failed
        #FIXME: we should display validation errors here
        rcode = 400
        json["msg"] = _("Failed to process suggestion.")

    response = jsonify(json)
    return HttpResponse(response,
                        status=rcode,
                        content_type="application/json")
示例#53
0
def evernote_reports(request):
    User = get_user_model()
    now = make_aware(datetime.now())

    ctx = {
        'users': jsonify(map(
            lambda x: {'id': x.username, 'text': escape(x.formatted_name)},
            User.objects.hide_meta()
        )),
        'user_rates_form': UserRatesForm(),
        'paid_task_form': PaidTaskForm(),
        'now': now.strftime('%Y-%m-%d %H:%M:%S'),
        'admin_report': True,
        'paid_task_types': PaidTaskTypes,
    }

    return render_to_response('admin/reports.html', ctx,
                              context_instance=RequestContext(request))
示例#54
0
文件: views.py 项目: julen/pootle
def accept_suggestion(request, unit, suggid):
    json = {}
    translation_project = request.translation_project
    json["udbid"] = unit.id
    json["sugid"] = suggid
    if request.POST.get('accept'):
        try:
            suggestion = unit.suggestion_set.get(id=suggid)
        except ObjectDoesNotExist:
            raise Http404

        success = unit.accept_suggestion(suggid)
        json['newtargets'] = [
            highlight_whitespace(target) for target in unit.target.strings
        ]
        json['newdiffs'] = {}
        for sugg in unit.get_suggestions():
            json['newdiffs'][sugg.id] = [highlight_diffs(unit.target.strings[i], target) \
                                         for i, target in enumerate(sugg.target.strings)]

        if suggestion is not None and success:
            if suggestion.user:
                translation_submitted.send(sender=translation_project,
                                           unit=unit,
                                           profile=suggestion.user)
            #FIXME: we need a totally different model for tracking stats, this is just lame
            if suggestion.user != request.profile:
                suggstat, created = SuggestionStat.objects.get_or_create(
                    translation_project=translation_project,
                    suggester=suggestion.user,
                    state='pending',
                    unit=unit.id)
                suggstat.reviewer = request.profile
                suggstat.state = 'accepted'
                suggstat.save()
            else:
                suggstat = None
            sub = Submission(translation_project=translation_project,
                             submitter=suggestion.user,
                             from_suggestion=suggstat)
            sub.save()
    response = jsonify(json)
    return HttpResponse(response, mimetype="application/json")
示例#55
0
def reject_suggestion(request, unit, suggid):
    json = {
        'udbid': unit.id,
        'sugid': suggid,
    }

    if request.POST.get('reject'):
        try:
            sugg = unit.suggestion_set.get(id=suggid)
        except ObjectDoesNotExist:
            raise Http404

        unit.reject_suggestion(sugg, request.translation_project,
                               request.profile)

        json['user_score'] = request.profile.public_score

    response = jsonify(json)
    return HttpResponse(response, content_type="application/json")
示例#56
0
    def get_context_data(self, **kwargs):
        languages = Language.objects.exclude(code='templates')
        language_choices = [(lang.id, unicode(lang)) for lang in languages]
        try:
            english = Language.objects.get(code='en')
            default_language = english.id
        except Language.DoesNotExist:
            default_language = languages[0].id

        kwargs['form_choices'] = jsonify({
            'checkstyle': Project.checker_choices,
            'localfiletype': filetype_choices,
            'source_language': language_choices,
            'treestyle': Project.treestyle_choices,
            'defaults': {
                'source_language': default_language,
            },
        })
        return kwargs
示例#57
0
文件: views.py 项目: shayanb/pootle
def delete_comment(request, unit):
    """Deletes a comment by blanking its contents and records a new
    submission.
    """
    unit.commented_by = None
    unit.commented_on = None

    language = request.translation_project.language
    comment_form_class = unit_comment_form_factory(language)
    form = comment_form_class({}, instance=unit, request=request)

    if form.is_valid():
        form.save()
        json = {}
        rcode = 200
    else:
        json = {'msg': _("Failed to remove comment.")}
        rcode = 400

    response = jsonify(json)

    return HttpResponse(response, status=rcode, mimetype="application/json")
示例#58
0
文件: views.py 项目: notz/pootle
def language_settings_edit(request, language_code):
    language = get_object_or_404(Language, code=language_code)
    request.permissions = get_matching_permissions(get_profile(request.user),
                                                   language.directory)
    if not check_permission('administrate', request):
        raise PermissionDenied

    from pootle_language.forms import DescriptionForm
    form = DescriptionForm(request.POST, instance=language)

    response = {}
    rcode = 400

    if form.is_valid():
        form.save()
        rcode = 200

        if language.description_html:
            the_html = language.description_html
        else:
            the_html = u"".join([
                u'<p class="placeholder muted">',
                _(u"No description yet."), u"</p>"
            ])

        response["description_html"] = the_html

    context = {
        "form": form,
        "form_action": language.pootle_path + "edit_settings.html",
    }
    t = loader.get_template('admin/general_settings_form.html')
    c = RequestContext(request, context)
    response['form'] = t.render(c)

    return HttpResponse(jsonify(response),
                        status=rcode,
                        mimetype="application/json")
示例#59
0
def save_comment(request, unit):
    """Stores a new comment for the given ``unit``.

    :return: If the form validates, the cleaned comment is returned.
             An error message is returned otherwise.
    """
    # Update current unit instance's attributes
    unit.commented_by = request.profile
    unit.commented_on = timezone.now()

    language = request.translation_project.language
    form = unit_comment_form_factory(language)(request.POST,
                                               instance=unit,
                                               request=request)

    if form.is_valid():
        form.save()

        context = {
            'unit': unit,
            'language': language,
        }
        t = loader.get_template('editor/units/xhr_comment.html')
        c = RequestContext(request, context)

        json = {'comment': t.render(c)}
        rcode = 200
    else:
        json = {'msg': _("Comment submission failed.")}
        rcode = 400

    response = jsonify(json)

    return HttpResponse(response,
                        status=rcode,
                        content_type="application/json")