コード例 #1
0
ファイル: views.py プロジェクト: kaleissin/CALS
def show_features_for_language(request, *args, **kwargs):
    me = 'language'
    lang = _get_lang(all=True, *args, **kwargs)
    may_edit, (is_admin, is_manager) = may_edit_lang(request.user, lang)
    categories = Category.objects.active().select_related().order_by('id')
    cats = []
    for category in categories:
        try:
            lf = LanguageFeature.objects.filter(
                language=lang,
                feature__category=category).order_by('feature__id')
        except LanguageFeature.DoesNotExist:
            continue
        if lf:
            cats.append({'name': category.name, 'features': lf})

    # Nav for comparisons
    cform = CompareTwoForm()
    if request.method == 'POST':
        cform = CompareTwoForm(data=request.POST)
        if cform.is_valid():
            return _compare(request, (lang.slug, ))

    data = {
        'object': lang,
        'categories': cats,
        'me': me,
        'cform': cform,
        'may_edit': may_edit,
    }
    return render(request, 'cals/language/features_overview.html', data)
コード例 #2
0
ファイル: views.py プロジェクト: kaleissin/CALS
def show_features_for_language(request, *args, **kwargs):
    me = 'language'
    lang = _get_lang(all=True, *args, **kwargs)
    may_edit, (is_admin, is_manager) = may_edit_lang(request.user, lang)
    categories = Category.objects.active().select_related().order_by('id')
    cats = []
    for category in categories:
        try:
            lf = LanguageFeature.objects.filter(language=lang, feature__category=category).order_by('feature__id')
        except LanguageFeature.DoesNotExist:
            continue
        if lf:
            cats.append({'name': category.name, 'features': lf})

    # Nav for comparisons
    cform = CompareTwoForm()
    if request.method == 'POST':
        cform = CompareTwoForm(data=request.POST)
        if cform.is_valid():
            return _compare(request, (lang.slug,))

    data = {'object': lang,
            'categories': cats,
            'me': me,
            'cform': cform,
            'may_edit': may_edit,
    }
    return render(request, 'cals/language/features_overview.html', data)
コード例 #3
0
ファイル: decorators.py プロジェクト: kaleissin/CALS
 def dispatch(self, request, *args, **kwargs):
     language = Language.objects.get(slug=kwargs.get('language', None))
     may_edit, (admin, manager) = may_edit_lang(request.user, language)
     if not may_edit:
         error_msg = 'You are not authorized to edit "%s"' % language
         return HttpResponseForbidden(error_msg)
     self.user_is_manager = manager
     self.user_is_admin = admin
     return super(MayEditMixin, self).dispatch(request, *args, **kwargs)
コード例 #4
0
ファイル: views.py プロジェクト: kaleissin/CALS
 def get_object(self, queryset=None):
     self.language = _get_lang(**self.kwargs)
     self.feature = get_object_or_404(Feature, id=self.kwargs.get('feature', None))
     self.object = get_object_or_404(LanguageFeature, language=self.language, feature=self.feature)
     may_edit, (admin, manager) = may_edit_lang(self.request.user, self.language)
     self.may_edit = may_edit
     self.user_is_superuser = admin
     self.user_is_manager = manager
     return self.object
コード例 #5
0
 def get_object(self, queryset=None):
     self.language = _get_lang(**self.kwargs)
     self.feature = get_object_or_404(Feature,
                                      id=self.kwargs.get('feature', None))
     self.object = get_object_or_404(LanguageFeature,
                                     language=self.language,
                                     feature=self.feature)
     may_edit, (admin, manager) = may_edit_lang(self.request.user,
                                                self.language)
     self.may_edit = may_edit
     self.user_is_superuser = admin
     self.user_is_manager = manager
     return self.object
コード例 #6
0
ファイル: views.py プロジェクト: kaleissin/CALS
def show_language(request, *args, **kwargs):
    me = 'language'
    lang = _get_lang(all=True, *args, **kwargs)
    may_edit, (is_admin, is_manager) = may_edit_lang(request.user, lang)

    # Nav for comparisons
    cform = CompareTwoForm()
    if request.method == 'POST':
        cform = CompareTwoForm(data=request.POST)
        if cform.is_valid():
            return _compare(request, (lang.slug,))

    data = {'object': lang,
            'me': me,
            'cform': cform,
            'may_edit': may_edit,
    }
    return render(request, 'cals/language/overview.html', data)
