Beispiel #1
0
def tags_box(context, object):

    request = context['request']

    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    tags = object.tags.all()
    form = None
    if 'tags-edit' in request.GET:
        if request.POST.get('tags_object', 0) == unicode(object.id):
            form = TagsForm(tags, request.POST)
            if form.is_valid():
                if 'multicomplete_tags' in request.POST:
                    tag_names = request.POST.get('multicomplete_tags').split(
                        ',')
                    new_tags = []
                    for name in tag_names:
                        name = name.strip()
                        if name:
                            try:
                                tag = Tag.objects.get(name=name)
                            except Tag.DoesNotExist:
                                tag = Tag(name=name)
                                tag.save()
                            new_tags.append(tag)
                else:
                    new_tags = form.is_valid()

                object.tags.clear()
                for tag in new_tags:
                    object.tags.add(tag)
                tags = object.tags.all()
                form = None
        else:
            form = TagsForm(tags)

    context = {
        'object': object,
        'tags': tags,
        'form': form,
        'editlink': request.path + '?tags-edit'
    }

    if 'ajax' in response_format_tags:
        context = converter.preprocess_context(context)

    return Markup(
        render_to_string('core/tags/tags_box',
                         context,
                         context_instance=RequestContext(request),
                         response_format=response_format))
Beispiel #2
0
def permission_block(context, object):
    """Block with objects permissions"""
    request = context['request']
    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    if 'permission' in request.GET and request.user.profile.has_permission(
            object, mode='w'):
        if request.POST:
            if 'cancel' in request.POST:
                request.redirect = request.path
                return Markup(
                    render_to_string('core/tags/permission_block', {
                        'object': object,
                        'path': request.path
                    },
                                     context_instance=RequestContext(request),
                                     response_format=response_format))
            form = PermissionForm(request.POST, instance=object)
            if form.is_valid():
                form.save()
                request.redirect = request.path
                return Markup(
                    render_to_string('core/tags/permission_block', {
                        'object': object,
                        'path': request.path
                    },
                                     context_instance=RequestContext(request),
                                     response_format=response_format))
        else:
            form = PermissionForm(instance=object)

        context = {'object': object, 'path': request.path, 'form': form}

        if 'ajax' in response_format_tags:
            context = converter.preprocess_context(context)

        return Markup(
            render_to_string('core/tags/permission_block_edit',
                             context,
                             context_instance=RequestContext(request),
                             response_format=response_format))

    return Markup(
        render_to_string('core/tags/permission_block', {
            'object': object,
            'path': request.path
        },
                         context_instance=RequestContext(request),
                         response_format=response_format))
Beispiel #3
0
def tags_box(context, object):

    request = context['request']

    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    tags = object.tags.all()
    form = None
    if 'tags-edit' in request.GET:
        if request.POST.get('tags_object', 0) == unicode(object.id):
            form = TagsForm(tags, request.POST)
            if form.is_valid():
                if 'multicomplete_tags' in request.POST:
                    tag_names = request.POST.get(
                        'multicomplete_tags').split(',')
                    new_tags = []
                    for name in tag_names:
                        name = name.strip()
                        if name:
                            try:
                                tag = Tag.objects.get(name=name)
                            except Tag.DoesNotExist:
                                tag = Tag(name=name)
                                tag.save()
                            new_tags.append(tag)
                else:
                    new_tags = form.is_valid()

                object.tags.clear()
                for tag in new_tags:
                    object.tags.add(tag)
                tags = object.tags.all()
                form = None
        else:
            form = TagsForm(tags)

    context = {'object': object,
               'tags': tags,
               'form': form,
               'editlink': request.path + '?tags-edit'}

    if 'ajax' in response_format_tags:
        context = converter.preprocess_context(context)

    return Markup(render_to_string('core/tags/tags_box', context,
                                   context_instance=RequestContext(request),
                                   response_format=response_format))
