示例#1
0
文件: views.py 项目: waytai/Repos.io
def note_delete(request):
    """
    Delete a note for the current user
    """

    result = {}
    message_func = messages.success
    message_type = 'message'

    form = NoteDeleteForm(request.POST)
    if form.is_valid():
        noted_object = form.get_related_object()
        form.save()
        message = 'Your private note for `%s` was deleted'
    else:
        noted_object = form.get_related_object()
        if not noted_object:
            return HttpResponseBadRequest('Vilain :)')
        message = 'We were unable to delete your note for `%s`!'
        message_func = messages.error
        message_type = 'error'

    message = message % noted_object

    if request.is_ajax():
        result[message_type] = message
        return JSONResponse(result)
    else:
        message_func(request, message)
        return redirect(noted_object or '/')
示例#2
0
def note_delete(request):
    """
    Delete a note for the current user
    """

    result = {}
    message_func = messages.success
    message_type = 'message'

    form = NoteDeleteForm(request.POST)
    if form.is_valid():
        noted_object = form.get_related_object()
        form.save()
        message = 'Your private note for `%s` was deleted'
    else:
        noted_object = form.get_related_object()
        if not noted_object:
            return HttpResponseBadRequest('Vilain :)')
        message = 'We were unable to delete your note for `%s`!'
        message_func = messages.error
        message_type = 'error'

    message = message % noted_object

    if request.is_ajax():
        result[message_type] = message
        return JSONResponse(result)
    else:
        message_func(request, message)
        return redirect(noted_object or '/')
示例#3
0
def note_delete(request):
    """
    Delete a note for the current user
    """
    form = NoteDeleteForm(request.POST)
    if form.is_valid():
        noted_object = form.get_related_object()
        form.save()
        messages.success(request, 'Your private note was deleted')
    else:
        noted_object = form.get_related_object()
        if not noted_object:
            return HttpResponseBadRequest('Vilain :)')
        messages.error(request, 'We were unable to delete your note !')

    return return_from_editor(request, noted_object)
示例#4
0
def edit_private(object_str):
    """
    Display the the private editor for the given object. `object_str` is the object
    representaiton as defined by the `simple_str` method in the core module.
    """
    if not (object_str and globals.user and globals.user.is_authenticated()):
        return {}

    try:
        # find the object
        edit_object = get_object_from_str(object_str)
        app_label, model_name = get_app_and_model(edit_object)

        # get private data
        note = edit_object.get_user_note()
        private_tags = edit_object.get_user_tags()

        # special tags
        flags_and_tags = split_tags_and_flags(private_tags, model_name)

        # get other private tags
        other_tags = Tag.objects.filter(
            **{'private_%s_tags__owner' % model_name: globals.user})
        if private_tags:
            other_tags = other_tags.exclude(
                id__in=[t.id for t in private_tags])
        if flags_and_tags['special']:
            other_tags = other_tags.exclude(
                slug__in=[t['slug'] for t in flags_and_tags['special']])
        other_tags = other_tags.distinct()

        # for tags url
        if model_name == 'account':
            model_name_plural = 'accounts'
        else:
            model_name_plural = 'repositories'

        # urls for edit link and when_finished link
        if globals.request.is_ajax():
            when_finished = globals.request.META.get('HTTP_REFERER')
            if when_finished:
                host = 'http%s://%s' % ('s' if globals.request.is_secure() else
                                        '', globals.request.get_host())
                if when_finished.startswith(host):
                    when_finished = when_finished[len(host):]
                else:
                    when_finished = None
            if not when_finished:
                when_finished = edit_object.get_absolute_url()
            edit_url = when_finished + '%sedit_extra=%s&when_finished=%s' % (
                '&' if '?' in when_finished else '?',
                edit_object.simple_str(),
                urlquote(when_finished),
            )
        else:
            when_finished = get_request_param(globals.request, 'when_finished')
            edit_url = get_request_param(globals.request, 'edit_url',
                                         globals.request.get_full_path())

        return dict(
            edit_object=edit_object,
            note_save_form=NoteForm(instance=note) if note else NoteForm(
                noted_object=edit_object),
            note_delete_form=NoteDeleteForm(instance=note) if note else None,
            tag_save_form=TagsBaseForm(tagged_object=edit_object),
            tags_delete_form=TagsDeleteForm(
                tagged_object=edit_object) if private_tags else None,
            private_tags=flags_and_tags['normal'],
            other_tags=other_tags,
            special_tags=flags_and_tags['special'],
            used_special_tags=flags_and_tags['special_used'],
            url_tags=reverse('dashboard_tags',
                             kwargs=dict(obj_type=model_name_plural)),
            edit_url=edit_url,
            when_finished=when_finished,
        )

    except:
        return {}