コード例 #7
0
ファイル: views.py プロジェクト: kaleissin/CALS
def show_language(request, *args, **kwargs):
    me = 'language'
    lang = _get_lang(all=True, *args, **kwargs)
    may_edit, (is_admin, is_manager) = may_edit_lang(request.user, lang)

    # Nav for comparisons
    cform = CompareTwoForm()
    if request.method == 'POST':
        cform = CompareTwoForm(data=request.POST)
        if cform.is_valid():
            return _compare(request, (lang.slug, ))

    data = {
        'object': lang,
        'me': me,
        'cform': cform,
        'may_edit': may_edit,
    }
    return render(request, 'cals/language/overview.html', data)
コード例 #8
0
ファイル: views.py プロジェクト: kaleissin/CALS
def change_language(request, *args, **kwargs):
    me = 'language'
    state = 'change'
    lang = _get_lang(*args, **kwargs)
    old_name = lang.name
    user = request.user

    may_edit, (is_admin, is_manager) = may_edit_lang(user, lang)
    if not may_edit:
        return HttpResponseForbidden(error_forbidden)

    _LOG.info('%s about to change %s', user, lang)

    langform = LanguageForm(instance=lang)
    #profile = user.profile
    manager = lang.manager
    if is_manager:
        editorform = EditorForm(instance=lang)
    else:
        editorform = None
    _LOG.info('User is manager: %s', user == lang.manager)
    # sort values into categories
    cats = make_feature_list_for_lang(lang)

    if request.method == 'POST':
        langform = LanguageForm(data=request.POST,
                                instance=lang,
                                initial=request.POST)
        try:
            if langform.is_valid():
                with transaction.atomic():
                    new_lang = langform.save(commit=False)
                    new_name = new_lang.name
                    if new_name != old_name and language_exists(new_name):
                        new_name = escape(new_name)
                        url = '/language/%s/' % lang.get_slug()
                        msg = '''The name "%s" is taken,
                        see <a href="%s">%s</a>''' % (new_name, url, new_name)
                        messages.error(request, msg)
                    else:
                        _LOG.info('Actually changing %s!', lang)

                        lang = _change_editors_managers(
                            request, manager, new_lang, langform)

                        lang.last_modified_by = user

                        # break out in separate function
                        # greeting
                        greetingexercise = TranslationExercise.objects.get(
                            id=1)
                        new_greeting = lang.greeting
                        try:
                            greetingtrans = Translation.objects.get(
                                language=lang,
                                exercise=greetingexercise,
                                translator=user)
                        except Translation.DoesNotExist:
                            greetingtrans = None
                        if new_greeting:
                            if greetingtrans:
                                if new_greeting != greetingtrans.translation:
                                    greetingtrans.translation = new_greeting
                            else:
                                Translation.objects.create(
                                    language=lang,
                                    exercise=greetingexercise,
                                    translator=user,
                                    translation=new_greeting)
                        else:
                            if greetingtrans:
                                greetingtrans.delete()
                        # values
                        lang = set_featurevalues_for_lang(
                            lang, request.POST.getlist('value'))

                        # Final save
                        lang.save(user=user)
                        lang, tags_changed = set_tags_for_lang(
                            langform.cleaned_data['tags'], lang)
                        streamaction.send(user,
                                          verb='updated',
                                          action_object=lang)
                        return HttpResponseRedirect('.')
            else:
                error = "Couldn't change language-description: " + str(
                    langform.errors)
                messages.error(request, error)
        except ValueError:
            assert False, langform
    data = {
        'form': langform,
        'categories': cats,
        'editorform': editorform,
        #'moreinfoformset': moreinfoformset,
        'me': me,
        'state': state,
    }
    return render(request, 'language_form.html', data)
コード例 #9
0
def change_language(request, *args, **kwargs):
    me = 'language'
    state = 'change'
    lang = _get_lang(*args, **kwargs)
    old_name = lang.name
    user = request.user

    may_edit, (is_admin, is_manager) = may_edit_lang(user, lang)
    if not may_edit:
        return HttpResponseForbidden(error_forbidden)

    _LOG.info('%s about to change %s', user, lang)

    langform = LanguageForm(instance=lang)
    #profile = user.profile
    manager = lang.manager
    if is_manager:
        editorform = EditorForm(instance=lang)
    else:
        editorform = None
    _LOG.info('User is manager: %s', user == lang.manager)
    # sort values into categories
    cats = make_feature_list_for_lang(lang)

    if request.method == 'POST':
        langform = LanguageForm(data=request.POST, instance=lang, initial=request.POST)
        try:
            if langform.is_valid():
                with transaction.atomic():
                    new_lang = langform.save(commit=False)
                    new_name = new_lang.name
                    if new_name != old_name and language_exists(new_name):
                        new_name = escape(new_name)
                        url = '/language/%s/' % lang.get_slug()
                        msg = '''The name "%s" is taken,
                        see <a href="%s">%s</a>''' % (new_name, url, new_name)
                        messages.error(request, msg)
                    else:
                        _LOG.info('Actually changing %s!', lang)

                        lang = _change_editors_managers(request, manager, new_lang, langform)

                        lang.last_modified_by = user

                        # break out in separate function
                        # greeting
                        greetingexercise = TranslationExercise.objects.get(id=1)
                        new_greeting = lang.greeting
                        try:
                            greetingtrans = Translation.objects.get(language=lang, exercise=greetingexercise,
                                    translator=user)
                        except Translation.DoesNotExist:
                            greetingtrans = None
                        if new_greeting:
                            if greetingtrans:
                                if new_greeting != greetingtrans.translation:
                                    greetingtrans.translation = new_greeting
                            else:
                                Translation.objects.create(language=lang,
                                        exercise=greetingexercise, translator=user,
                                        translation=new_greeting)
                        else:
                            if greetingtrans:
                                greetingtrans.delete()
                        # values
                        lang = set_featurevalues_for_lang(lang, request.POST.getlist('value'))

                        # Final save
                        lang.save(user=user)
                        lang, tags_changed = set_tags_for_lang(langform.cleaned_data['tags'], lang)
                        streamaction.send(user, verb='updated', action_object=lang)
                        return HttpResponseRedirect('.')
            else:
                error = "Couldn't change language-description: " + str(langform.errors)
                messages.error(request, error)
        except ValueError:
            assert False, langform
    data = {'form': langform,
            'categories': cats,
            'editorform': editorform,
            #'moreinfoformset': moreinfoformset,
            'me': me,
            'state': state,}
    return render(request, 'language_form.html', data)
コード例 #10
0
def describe_languagefeature(request, *args, **kwargs):
    me = 'language'
    lang = _get_lang(*args, **kwargs)
    may_edit, _ = may_edit_lang(request.user, lang)
    if not may_edit:
        return HttpResponseForbidden(error_forbidden)

    feature = get_object_or_404(Feature, id=kwargs.get('feature', None))
    lf = get_object_or_404(LanguageFeature, language=lang, feature=feature)
    value_str = '%s_%s' % (feature.id, lf.value.id)
    preview = ''
    preview_value = ''
    link = '/language/%s/feature/%i/' % (lang.slug, feature.id)
    new_xhtml = ''
    lfd = lf.description
    if lfd:
        new_xhtml = lfd.make_xhtml()

    if request.method == 'POST':
        if lf.description:
            descriptionform = DescriptionForm(data=request.POST,
                                              instance=lf.description)
        else:
            descriptionform = DescriptionForm(data=request.POST)
        valueform = FeatureValueForm(feature=feature, data=request.POST)

        if valueform.is_valid():
            new_f, new_v = list(
                map(int,
                    valueform.cleaned_data.get('value', value_str).split('_')))
            if not new_v:
                messages.error(request, "Cannot delete a feature that way")
                return HttpResponseRedirect(link + 'change')

            new_fv = FeatureValue.objects.get(feature=feature, id=new_v)
            preview_value = new_fv

        new_lfd = None
        if descriptionform.is_valid():
            # Need to prevent extraenous saving here because of versioning
            new_lfd = descriptionform.save(commit=False)

            new_xhtml = new_lfd.make_xhtml()

        if request.POST.get('preview'):
            preview = new_xhtml
            msg = "You are previewing the description of '%s: %s' for %s" % (
                feature, new_fv, lang)
            messages.info(request, msg)
            if not new_lfd:
                descriptionform = DescriptionForm()
        elif request.POST.get('submit'):
            # value
            value_change = ''
            if new_v and new_f == feature.id and new_v != lf.value.id:
                lf.value = new_fv
                lf.save()
                value_change = 'Value now "%s." ' % lf.value

            # description
            desc_change = ''
            # Add/change desc
            if new_lfd and new_xhtml:
                if not lf.description or new_lfd.freetext != lf.description.freetext \
                        or new_xhtml != lf.description.freetext_xhtml \
                        or lfd.freetext_type != lf.description.freetext_type:
                    new_lfd.content_type = ContentType.objects.get_for_model(
                        lf)
                    new_lfd.object_id = lf.id
                    new_lfd.save(user=request.user)
                    desc_change = 'Description changed.'
            # Delete desc
            else:
                if lfd:
                    lfd.delete()
                descriptionform = DescriptionForm()
            messages.info(request, '%s%s' % (value_change, desc_change))
            return HttpResponseRedirect(link)
    else:
        valueform = FeatureValueForm(feature=feature,
                                     initial={'value': value_str})

        if lf.description:
            descriptionform = DescriptionForm(instance=lf.description)
        else:
            descriptionform = DescriptionForm()

    data = {
        'me': me,
        'form': descriptionform,
        'language': lang,
        'feature': feature,
        'object': lf,
        'valueform': valueform,
        'preview': preview,
        'preview_value': preview_value,
        'may_edit': may_edit,
        'user': request.user,
        'description': lf.description,
    }
    return render(request, 'languagefeature/language_description_form.html',
                  data)