Beispiel #4
0
def tags(request, target, object_id, edit=False, formdata=None):
    if formdata is None:
        formdata = {}
    dajax = Dajax()

    response_format = 'html'
    obj = Object.objects.get(pk=object_id)

    tags = obj.tags.all()
    form = None
    if 'tags' in formdata and not type(formdata['tags']) == list:
        formdata['tags'] = [formdata['tags']]

    if edit or formdata:
        if formdata.get('tags_object', 0) == unicode(obj.id):
            form = TagsForm(tags, formdata)
            if form.is_valid():
                if 'multicomplete_tags' in formdata:
                    tag_names = formdata.get('multicomplete_tags').split(',')
                    new_tags = []
                    for name in tag_names:
                        name = name.strip()
                        if name:
                            try:
                                tag = Tag.objects.get(name=name)
                            except Tag.DoesNotExist:
                                tag = Tag(name=name)
                                tag.save()
                            new_tags.append(tag)
                else:
                    new_tags = form.is_valid()

                obj.tags.clear()
                for tag in new_tags:
                    obj.tags.add(tag)
                tags = obj.tags.all()
                form = None
        else:
            form = TagsForm(tags)

    context = {'object': obj,
               'tags': tags,
               'form': form}

    context = converter.preprocess_context(context)

    output = render_to_string('core/ajax/tags_box', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)

    dajax.add_data({'target': target, 'content': output}, 'anaf.add_data')
    return dajax.json()
Beispiel #5
0
def tags(request, target, object_id, edit=False, formdata=None):
    if formdata is None:
        formdata = {}
    dajax = Dajax()

    response_format = 'html'
    obj = Object.objects.get(pk=object_id)

    tags = obj.tags.all()
    form = None
    if 'tags' in formdata and not type(formdata['tags']) == list:
        formdata['tags'] = [formdata['tags']]

    if edit or formdata:
        if formdata.get('tags_object', 0) == unicode(obj.id):
            form = TagsForm(tags, formdata)
            if form.is_valid():
                if 'multicomplete_tags' in formdata:
                    tag_names = formdata.get('multicomplete_tags').split(',')
                    new_tags = []
                    for name in tag_names:
                        name = name.strip()
                        if name:
                            try:
                                tag = Tag.objects.get(name=name)
                            except Tag.DoesNotExist:
                                tag = Tag(name=name)
                                tag.save()
                            new_tags.append(tag)
                else:
                    new_tags = form.is_valid()

                obj.tags.clear()
                for tag in new_tags:
                    obj.tags.add(tag)
                tags = obj.tags.all()
                form = None
        else:
            form = TagsForm(tags)

    context = {'object': obj, 'tags': tags, 'form': form}

    context = converter.preprocess_context(context)

    output = render_to_string('core/ajax/tags_box',
                              context,
                              context_instance=RequestContext(request),
                              response_format=response_format)

    dajax.add_data({'target': target, 'content': output}, 'anaf.add_data')
    return dajax.json()
Beispiel #6
0
def permission_block(context, object):
    """Block with objects permissions"""
    request = context['request']
    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    if 'permission' in request.GET and request.user.profile.has_permission(object, mode='w'):
        if request.POST:
            if 'cancel' in request.POST:
                request.redirect = request.path
                return Markup(render_to_string('core/tags/permission_block',
                                               {'object': object,
                                                   'path': request.path},
                                               context_instance=RequestContext(
                                                   request),
                                               response_format=response_format))
            form = PermissionForm(request.POST, instance=object)
            if form.is_valid():
                form.save()
                request.redirect = request.path
                return Markup(render_to_string('core/tags/permission_block',
                                               {'object': object,
                                                   'path': request.path},
                                               context_instance=RequestContext(
                                                   request),
                                               response_format=response_format))
        else:
            form = PermissionForm(instance=object)

        context = {'object': object, 'path': request.path, 'form': form}

        if 'ajax' in response_format_tags:
            context = converter.preprocess_context(context)

        return Markup(render_to_string('core/tags/permission_block_edit',
                                       context,
                                       context_instance=RequestContext(
                                           request),
                                       response_format=response_format))

    return Markup(render_to_string('core/tags/permission_block',
                                   {'object': object, 'path': request.path},
                                   context_instance=RequestContext(request),
                                   response_format=response_format))
Beispiel #7
0
def link_block(context, object):
    "Block with objects links"

    request = context['request']

    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    if request.GET and 'link_add' in request.GET:
        if request.POST:

            if 'cancel' in request.POST:
                links = Object.filter_by_request(context['request'],
                                                 object.links)
                return Markup(
                    render_to_string('core/tags/link_block', {
                        'object': object,
                        'links': links,
                        'request': request,
                        'path': request.path
                    },
                                     context_instance=RequestContext(request),
                                     response_format=response_format))

            form = ObjectLinksForm(request.user.profile, response_format_tags,
                                   object, request.POST)

            if form.is_valid() and request.user.profile.has_permission(
                    object, mode='w'):
                object.links.add(form.cleaned_data['links'])
                links = Object.filter_by_request(context['request'],
                                                 object.links)
                return Markup(
                    render_to_string('core/tags/link_block', {
                        'object': object,
                        'links': links,
                        'request': request,
                        'path': request.path
                    },
                                     context_instance=RequestContext(request),
                                     response_format=response_format))

        links = Object.filter_by_request(context['request'], object.links)
        form = ObjectLinksForm(request.user.profile,
                               response_format_tags,
                               instance=object)

        context = {
            'object': object,
            'path': request.path,
            'form': form,
            'links': links
        }

        if 'ajax' in response_format_tags:
            context = converter.preprocess_context(context)

        rendered_string = render_to_string(
            'core/tags/link_block_edit',
            context,
            context_instance=RequestContext(request),
            response_format=response_format)

        return Markup(rendered_string)

    elif request.GET and 'link_delete' in request.GET and request.user.profile.has_permission(
            object, mode='w'):
        try:
            link = Object.objects.get(pk=request.GET['link_delete'])
            object.links.remove(link)
        except Exception:
            pass

    links = Object.filter_by_request(context['request'], object.links)

    return Markup(
        render_to_string('core/tags/link_block', {
            'object': object,
            'links': links,
            'request': request,
            'path': request.path
        },
                         context_instance=RequestContext(request),
                         response_format=response_format))
Beispiel #8
0
def subscription_block(context, object):
    "Block with objects subscriptions"

    request = context['request']

    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    subscriptions = object.subscribers.all()

    subscribed = False
    if request.user.profile in subscriptions:
        subscribed = True

    if 'subscribe_add' in request.GET and request.user.profile.has_permission(
            object, mode='w'):
        if request.POST and 'subscriber' in request.POST:
            if 'cancel' in request.POST:
                request.redirect = request.path
                return Markup(
                    render_to_string('core/tags/subscription_block', {
                        'object': object,
                        'subscriptions': subscriptions,
                        'request': request,
                        'path': request.path,
                        'subscribed': subscribed
                    },
                                     context_instance=RequestContext(request),
                                     response_format=response_format))
            else:
                form = SubscribeForm(object, request.POST)
                if form.is_valid():
                    subscriptions = form.save()

                request.redirect = request.path
                return Markup(
                    render_to_string('core/tags/subscription_block', {
                        'object': object,
                        'subscriptions': subscriptions,
                        'request': request,
                        'path': request.path,
                        'subscribed': subscribed
                    },
                                     context_instance=RequestContext(request),
                                     response_format=response_format))

        else:
            form = SubscribeForm(instance=object)

            context = {
                'object': object,
                'subscriptions': subscriptions,
                'request': request,
                'path': request.path,
                'subscribed': subscribed,
                'form': form
            }

            if 'ajax' in response_format_tags:
                context = converter.preprocess_context(context)

            return Markup(
                render_to_string('core/tags/subscription_block_add',
                                 context,
                                 context_instance=RequestContext(request),
                                 response_format=response_format))

    if 'subscribe' in request.GET and not subscribed:
        object.subscribers.add(request.user.profile)
        subscriptions = object.subscribers.all()
        subscribed = True
    elif 'unsubscribe' in request.GET and request.GET['unsubscribe']:
        user_id = int(request.GET['unsubscribe'])
        try:
            if request.user.profile.id == user_id or \
                    request.user.profile.has_permission(object, mode='w'):
                object.subscribers.remove(subscriptions.get(pk=user_id))
                subscriptions = object.subscribers.all()
                if user_id == request.user.profile.id:
                    subscribed = False
        except Exception:
            pass

    return Markup(
        render_to_string('core/tags/subscription_block', {
            'object': object,
            'subscriptions': subscriptions,
            'request': request,
            'path': request.path,
            'subscribed': subscribed
        },
                         context_instance=RequestContext(request),
                         response_format=response_format))
