Network Working Group Y. Rekhter
Request for Comments: 1918 Cisco Systems
Obsoletes: 1627, 1597 B. Moskowitz
BCP: 5 Chrysler Corp.
Category: Best Current Practice D. Karrenberg
RIPE NCC
G. J. de Groot
RIPE NCC
E. Lear
Silicon Graphics, Inc.
February 1996
import hashlib
import os
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.db import models
def upload_to(instance, filename, fieldname):
ext = os.path.splitext(filename)[1].lower()
class_name = instance.__class__.__name__.lower()
h = hashlib.sha256()
field = getattr(instance, fieldname)
for chunk in field.chunks():
h.update(chunk)
name = h.hexdigest()
return os.path.join(
class_name,
name + ext,
)
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=None):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name
class Article(models.Model):
image = models.ImageField(
storage=OverwriteStorage(),
upload_to=lambda inst, fn: upload_to(inst, fn, 'image'),
)
pip install django-hashedfilenamestorage
DEFAULT_FILE_STORAGE = 'django_hashedfilenamestorage.storage.HashedFilenameFileSystemStorage'
from os.path import splitext
from uuid import uuid4
from django.core.files.storage import FileSystemStorage
class UUIDFileStorage(FileSystemStorage):
def get_available_name(self, name, max_length=None):
_, ext = path.splitext(name)
return uuid4().hex + ext
class SomeModel(models.Model):
some_filed = models.ImageField('Какое-то изображение', upload_to='someimages', storage=UUIDFileStorage())
import itertools
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
...
limit = 250
for index, items in enumerate(grouper(rows, limit)):
items = (i for i in items if i) # remove empty rows added by grouper
products = []
for item in items:
product = Product(
price=item[0],
shipping=item[1],
)
products.append(product)
Product.objects.bulk_create(products)
from django.db import transaction
...
limit = 250
for index, items in enumerate(grouper(rows, limit)):
with transaction.atomic():
for item in (i for i in items if i):
product = Product.objects.create(
price=item[0],
shipping=item[1],
)
# product can be used there to create another objects, e.g.:
for color in item[2].split(','):
ProductColor.objects.create(
product=product,
color=color,
)
Как написать твит-бота на PHP не имея опыта в программировании?
Как лучше в конце-концов начать изучать пэхэпэ с удовольствием для себя ?