Exemplo n.º 1
0
def get_story_image(request, story, size=None):
    try:
        story = ArticleStory.objects.select_related().get(pk=story)
        allowed_objects = get_allowed_objects(
            request.user, ArticleCategory, 'reader')
        al = story.categories.filter(pk__in=allowed_objects)
        if len(al) == 0:
            return HttpResponseRedirect('/')
    except:
        return HttpResponseRedirect('/')

    path = u"%s/%s" % (get_upload_articles_images(), story.image)
    if size:
        miniature = resize_image(story.image.path, size)
        split = path.rsplit('/', 1)
        path = '%s/%s' % (split[0], miniature)

    try:
        image_data = open(path, "rb").read()
        return HttpResponse(image_data, mimetype=story.mimetype)
    except:
        return HttpResponse('')
Exemplo n.º 2
0
def add_new_story(request):
    allowed_objects = get_allowed_objects(
        request.user, ArticleCategory, 'writer')
    if len(allowed_objects) == 0:
        return HttpResponseRedirect('/')

    if request.method == 'POST':
        form_story = ArticleStoryForm(request.POST, request.FILES, request=request)
        fa_set = formset_factory(ArticleAttachmentsForm, extra=2)
        form_attachments_set = fa_set(request.POST, request.FILES)
        if form_story.is_valid():
            if 'image' in request.FILES:
                image_file = request.FILES['image']
                mimetype = image_file.content_type
            else:
                image_file = None
                mimetype = ''

            if image_file:
                story = form_story.save()
                upload_path = get_upload_articles_images()
                path_src = u"%s/%s" % (upload_path, story.image)
                path_dest = u"%s/%s" % (upload_path, story.pk)
                os.makedirs(path_dest)
                shutil.move(path_src, path_dest)
                s = os.path.splitext(image_file.name)
                filename = u"%s%s" % (slugify(s[0]), s[1])
                story.image = "%s/%s" % (story.pk, filename)
            else:
                story = form_story.save(commit=False)

            story.mimetype = mimetype
            story.owner = request.user
            story.save()

            story.categories.clear()
            selected_categories = request.POST.getlist('_selected_action')
            story.categories = selected_categories

            if form_attachments_set.is_valid():
                for i in range(0, form_attachments_set.total_form_count()):
                    form = form_attachments_set.forms[i]
                    try:
                        file = request.FILES['form-' + str(
                            i) + '-attached_file']
                        attachment = ArticleAttachments(description=form.cleaned_data[
                                                        'description'], mimetype=file.content_type)
                        attachment.article = story
                        attachment.save()
                        attachment.attached_file.save(file.name, file)
                    except:
                        pass

            tags = map(int, request.POST.getlist('article_tags[]'))
            tags = ArticleTags.objects.filter(pk__in=tags)
            story.tags.add(*tags)

            if story.categories:
                notify_users(request, story, story.categories.all(), True)

            return HttpResponseRedirect(reverse('writer_stories_list', args=[]))
    else:
        form_story = ArticleStoryForm(request=request)
        fa_set = formset_factory(ArticleAttachmentsForm, extra=2)
        form_attachments_set = fa_set()

    tags = ArticleTags.objects.all()
    context = RequestContext(request)
    extra_context = {'form_story': form_story,
                    'form_attachments': form_attachments_set,
                    'tags': tags,
                    }
    return render_to_response('articles/new_article.html', extra_context, context_instance=context)
