Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap out Images for pinax-images #94

Merged
merged 4 commits into from Dec 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/usage.md
Expand Up @@ -9,8 +9,6 @@ The `description` field in the admin represents the text that will be used in
different HTML META header tags that are useful for controlling the display
on social networks like Twitter and Facebook.

This is the same idea behind the `primary_image` field in the admin.


## Images

Expand Down
1 change: 1 addition & 0 deletions makemigrations.py
Expand Up @@ -12,6 +12,7 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sites",
"pinax.images",
"pinax.blog",
"pinax.blog.tests"
],
Expand Down
10 changes: 1 addition & 9 deletions pinax/blog/admin.py
Expand Up @@ -5,15 +5,10 @@

from .conf import settings
from .forms import AdminPostForm
from .models import Blog, Post, Image, ReviewComment, Section
from .models import Blog, Post, ReviewComment, Section
from .utils import can_tweet


class ImageInline(admin.TabularInline):
model = Image
fields = ["image_path"]


class ReviewInline(admin.TabularInline):
model = ReviewComment

Expand Down Expand Up @@ -41,7 +36,6 @@ class PostAdmin(admin.ModelAdmin):
"teaser",
"content",
"description",
"primary_image",
"sharable_url",
"state",
"published"
Expand All @@ -52,7 +46,6 @@ class PostAdmin(admin.ModelAdmin):
fields.append("tweet")
prepopulated_fields = {"slug": ("title",)}
inlines = [
ImageInline,
ReviewInline,
]

Expand Down Expand Up @@ -91,5 +84,4 @@ class SectionAdmin(admin.ModelAdmin):


admin.site.register(Post, PostAdmin)
admin.site.register(Image)
admin.site.register(Section, SectionAdmin)
1 change: 0 additions & 1 deletion pinax/blog/forms.py
Expand Up @@ -18,7 +18,6 @@
"teaser",
"content",
"description",
"primary_image",
"state",
]

Expand Down
22 changes: 22 additions & 0 deletions pinax/blog/migrations/0011_post_image_set.py
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-22 22:58
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('pinax_images', '0001_initial'),
('blog', '0010_auto_20161223_1014'),
]

operations = [
migrations.AddField(
model_name='post',
name='image_set',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to='pinax_images.ImageSet'),
),
]
26 changes: 26 additions & 0 deletions pinax/blog/migrations/0012_set_default_imagesets.py
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-22 22:58
from __future__ import unicode_literals

from django.db import migrations


def image_sets(apps, schema_editor):
Post = apps.get_model("blog", "Post")
ImageSet = apps.get_model("pinax_images", "ImageSet")
db_alias = schema_editor.connection.alias
for post in Post.objects.using(db_alias).all():
if post.image_set is None:
post.image_set = ImageSet.objects.using(db_alias).create(created_by=post.author)
post.save()


class Migration(migrations.Migration):

dependencies = [
('blog', '0011_post_image_set'),
]

operations = [
migrations.RunPython(image_sets)
]
21 changes: 21 additions & 0 deletions pinax/blog/migrations/0013_imageset_not_null.py
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-22 23:01
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('blog', '0012_set_default_imagesets'),
]

operations = [
migrations.AlterField(
model_name='post',
name='image_set',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to='pinax_images.ImageSet'),
),
]
34 changes: 34 additions & 0 deletions pinax/blog/migrations/0014_migrate_to_pinax_images.py
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-22 23:01
from __future__ import unicode_literals

from django.db import migrations


def migrate_images(apps, schema_editor):
Post = apps.get_model("blog", "Post")
PinaxImage = apps.get_model("pinax_images", "Image")
db_alias = schema_editor.connection.alias
for post in Post.objects.using(db_alias).all():
for image in post.images.all():
pi = PinaxImage.objects.using(db_alias).create(
created_by=post.author,
created_at=image.timestamp,
image=image.image_path,
original_filename=image.image_path.name.split("/")[-1],
image_set=post.image_set
)
if post.primary_image == image:
post.image_set.primary_image = pi
post.image_set.save()


class Migration(migrations.Migration):

dependencies = [
('blog', '0013_imageset_not_null'),
]

operations = [
migrations.RunPython(migrate_images)
]
19 changes: 19 additions & 0 deletions pinax/blog/migrations/0015_drop_primary_image.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-22 23:18
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('blog', '0014_migrate_to_pinax_images'),
]