コード例 #11
0
ファイル: views.py プロジェクト: kaleissin/CALS
def describe_languagefeature(request, *args, **kwargs):
    me = 'language'
    lang = _get_lang(*args, **kwargs)
    may_edit, _ = may_edit_lang(request.user, lang)
    if not may_edit:
        return HttpResponseForbidden(error_forbidden)

    feature = get_object_or_404(Feature, id=kwargs.get('feature', None))
    lf = get_object_or_404(LanguageFeature, language=lang, feature=feature)
    value_str = '%s_%s' % (feature.id, lf.value.id)
    preview = ''
    preview_value = ''
    link = '/language/%s/feature/%i/' % (lang.slug, feature.id)
    new_xhtml = ''
    lfd = lf.description
    if lfd:
        new_xhtml = lfd.make_xhtml()

    if request.method == 'POST':
        if lf.description:
            descriptionform = DescriptionForm(data=request.POST, instance=lf.description)
        else:
            descriptionform = DescriptionForm(data=request.POST)
        valueform = FeatureValueForm(feature=feature, data=request.POST)

        if valueform.is_valid():
            new_f, new_v = list(map(int, valueform.cleaned_data.get('value', value_str).split('_')))
            if not new_v:
                messages.error(request, "Cannot delete a feature that way")
                return HttpResponseRedirect(link + 'change')

            new_fv = FeatureValue.objects.get(feature=feature, id=new_v)
            preview_value = new_fv

        new_lfd = None
        if descriptionform.is_valid():
            # Need to prevent extraenous saving here because of versioning
            new_lfd = descriptionform.save(commit=False)

            new_xhtml = new_lfd.make_xhtml()

        if request.POST.get('preview'):
            preview = new_xhtml
            msg = "You are previewing the description of '%s: %s' for %s" % (feature, new_fv, lang)
            messages.info(request, msg)
            if not new_lfd:
                descriptionform = DescriptionForm()
        elif request.POST.get('submit'):
            # value
            value_change = ''
            if new_v and new_f == feature.id and new_v != lf.value.id:
                lf.value = new_fv
                lf.save()
                value_change = 'Value now "%s." ' % lf.value

            # description
            desc_change = ''
            # Add/change desc
            if new_lfd and new_xhtml:
                if not lf.description or new_lfd.freetext != lf.description.freetext \
                        or new_xhtml != lf.description.freetext_xhtml \
                        or lfd.freetext_type != lf.description.freetext_type:
                    new_lfd.content_type = ContentType.objects.get_for_model(lf)
                    new_lfd.object_id = lf.id
                    new_lfd.save(user=request.user)
                    desc_change = 'Description changed.'
            # Delete desc
            else:
                if lfd:
                    lfd.delete()
                descriptionform = DescriptionForm()
            messages.info(request, '%s%s' % (value_change, desc_change))
            return HttpResponseRedirect(link)
    else:
        valueform = FeatureValueForm(feature=feature, initial={'value': value_str})

        if lf.description:
            descriptionform = DescriptionForm(instance=lf.description)
        else:
            descriptionform = DescriptionForm()

    data = {'me': me,
            'form': descriptionform,
            'language': lang,
            'feature': feature,
            'object': lf,
            'valueform': valueform,
            'preview': preview,
            'preview_value': preview_value,
            'may_edit': may_edit,
            'user': request.user,
            'description': lf.description,
            }
    return render(request, 'languagefeature/language_description_form.html', data)