Exemplo n.º 3
0
def edit_story(request, story):
    allowed_objects = get_allowed_objects(
            request.user, ArticleCategory, 'manager')
    try:
        story = ArticleStory.objects.select_related().get(pk=story)
        categories = story.categories.all()
        intersect = set(allowed_objects).intersection( set([category.pk for category in categories]))
        if story.owner != request.user and not intersect:
            return HttpResponseRedirect('/')
    except Exception as e:
        print e
        return HttpResponseRedirect('/')

    attachments = story.articleattachments_set.all()
    if request.method == 'POST':
        if 'bdelstory' in request.POST:
            story.articleattachments_set.all().delete()
            pk = story.pk
            story.delete()
            try:
                shutil.rmtree(u"%s/%s" % (get_upload_articles_images(), pk))
            except:
                pass
            try:
                shutil.rmtree(u"%s/%s" % (get_upload_articles_files(), pk))
            except:
                pass
            return HttpResponseRedirect(reverse('writer_stories_list', args=[]))

        form_story = ArticleStoryForm(
            request.POST, request.FILES, instance=story, request=request)
        if form_story.is_valid():
            if 'selected_image' in request.POST:
                story.image.delete()
                try:
                    shutil.rmtree(u"%s/%s" % (get_upload_articles_images(), story.pk))
                except:
                    pass
            if 'image' in request.FILES:
                story.mimetype = request.FILES['image'].content_type
            else:
                image_file = None

            story.modifier = request.user
            story.save()

            story.categories.clear()
            selected_categories = request.POST.getlist('_selected_action')
            story.categories = selected_categories

        form_story = ArticleStoryForm(instance=story, request=request)

        if len(attachments) < 2:
            fa_set = formset_factory(
                ArticleAttachmentsForm, extra=2 - len(attachments))
            form_attachments_set = fa_set(request.POST, request.FILES)
            if form_attachments_set.is_valid():
                for i in range(0, form_attachments_set.total_form_count()):
                    form = form_attachments_set.forms[i]
                    try:
                        file = request.FILES['form-' + str(
                            i) + '-attached_file']
                        attachment = ArticleAttachments(description=form.cleaned_data[
                                                        'description'], mimetype=file.content_type)
                        attachment.article = story
                        attachment.save()
                        attachment.attached_file.save(file.name, file)
                    except:
                        pass

        if 'selected_attachments' in request.POST:
            att = request.POST.getlist('selected_attachments')
            ArticleAttachments.objects.filter(pk__in=att).delete()
        attachments = story.articleattachments_set.all()
        extra = len(attachments)
        if extra < 2:
            fa_set = formset_factory(ArticleAttachmentsForm, extra=2 - extra)
            form_attachments_set = fa_set()
        else:
            form_attachments_set = None

        tags = map(int, request.POST.getlist('article_tags[]'))
        story.tags.clear()
        tags = ArticleTags.objects.filter(pk__in=tags)
        story.tags.add(*tags)
        if story.categories:
            notify_users(request, story, story.categories.all(), False)

    else:
        form_story = ArticleStoryForm(instance=story, request=request)
        extra = len(attachments)
        if extra < 2:
            fa_set = formset_factory(ArticleAttachmentsForm, extra=2 - extra)
            form_attachments_set = fa_set()
        else:
            form_attachments_set = None

    tags = ArticleTags.objects.all()
    story_tags = story.tags.all()
    for tag in tags:
        if tag in story_tags:
            tag.checked = True
    context = RequestContext(request)
    extra_context = {'form_story': form_story,
                     'story': story,
                     'tags': tags,
                     'form_attachments': form_attachments_set,
                     'attachments': attachments,
                     'categories': [c.pk for c in categories],
                     }
    return render_to_response('articles/edit_article.html', extra_context, context_instance=context)
Exemplo n.º 4
0
    def get_acl_permissions():
        return {
            'reader': ugettext_noop(u'Who can browse this category'),
            'writer': ugettext_noop(u'Who can submit articles to this category'),
            'manager': ugettext_noop(u'Who can manage this category'),
        }
    get_acl_permissions = staticmethod(get_acl_permissions)

ARTICLE_STATES = (
    (0, 'Draft'),
    (1, 'Published'),
    (2, 'Archived'),
)

article_image_storage = FileSystemStorage(
    location=get_upload_articles_images(), base_url='/images/articles/')


def get_image_filepath_storage(instance, filename):
    s = os.path.splitext(filename)
    filename = u"%s%s" % (slugify(s[0]), s[1])
    return os.path.join(u"%s/%s" % (str(instance.pk), filename))


class ArticleStory(ZornaEntity):
    categories = models.ManyToManyField(ArticleCategory, verbose_name=_(
        'categories'), blank=True, editable=False)
    title = models.CharField(_('Title'), max_length=255)
    slug = models.SlugField(_('slug'), max_length=255, null=True, blank=True)
    head = models.TextField(_('Head'), blank=True)
    body = models.TextField(_('Body'))