示例#1
0
文件: object.py 项目: alejo8591/maker
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))
示例#2
0
文件: ajax.py 项目: alejo8591/maker
def tags(request, target, object_id, edit=False, formdata={}):
    dajax = Dajax()
    
    response_format = 'html'
    object = Object.objects.get(pk=object_id)
    
    tags = object.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(object.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()
                    
                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 }    
    
    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}, 'maker.add_data')
    return dajax.json()
示例#3
0
文件: object.py 项目: alejo8591/maker
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:
        if request.user.get_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))
示例#4
0
文件: object.py 项目: alejo8591/maker
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.get_profile(), response_format_tags, object, request.POST)
            
            if form.is_valid() and request.user.get_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.get_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:
        
        if request.user.get_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))
示例#5
0
文件: object.py 项目: alejo8591/maker
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.get_profile() in subscriptions:
        subscribed = True

    if 'subscribe_add' in request.GET and request.user.get_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:
        if not subscribed:
            object.subscribers.add(request.user.get_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.get_profile().id == user_id or \
                request.user.get_profile().has_permission(object, mode='w'):
                object.subscribers.remove(subscriptions.get(pk=user_id))
                subscriptions = object.subscribers.all()
                if user_id == request.user.get_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))