Beispiel #1
0
 def save(self):
     post = super(AdminPostForm, self).save(commit=False)
     
     if post.pk is None:
         if self.cleaned_data["publish"]:
             post.published = datetime.now()
     else:
         if Post.objects.filter(pk=post.pk, published=None).count():
             if self.cleaned_data["publish"]:
                 post.published = datetime.now()
     
     render_func = curry(load_path_attr(PARSER[0]), **PARSER[1])
     
     post.teaser_html = render_func(self.cleaned_data["teaser"])
     post.content_html = render_func(self.cleaned_data["content"])
     post.updated = datetime.now()
     post.save()
     
     r = Revision()
     r.post = post
     r.title = post.title
     r.teaser = self.cleaned_data["teaser"]
     r.content = self.cleaned_data["content"]
     r.author = post.author
     r.updated = post.updated
     r.published = post.published
     r.save()
     
     if can_tweet() and self.cleaned_data["tweet"]:
         post.tweet()
     
     return post
Beispiel #2
0
 def save(self):
     post = super(AdminPostForm, self).save(commit=False)
     
     if post.pk is None:
         if self.cleaned_data["publish"]:
             post.published = datetime.now()
     else:
         if Post.objects.filter(pk=post.pk, published=None).count():
             if self.cleaned_data["publish"]:
                 post.published = datetime.now()
     
     post.teaser_html = parse(self.cleaned_data["teaser"], emitter=BiblionHtmlEmitter)
     post.content_html = parse(self.cleaned_data["content"], emitter=BiblionHtmlEmitter)
     post.updated = datetime.now()
     post.save()
     
     r = Revision()
     r.post = post
     r.title = post.title
     r.teaser = self.cleaned_data["teaser"]
     r.content = self.cleaned_data["content"]
     r.author = post.author
     r.updated = post.updated
     r.published = post.published
     r.save()
     
     if can_tweet() and self.cleaned_data["tweet"]:
         post.tweet()
     
     return post
Beispiel #3
0
 def save(self):
     post = super(AdminPostForm, self).save(commit=False)
     
     if post.pk is None:
         if self.cleaned_data["publish"]:
             post.published = datetime.now()
     else:
         if Post.objects.filter(pk=post.pk, published=None).count():
             if self.cleaned_data["publish"]:
                 post.published = datetime.now()
     
     render_func = curry(load_path_attr(PARSER[0], **PARSER[1]))
     
     post.teaser_html = render_func(self.cleaned_data["teaser"])
     post.content_html = render_func(self.cleaned_data["content"])
     post.updated = datetime.now()
     post.save()
     
     r = Revision()
     r.post = post
     r.title = post.title
     r.teaser = self.cleaned_data["teaser"]
     r.content = self.cleaned_data["content"]
     r.author = post.author
     r.updated = post.updated
     r.published = post.published
     r.save()
     
     if can_tweet() and self.cleaned_data["tweet"]:
         post.tweet()
     
     return post
Beispiel #4
0
 def save(self):
     post = super(AdminPostForm, self).save(commit=False)
     
     if post.pk is None:
         if self.cleaned_data["publish"]:
             post.published = datetime.now()
     else:
         if Post.objects.filter(pk=post.pk, published=None).count():
             if self.cleaned_data["publish"]:
                 post.published = datetime.now()
     
     post.teaser_html = parse(self.cleaned_data["teaser"], emitter=BiblionHtmlEmitter)
     post.content_html = parse(self.cleaned_data["content"], emitter=BiblionHtmlEmitter)
     post.updated = datetime.now()
     post.save()
     
     r = Revision()
     r.post = post
     r.title = post.title
     r.teaser = self.cleaned_data["teaser"]
     r.content = self.cleaned_data["content"]
     r.author = post.author
     r.updated = post.updated
     r.published = post.published
     r.save()
     
     if can_tweet() and self.cleaned_data["tweet"]:
         post.tweet()
     
     return post
Beispiel #5
0
class PostAdmin(admin.ModelAdmin):
    list_display = [
        "title", "published_flag", "section", "show_secret_share_url"
    ]
    list_filter = ["section"]
    form = AdminPostForm
    fields = [
        "section",
        "title",
        "slug",
        "author",
        "markup",
        "teaser",
        "content",
        "sharable_url",
        "publish",
    ]
    readonly_fields = ["sharable_url"]

    if can_tweet():
        fields.append("tweet")
    prepopulated_fields = {"slug": ("title", )}
    inlines = [
        ImageInline,
        ReviewInline,
    ]

    def show_secret_share_url(self, obj):
        return '<a href="%s">%s</a>' % (obj.sharable_url, obj.sharable_url)

    show_secret_share_url.short_description = "Share this url"
    show_secret_share_url.allow_tags = True

    def published_flag(self, obj):
        return bool(obj.published)

    published_flag.short_description = "Published"
    published_flag.boolean = True

    def formfield_for_dbfield(self, db_field, **kwargs):
        request = kwargs.get("request")
        if db_field.name == "author":
            ff = super(PostAdmin,
                       self).formfield_for_dbfield(db_field, **kwargs)
            ff.initial = request.user.id
            return ff
        return super(PostAdmin, self).formfield_for_dbfield(db_field, **kwargs)

    def get_form(self, request, obj=None, **kwargs):
        kwargs.update({
            "formfield_callback":
            curry(self.formfield_for_dbfield, request=request),
        })
        return super(PostAdmin, self).get_form(request, obj, **kwargs)

    def save_form(self, request, form, change):
        # this is done for explicitness that we want form.save to commit
        # form.save doesn't take a commit kwarg for this reason
        return form.save()
