In [1]:
%config InlineBackend.figure_format = 'retina'
%load_ext autoreload
%autoreload 1
In [2]:
from cast.models import (
Blog as CastBlog,
Image,
Video,
File,
Post,
Gallery,
)
In [3]:
from homepage.blogs.models import (
Blog,
BlogImage,
BlogFile,
BlogGallery,
BlogPost,
BlogVideo,
)
In [4]:
CastBlog.objects.all().delete()
Out[4]:
In [96]:
%%time
blog_attrs = ("pk", "user", "title", "description", "slug", "created", "modified")
cast_blogs = []
for bl in Blog.objects.all():
params = {attr: getattr(bl, attr) for attr in blog_attrs}
cast_blogs.append(CastBlog(**params))
In [97]:
%%time
created = CastBlog.objects.bulk_create(cast_blogs)
In [98]:
CastBlog.objects.count()
Out[98]:
In [99]:
Image.objects.all().delete()
Out[99]:
In [100]:
%%time
image_attrs = ("pk", "original", "original_height", "original_width", "user", "created", "modified")
cast_images = []
for bi in BlogImage.objects.all():
params = {attr: getattr(bi, attr) for attr in image_attrs}
cast_images.append(Image(**params))
In [101]:
%%time
created = Image.objects.bulk_create(cast_images)
In [102]:
Image.objects.count()
Out[102]:
In [103]:
Video.objects.all().delete()
Out[103]:
In [104]:
%%time
video_attrs = ("pk", "original", "poster", "poster_seconds", "user", "created", "modified")
cast_videos = []
for bv in BlogVideo.objects.all():
params = {attr: getattr(bv, attr) for attr in video_attrs}
cast_videos.append(Video(**params))
In [105]:
%%time
created = Video.objects.bulk_create(cast_videos)
In [106]:
Video.objects.count()
Out[106]:
In [107]:
File.objects.all().delete()
Out[107]:
In [108]:
%%time
file_attrs = ("pk", "original", "user", "created", "modified")
cast_files = []
for bf in BlogFile.objects.all():
params = {attr: getattr(bf, attr) for attr in file_attrs}
cast_files.append(File(**params))
In [109]:
%%time
created = File.objects.bulk_create(cast_files)
In [110]:
File.objects.count()
Out[110]:
In [111]:
Gallery.objects.all().delete()
Out[111]:
In [112]:
%%time
gallery_attrs = ("pk", "user", "created", "modified")
for bg in BlogGallery.objects.all():
params = {attr: getattr(bg, attr) for attr in gallery_attrs}
gallery = Gallery.objects.create(**params)
img_pks = [pk for pk, in bg.images.values_list("pk")]
images = Image.objects.filter(pk__in=img_pks)
for image in images:
gallery.images.add(image)
gallery.save()
In [113]:
Gallery.objects.count()
Out[113]:
In [203]:
Post.objects.all().delete()
Out[203]:
In [204]:
%%time
post_attrs = ("pk", "author", "title", "pub_date", "visible_date",
"content", "slug", "created", "modified")
for bp in BlogPost.objects.all():
params = {attr: getattr(bp, attr) for attr in post_attrs}
params["content"] = (params["content"].replace("blog_image", "image")
.replace("blog_gallery", "gallery")
.replace("blog_video", "video"))
params["blog"] = CastBlog.objects.get(pk=bp.blog.pk)
post = Post.objects.create(**params)
img_pks = [pk for pk, in bp.images.values_list("pk")]
images = Image.objects.filter(pk__in=img_pks)
print(images)
for image in images:
post.images.add(image)
print(post.images.all())
print(post)
#post.save()
#print(post.images.all())
video_pks = [pk for pk, in bp.videos.values_list("pk")]
videos = Video.objects.filter(pk__in=video_pks)
for video in videos:
post.videos.add(video)
gallery_pks = [pk for pk, in bp.galleries.values_list("pk")]
galleries = Gallery.objects.filter(pk__in=gallery_pks)
for gallery in galleries:
post.galleries.add(gallery)
#post.save()
In [196]:
post.images.all()
Out[196]:
In [146]:
Post.objects.count()
Out[146]:
In [121]:
post = Post.objects.get(slug="pycon_2017")
In [140]:
foo = post.content
In [132]:
post.pk
Out[132]:
In [131]:
BlogPost.objects.all()[1].description
In [133]:
bp = BlogPost.objects.get(pk=27)
In [135]:
bp.content
Out[135]:
In [136]:
bp.description
In [137]:
Post.objects.count()
Out[137]:
In [143]:
foo.replace("blog_image", "image").replace("blog_gallery", "gallery").replace()
Out[143]:
In [147]:
Image.objects.get(pk=527)
Out[147]:
In [202]:
Post.objects.get(pk=124).images.all()
Out[202]:
In [197]:
bp = BlogPost.objects.get(pk=124)
In [153]:
img_pks = [pk for pk, in bp.images.values_list("pk")]
In [154]:
img_pks
Out[154]:
In [155]:
images = Image.objects.filter(pk__in=img_pks)
In [156]:
images
Out[156]:
In [205]:
post = Post.objects.get(pk=124)
In [159]:
for image in images:
post.images.add(image)
In [160]:
post.images.all()
Out[160]:
In [206]:
post.images.all()
Out[206]:
In [207]:
post.created, post.modified
Out[207]:
In [213]:
video = Video.objects.all().order_by("-created")[0]
In [214]:
video.created
Out[214]:
In [219]:
sorted([pk for pk, in Post.objects.values_list("pk")])
Out[219]:
In [224]:
Video.objects.get(pk=59)
In [232]:
max([pk for pk, in Video.objects.values_list("pk")])
Out[232]:
In [226]:
max([pk for pk, in BlogVideo.objects.values_list("pk")])
Out[226]:
In [4]:
video = BlogVideo.objects.all().order_by("-created")[0]
In [5]:
video.poster
Out[5]:
Out[5]:
In [6]:
video.pk
Out[6]:
Out[6]:
In [7]:
video = Video.objects.all().order_by("-created")[0]
In [8]:
video.pk
Out[8]:
Out[8]:
In [9]:
video.poster
Out[9]:
Out[9]:
In [ ]:
video.create_poster()
In [13]:
video.calc_poster
Out[13]:
Out[13]:
In [14]:
video._create_poster()
In [7]:
image = BlogImage.objects.all()[0]
In [8]:
image.original
Out[8]:
In [10]:
image = Image.objects.all()[0]
In [11]:
image.original
Out[11]:
In [13]:
image = Image.objects.get(pk=577)
In [14]:
image.original
Out[14]:
In [4]:
import io
import os
import tempfile
from django.core.files import File
from subprocess import check_output
In [5]:
video = Video.objects.all().order_by("-created")[0]
print(video.pk)
In [6]:
video._create_poster()
In [24]:
content = video._get_content()
In [26]:
content.file
In [28]:
content.size()
In [9]:
content.file = open('/var/folders/2l/7zccp5bs1234yz4ns62s796m0000gn/T/poster_lj52okma.jpg', "rb")
In [11]:
name = "poster_lj52okma.jpg"
In [10]:
print(content)
In [9]:
fp, tmp_path = tempfile.mkstemp(prefix='poster_', suffix='.jpg')
In [16]:
print(fp)
In [10]:
print(video.original.url)
print(video.original.path)
video_url = video.original.path
In [11]:
width, height = video._get_video_dimensions(video_url)
In [12]:
poster_cmd = (
'ffmpeg -ss {seconds} -i "{video_path}" -vframes 1'
' -y -f image2 -s {width}x{height} {poster_path}'
).format(video_path=video_url, seconds=video.poster_seconds,
poster_path=tmp_path, width=width, height=height)
In [13]:
poster_cmd
Out[13]:
In [14]:
check_output(poster_cmd, shell=True)
Out[14]:
In [17]:
tmp_path
Out[17]:
In [14]:
help(video.poster.save)
In [18]:
name = os.path.basename(tmp_path)
print(name)
In [19]:
content = File(open(tmp_path, 'rb'))
print("content: ", content)
In [20]:
fh = open(tmp_path, 'rb')
print(fh)
In [22]:
print(content)
In [23]:
content = File(fh)
In [25]:
print(repr(content))
In [26]:
print(content.file)
In [19]:
help(File)
In [7]:
help(open)
In [12]:
video.poster.save(name, content, save=False)
In [19]:
video.poster.field.max_length
Out[19]:
In [20]:
video.poster.storage.save(name, content, max_length=video.poster.field.max_length)
In [21]:
video.poster.storage._save(name, content)
In [23]:
content.chunks
In [15]:
content.file.read
Out[15]:
In [22]:
video.poster
Out[22]:
In [21]:
video.save()
In [6]:
video._create_poster()
In [11]:
video.poster
Out[11]:
In [215]:
from django.db import connection
In [ ]:
with connection.cursor() as cursor:
cursor.execute("select max(id) from
In [ ]: