コード例 #1
0
ファイル: ajax.py プロジェクト: Milstein/imaginationforpeople
def _answer_save(request, language_code, project_slug, project_translation, question, value):
    project = project_translation.master
    question = get_object_or_404(Question, id=question)

    if value:
        #It looks like there are unforeseen consequences to a nani changeset:https://github.com/KristianOellegaard/django-hvad/commit/aba974f79bdaf9fff46c01b1d4b3e941032d02dd
        #It tries to create duplicate entries in the db
        #answer, created = Answer.objects.language(language_code).get_or_create(project=project,
        #                                                                       question=question)
        #Let's work around it:
        base_answer, created = Answer.objects.untranslated().get_or_create(project=project,
                                                                      question=question)

        if created or language_code not in base_answer.get_available_languages():
            answer = base_answer.translate(language_code)
        else:
            answer = Answer.objects.language(language_code).get(pk=base_answer.pk)
        #End workaround
        
        # Remove html tags
        value = strip_tags(value)
        answer.content = value
        answer.save()
        
        # Generate a diff
        diffs = make_diffs_for_object(answer.translations.get(language_code=language_code),
                                      'content',
                                      answer.content)

        if request.user.is_anonymous():
            revision.user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
        else:
            revision.user = request.user

        # Create an action
        answer_action = action_create(actor=request.user, verb='edit_pjquestion', action_object=answer, target=project)
        revision.add_meta(VersionActivity, action=answer_action, diffs=diffs)
        
        response_dict = dict(text=linebreaksbr(urlize(value)), # Make line breaks and link urls
                             redirect=project_slug is None,
                             redirect_url=project.get_absolute_url())

        return HttpResponse(simplejson.dumps(response_dict), 'application/json')
    else:
        return HttpResponseNotFound()
コード例 #2
0
ファイル: ajax.py プロジェクト: Milstein/imaginationforpeople
def _textfield_save(request, language_code, project_slug, project_translation, section, value):
    # Resolve the fieldname
    fieldname = TEXTFIELD_MAPPINGS[section]
    FieldForm = modelform_factory(I4pProjectTranslation, fields=(fieldname,))

    # prevent html tags from being saved
    value = strip_tags(value)
    
    form = FieldForm({fieldname: value}, instance=project_translation)

    if form.is_valid():
        response_dict = {}
        project_translation = form.save()
        if project_translation._meta.get_field(fieldname).choices:
            text = getattr(project_translation, "get_%s_display" % fieldname)()
            if fieldname == "completion_progress":
                response_dict["completion_progress"] = getattr(project_translation,fieldname)
        else:
            # Generate line breaks and link urls if found
            text = linebreaksbr(urlize(value))

        if request.user.is_anonymous():
            revision.user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
        else:
            revision.user = request.user
        project = project_translation.master
        pj_translation_action = action_create(actor=request.user, verb='edit_pjfield', action_object=project_translation, target=project)
        revision.add_meta(VersionActivity, action=pj_translation_action)
            
        response_dict.update({'text': text or '',
                              'redirect': project_slug is None,
                              'redirect_url': project_translation.master.get_absolute_url()})

        return HttpResponse(simplejson.dumps(response_dict), 'application/json')
    else:
        return HttpResponseNotFound()