Beispiel #6
0
 def tweet(self):
     if can_tweet():
         account = twitter.Api(username=settings.TWITTER_USERNAME, password=settings.TWITTER_PASSWORD)
         account.PostUpdate(self.as_tweet())
     else:
         raise ImproperlyConfigured(
             "Unable to send tweet due to either " "missing python-twitter or required settings."
         )
Beispiel #7
0
 def tweet(self):
     if can_tweet():
         account = twitter.Api(
             username = settings.TWITTER_USERNAME,
             password = settings.TWITTER_PASSWORD,
         )
         account.PostUpdate(self.as_tweet())
     else:
         raise ImproperlyConfigured("Unable to send tweet due to either "
             "missing python-twitter or required settings.")
Beispiel #8
0
    def save(self):
        published = False
        post = super(AdminPostForm, self).save(commit=False)

        if post.pk is None:
            if self.cleaned_data["publish"]:
                post.published = datetime.now()
                published = True
        else:
            if Post.objects.filter(pk=post.pk, published=None).count():
                if self.cleaned_data["publish"]:
                    post.published = datetime.now()
                    published = True

        render_func = curry(
            load_path_attr(
                settings.BIBLION_MARKUP_CHOICE_MAP[self.cleaned_data["markup"]]["parser"]
            )
        )

        post.teaser_html = render_func(self.cleaned_data["teaser"])
        post.content_html = render_func(self.cleaned_data["content"])
        post.updated = datetime.now()
        post.save()

        r = Revision()
        r.post = post
        r.title = post.title
        r.teaser = self.cleaned_data["teaser"]
        r.content = self.cleaned_data["content"]
        r.author = post.author
        r.updated = post.updated
        r.published = post.published
        r.save()

        if can_tweet() and self.cleaned_data["tweet"]:
            post.tweet()

        if published:
            post_published.send(sender=Post, post=post)

        return post
Beispiel #9
0
    def save(self):
        published = False
        post = super(AdminPostForm, self).save(commit=False)

        if post.pk is None:
            if self.cleaned_data["publish"]:
                post.published = datetime.now()
                published = True
        else:
            if Post.objects.filter(pk=post.pk, published=None).count():
                if self.cleaned_data["publish"]:
                    post.published = datetime.now()
                    published = True

        render_func = curry(
            load_path_attr(
                MARKUP_CHOICE_MAP[self.cleaned_data["markup"]]["parser"]))

        post.teaser_html = render_func(self.cleaned_data["teaser"])
        post.content_html = render_func(self.cleaned_data["content"])
        post.updated = datetime.now()
        post.save()

        r = Revision()
        r.post = post
        r.title = post.title
        r.teaser = self.cleaned_data["teaser"]
        r.content = self.cleaned_data["content"]
        r.author = post.author
        r.updated = post.updated
        r.published = post.published
        r.save()

        if can_tweet() and self.cleaned_data["tweet"]:
            post.tweet()

        if published:
            post_published.send(sender=Post, post=post)

        return post
Beispiel #10
0
class AdminPostForm(forms.ModelForm):

    title = forms.CharField(
        max_length=90,
        widget=forms.TextInput(attrs={"style": "width: 50%;"}),
    )
    slug = forms.CharField(widget=forms.TextInput(
        attrs={"style": "width: 50%;"}))
    teaser = forms.CharField(
        widget=forms.Textarea(attrs={"style": "width: 80%;"}), )
    content = forms.CharField(widget=forms.Textarea(
        attrs={"style": "width: 80%; height: 300px;"}))
    publish = forms.BooleanField(
        required=False,
        help_text="Checking this will publish this articles on the site",
    )

    if can_tweet():
        tweet = forms.BooleanField(
            required=False,
            help_text="Checking this will send out a tweet for this post",
        )

    class Meta:
        model = Post

    def __init__(self, *args, **kwargs):
        super(AdminPostForm, self).__init__(*args, **kwargs)

        post = self.instance

        # grab the latest revision of the Post instance
        latest_revision = post.latest()

        if latest_revision:
            # set initial data from the latest revision
            self.fields["teaser"].initial = latest_revision.teaser
            self.fields["content"].initial = latest_revision.content

            # @@@ can a post be unpublished then re-published? should be pulled
            # from latest revision maybe?
            self.fields["publish"].initial = bool(post.published)

    def save(self):
        published = False
        post = super(AdminPostForm, self).save(commit=False)

        if post.pk is None:
            if self.cleaned_data["publish"]:
                post.published = datetime.now()
                published = True
        else:
            if Post.objects.filter(pk=post.pk, published=None).count():
                if self.cleaned_data["publish"]:
                    post.published = datetime.now()
                    published = True

        render_func = curry(
            load_path_attr(
                MARKUP_CHOICE_MAP[self.cleaned_data["markup"]]["parser"]))

        post.teaser_html = render_func(self.cleaned_data["teaser"])
        post.content_html = render_func(self.cleaned_data["content"])
        post.updated = datetime.now()
        post.save()

        r = Revision()
        r.post = post
        r.title = post.title
        r.teaser = self.cleaned_data["teaser"]
        r.content = self.cleaned_data["content"]
        r.author = post.author
        r.updated = post.updated
        r.published = post.published
        r.save()

        if can_tweet() and self.cleaned_data["tweet"]:
            post.tweet()

        if published:
            post_published.send(sender=Post, post=post)

        return post