Beispiel #9
0
def link_block(context, object):
    "Block with objects links"

    request = context['request']

    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    if request.GET and 'link_add' in request.GET:
        if request.POST:

            if 'cancel' in request.POST:
                links = Object.filter_by_request(
                    context['request'], object.links)
                return Markup(render_to_string('core/tags/link_block',
                                               {'object': object, 'links': links,
                                                   'request': request, 'path': request.path},
                                               context_instance=RequestContext(
                                                   request),
                                               response_format=response_format))

            form = ObjectLinksForm(
                request.user.profile, response_format_tags, object, request.POST)

            if form.is_valid() and request.user.profile.has_permission(object, mode='w'):
                object.links.add(form.cleaned_data['links'])
                links = Object.filter_by_request(
                    context['request'], object.links)
                return Markup(render_to_string('core/tags/link_block',
                                               {'object': object, 'links': links,
                                                   'request': request, 'path': request.path},
                                               context_instance=RequestContext(
                                                   request),
                                               response_format=response_format))

        links = Object.filter_by_request(context['request'], object.links)
        form = ObjectLinksForm(
            request.user.profile, response_format_tags, instance=object)

        context = {'object': object, 'path': request.path,
                   'form': form, 'links': links}

        if 'ajax' in response_format_tags:
            context = converter.preprocess_context(context)

        rendered_string = render_to_string('core/tags/link_block_edit', context,
                                           context_instance=RequestContext(
                                               request),
                                           response_format=response_format)

        return Markup(rendered_string)

    elif request.GET and 'link_delete' in request.GET and request.user.profile.has_permission(object, mode='w'):
        try:
            link = Object.objects.get(pk=request.GET['link_delete'])
            object.links.remove(link)
        except Exception:
            pass

    links = Object.filter_by_request(context['request'], object.links)

    return Markup(render_to_string('core/tags/link_block',
                                   {'object': object, 'links': links, 'request': request,
                                    'path': request.path},
                                   context_instance=RequestContext(request),
                                   response_format=response_format))
Beispiel #10
0
def subscription_block(context, object):
    "Block with objects subscriptions"

    request = context['request']

    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']

    response_format_tags = response_format
    if 'response_format_tags' in context:
        response_format_tags = context['response_format_tags']

    subscriptions = object.subscribers.all()

    subscribed = False
    if request.user.profile in subscriptions:
        subscribed = True

    if 'subscribe_add' in request.GET and request.user.profile.has_permission(object, mode='w'):
        if request.POST and 'subscriber' in request.POST:
            if 'cancel' in request.POST:
                request.redirect = request.path
                return Markup(render_to_string('core/tags/subscription_block',
                                               {'object': object, 'subscriptions': subscriptions,
                                                'request': request, 'path': request.path,
                                                'subscribed': subscribed},
                                               context_instance=RequestContext(
                                                   request),
                                               response_format=response_format))
            else:
                form = SubscribeForm(object, request.POST)
                if form.is_valid():
                    subscriptions = form.save()

                request.redirect = request.path
                return Markup(render_to_string('core/tags/subscription_block',
                                               {'object': object, 'subscriptions': subscriptions,
                                                'request': request, 'path': request.path,
                                                'subscribed': subscribed},
                                               context_instance=RequestContext(
                                                   request),
                                               response_format=response_format))

        else:
            form = SubscribeForm(instance=object)

            context = {'object': object, 'subscriptions': subscriptions,
                       'request': request, 'path': request.path,
                       'subscribed': subscribed, 'form': form}

            if 'ajax' in response_format_tags:
                context = converter.preprocess_context(context)

            return Markup(render_to_string('core/tags/subscription_block_add', context,
                          context_instance=RequestContext(request),
                          response_format=response_format))

    if 'subscribe' in request.GET and not subscribed:
        object.subscribers.add(request.user.profile)
        subscriptions = object.subscribers.all()
        subscribed = True
    elif 'unsubscribe' in request.GET and request.GET['unsubscribe']:
        user_id = int(request.GET['unsubscribe'])
        try:
            if request.user.profile.id == user_id or \
                    request.user.profile.has_permission(object, mode='w'):
                object.subscribers.remove(subscriptions.get(pk=user_id))
                subscriptions = object.subscribers.all()
                if user_id == request.user.profile.id:
                    subscribed = False
        except Exception:
            pass

    return Markup(render_to_string('core/tags/subscription_block',
                                   {'object': object, 'subscriptions': subscriptions,
                                    'request': request, 'path': request.path,
                                    'subscribed': subscribed},
                                   context_instance=RequestContext(request),
                                   response_format=response_format))