operations = [
migrations.RemoveField(
model_name='post',
name='primary_image',
),
]
22 changes: 22 additions & 0 deletions pinax/blog/migrations/0016_drop_image.py
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-12-22 23:21
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('blog', '0015_drop_primary_image'),
]

operations = [
migrations.RemoveField(
model_name='image',
name='post',
),
migrations.DeleteModel(
name='Image',
),
]
32 changes: 7 additions & 25 deletions pinax/blog/models.py
Expand Up @@ -22,6 +22,8 @@

import pytz

from pinax.images.models import ImageSet

from .conf import settings
from .hooks import hookset
from .managers import PostManager
Expand Down Expand Up @@ -92,8 +94,7 @@ class Post(models.Model):
content_html = models.TextField(editable=False)

description = models.TextField(_("Description"), blank=True)
primary_image = models.ForeignKey("Image", null=True, blank=True,
related_name="+", verbose_name=_("Primary Image"))
image_set = models.ForeignKey(ImageSet, related_name="blog_posts")
tweet_text = models.CharField(_("Tweet text"), max_length=140, editable=False)

created = models.DateTimeField(_("Created"), default=timezone.now, editable=False) # when first revision was created
Expand Down Expand Up @@ -140,8 +141,8 @@ def meta_description(self):

@property
def meta_image(self):
if self.primary_image:
return self.primary_image.image_path.url
if self.image_set.primary_image:
return self.image_set.primary_image.image_path.url

def rev(self, rev_id):
return self.revisions.get(pk=rev_id)
Expand Down Expand Up @@ -205,6 +206,8 @@ def save(self, **kwargs):
self.secret_key = "".join(choice(letters) for _ in range(8))
if self.is_published and self.published is None:
self.published = timezone.now()
if not ImageSet.objects.filter(blog_posts=self).exists():
self.image_set = ImageSet.objects.create(created_by=self.author)
super(Post, self).save(**kwargs)

@property
Expand Down Expand Up @@ -285,27 +288,6 @@ class Meta:
verbose_name_plural = _("Revisions")


@python_2_unicode_compatible
class Image(models.Model):

post = models.ForeignKey(Post, related_name="images", verbose_name=_("Post"))

image_path = models.ImageField(upload_to="images/%Y/%m/%d")
url = models.CharField(_("Url"), max_length=150, blank=True)

timestamp = models.DateTimeField(_("Timestamp"), default=timezone.now, editable=False)

def __str__(self):
if self.pk is not None:
return "{{ %d }}" % self.pk
else:
return _("deleted image")

class Meta:
verbose_name = _("Image")
verbose_name_plural = _("Images")


class FeedHit(models.Model):

request_data = models.TextField(_("Request data"))
Expand Down
4 changes: 2 additions & 2 deletions pinax/blog/parsers/creole_parser.py
Expand Up @@ -6,7 +6,7 @@
from pygments.lexers import get_lexer_by_name, TextLexer
from pygments.util import ClassNotFound

from ..models import Image
from pinax.images.models import Image


class Rules:
Expand Down Expand Up @@ -180,7 +180,7 @@ def image_emit(self, node):
except Image.DoesNotExist:
# @@@ do something better here
return ""
return "<img src=\"%s\" />" % (image.image_path.url,)
return "<img src=\"%s\" />" % (image.image.url,)


class PinaxBlogHtmlEmitter(PygmentsHtmlEmitter, ImageLookupHtmlEmitter):
Expand Down
4 changes: 2 additions & 2 deletions pinax/blog/parsers/markdown_parser.py
@@ -1,7 +1,7 @@
from markdown import Markdown
from markdown.inlinepatterns import ImagePattern, IMAGE_LINK_RE

from ..models import Image
from pinax.images.models import Image


class ImageLookupImagePattern(ImagePattern):
Expand All @@ -12,7 +12,7 @@ def sanitize_url(self, url):
else:
try:
image = Image.objects.get(pk=int(url))
return image.image_path.url
return image.image.url
except Image.DoesNotExist:
pass
except ValueError:
Expand Down
1 change: 1 addition & 0 deletions runtests.py
Expand Up @@ -13,6 +13,7 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sites",
"pinax.images",
"pinax.blog",
"pinax.blog.tests",
"pinax_theme_bootstrap",
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -38,7 +38,8 @@ def read(*parts):
"pytz>=2016.6.1",
"Pillow>=3.0.0",
"Markdown>=2.6.5",
"Pygments>=2.0.2"
"Pygments>=2.0.2",
"pinax-images>=2.0.0"
],
test_suite="runtests.runtests",
classifiers=[
Expand Down