| 1 |
import django.db.models.manipulators |
|---|
| 2 |
import django.db.models.manager |
|---|
| 3 |
from django.core import validators |
|---|
| 4 |
from django.core.exceptions import ObjectDoesNotExist |
|---|
| 5 |
from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist |
|---|
| 6 |
from django.db.models.fields.related import OneToOneRel, ManyToOneRel |
|---|
| 7 |
from django.db.models.query import delete_objects |
|---|
| 8 |
from django.db.models.options import Options, AdminOptions |
|---|
| 9 |
from django.db import connection, backend, transaction |
|---|
| 10 |
from django.db.models import signals |
|---|
| 11 |
from django.db.models.loading import register_models, get_model |
|---|
| 12 |
from django.dispatch import dispatcher |
|---|
| 13 |
from django.utils.datastructures import SortedDict |
|---|
| 14 |
from django.utils.functional import curry |
|---|
| 15 |
from django.utils.encoding import smart_str, force_unicode |
|---|
| 16 |
from django.conf import settings |
|---|
| 17 |
from itertools import izip |
|---|
| 18 |
import types |
|---|
| 19 |
import sys |
|---|
| 20 |
import os |
|---|
| 21 |
|
|---|
| 22 |
class ModelBase(type): |
|---|
| 23 |
"Metaclass for all models" |
|---|
| 24 |
def __new__(cls, name, bases, attrs): |
|---|
| 25 |
# If this isn't a subclass of Model, don't do anything special. |
|---|
| 26 |
try: |
|---|
| 27 |
if not filter(lambda b: issubclass(b, Model), bases): |
|---|
| 28 |
return super(ModelBase, cls).__new__(cls, name, bases, attrs) |
|---|
| 29 |
except NameError: |
|---|
| 30 |
# 'Model' isn't defined yet, meaning we're looking at Django's own |
|---|
| 31 |
# Model class, defined below. |
|---|
| 32 |
return super(ModelBase, cls).__new__(cls, name, bases, attrs) |
|---|
| 33 |
|
|---|
| 34 |
# Create the class. |
|---|
| 35 |
new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')}) |
|---|
| 36 |
new_class.add_to_class('_meta', Options(attrs.pop('Meta', None))) |
|---|
| 37 |
new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {})) |
|---|
| 38 |
|
|---|
| 39 |
# Build complete list of parents |
|---|
| 40 |
for base in bases: |
|---|
| 41 |
# TODO: Checking for the presence of '_meta' is hackish. |
|---|
| 42 |
if '_meta' in dir(base): |
|---|
| 43 |
new_class._meta.parents.append(base) |
|---|
| 44 |
new_class._meta.parents.extend(base._meta.parents) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
if getattr(new_class._meta, 'app_label', None) is None: |
|---|
| 48 |
# Figure out the app_label by looking one level up. |
|---|
| 49 |
# For 'django.contrib.sites.models', this would be 'sites'. |
|---|
| 50 |
model_module = sys.modules[new_class.__module__] |
|---|
| 51 |
new_class._meta.app_label = model_module.__name__.split('.')[-2] |
|---|
| 52 |
|
|---|
| 53 |
# Bail out early if we have already created this class. |
|---|
| 54 |
m = get_model(new_class._meta.app_label, name, False) |
|---|
| 55 |
if m is not None: |
|---|
| 56 |
return m |
|---|
| 57 |
|
|---|
| 58 |
# Add all attributes to the class. |
|---|
| 59 |
for obj_name, obj in attrs.items(): |
|---|
| 60 |
new_class.add_to_class(obj_name, obj) |
|---|
| 61 |
|
|---|
| 62 |
# Add Fields inherited from parents |
|---|
| 63 |
for parent in new_class._meta.parents: |
|---|
| 64 |
for field in parent._meta.fields: |
|---|
| 65 |
# Only add parent fields if they aren't defined for this class. |
|---|
| 66 |
try: |
|---|
| 67 |
new_class._meta.get_field(field.name) |
|---|
| 68 |
except FieldDoesNotExist: |
|---|
| 69 |
field.contribute_to_class(new_class, field.name) |
|---|
| 70 |
|
|---|
| 71 |
new_class._prepare() |
|---|
| 72 |
|
|---|
| 73 |
register_models(new_class._meta.app_label, new_class) |
|---|
| 74 |
# Because of the way imports happen (recursively), we may or may not be |
|---|
| 75 |
# the first class for this model to register with the framework. There |
|---|
| 76 |
# should only be one class for each model, so we must always return the |
|---|
| 77 |
# registered version. |
|---|
| 78 |
return get_model(new_class._meta.app_label, name, False) |
|---|
| 79 |
|
|---|
| 80 |
class Model(object): |
|---|
| 81 |
__metaclass__ = ModelBase |
|---|
| 82 |
|
|---|
| 83 |
def _get_pk_val(self): |
|---|
| 84 |
return getattr(self, self._meta.pk.attname) |
|---|
| 85 |
|
|---|
| 86 |
def __repr__(self): |
|---|
| 87 |
return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self))) |
|---|
| 88 |
|
|---|
| 89 |
def __str__(self): |
|---|
| 90 |
if hasattr(self, '__unicode__'): |
|---|
| 91 |
return force_unicode(self).encode('utf-8') |
|---|
| 92 |
return '%s object' % self.__class__.__name__ |
|---|
| 93 |
|
|---|
| 94 |
def __eq__(self, other): |
|---|
| 95 |
return isinstance(other, self.__class__) and self._get_pk_val() == other._get_pk_val() |
|---|
| 96 |
|
|---|
| 97 |
def __ne__(self, other): |
|---|
| 98 |
return not self.__eq__(other) |
|---|
| 99 |
|
|---|
| 100 |
def __init__(self, *args, **kwargs): |
|---|
| 101 |
dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs) |
|---|
| 102 |
|
|---|
| 103 |
# There is a rather weird disparity here; if kwargs, it's set, then args |
|---|
| 104 |
# overrides it. It should be one or the other; don't duplicate the work |
|---|
| 105 |
# The reason for the kwargs check is that standard iterator passes in by |
|---|
| 106 |
# args, and nstantiation for iteration is 33% faster. |
|---|
| 107 |
args_len = len(args) |
|---|
| 108 |
if args_len > len(self._meta.fields): |
|---|
| 109 |
# Daft, but matches old exception sans the err msg. |
|---|
| 110 |
raise IndexError("Number of args exceeds number of fields") |
|---|
| 111 |
|
|---|
| 112 |
fields_iter = iter(self._meta.fields) |
|---|
| 113 |
if not kwargs: |
|---|
| 114 |
# The ordering of the izip calls matter - izip throws StopIteration |
|---|
| 115 |
# when an iter throws it. So if the first iter throws it, the second |
|---|
| 116 |
# is *not* consumed. We rely on this, so don't change the order |
|---|
| 117 |
# without changing the logic. |
|---|
| 118 |
for val, field in izip(args, fields_iter): |
|---|
| 119 |
setattr(self, field.attname, val) |
|---|
| 120 |
else: |
|---|
| 121 |
# Slower, kwargs-ready version. |
|---|
| 122 |
for val, field in izip(args, fields_iter): |
|---|
| 123 |
setattr(self, field.attname, val) |
|---|
| 124 |
kwargs.pop(field.name, None) |
|---|
| 125 |
# Maintain compatibility with existing calls. |
|---|
| 126 |
if isinstance(field.rel, ManyToOneRel): |
|---|
| 127 |
kwargs.pop(field.attname, None) |
|---|
| 128 |
|
|---|
| 129 |
# Now we're left with the unprocessed fields that *must* come from |
|---|
| 130 |
# keywords, or default. |
|---|
| 131 |
|
|---|
| 132 |
for field in fields_iter: |
|---|
| 133 |
if kwargs: |
|---|
| 134 |
if isinstance(field.rel, ManyToOneRel): |
|---|
| 135 |
try: |
|---|
| 136 |
# Assume object instance was passed in. |
|---|
| 137 |
rel_obj = kwargs.pop(field.name) |
|---|
| 138 |
except KeyError: |
|---|
| 139 |
try: |
|---|
| 140 |
# Object instance wasn't passed in -- must be an ID. |
|---|
| 141 |
val = kwargs.pop(field.attname) |
|---|
| 142 |
except KeyError: |
|---|
| 143 |
val = field.get_default() |
|---|
| 144 |
else: |
|---|
| 145 |
# Object instance was passed in. Special case: You can |
|---|
| 146 |
# pass in "None" for related objects if it's allowed. |
|---|
| 147 |
if rel_obj is None and field.null: |
|---|
| 148 |
val = None |
|---|
| 149 |
else: |
|---|
| 150 |
try: |
|---|
| 151 |
val = getattr(rel_obj, field.rel.get_related_field().attname) |
|---|
| 152 |
except AttributeError: |
|---|
| 153 |
raise TypeError("Invalid value: %r should be a %s instance, not a %s" % |
|---|
| 154 |
(field.name, field.rel.to, type(rel_obj))) |
|---|
| 155 |
else: |
|---|
| 156 |
val = kwargs.pop(field.attname, field.get_default()) |
|---|
| 157 |
else: |
|---|
| 158 |
val = field.get_default() |
|---|
| 159 |
setattr(self, field.attname, val) |
|---|
| 160 |
|
|---|
| 161 |
if kwargs: |
|---|
| 162 |
for prop in kwargs.keys(): |
|---|
| 163 |
try: |
|---|
| 164 |
if isinstance(getattr(self.__class__, prop), property): |
|---|
| 165 |
setattr(self, prop, kwargs.pop(prop)) |
|---|
| 166 |
except AttributeError: |
|---|
| 167 |
pass |
|---|
| 168 |
if kwargs: |
|---|
| 169 |
raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0] |
|---|
| 170 |
dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self) |
|---|
| 171 |
|
|---|
| 172 |
def add_to_class(cls, name, value): |
|---|
| 173 |
if name == 'Admin': |
|---|
| 174 |
assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value)) |
|---|
| 175 |
value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')])) |
|---|
| 176 |
if hasattr(value, 'contribute_to_class'): |
|---|
| 177 |
value.contribute_to_class(cls, name) |
|---|
| 178 |
else: |
|---|
| 179 |
setattr(cls, name, value) |
|---|
| 180 |
add_to_class = classmethod(add_to_class) |
|---|
| 181 |
|
|---|
| 182 |
def _prepare(cls): |
|---|
| 183 |
# Creates some methods once self._meta has been populated. |
|---|
| 184 |
opts = cls._meta |
|---|
| 185 |
opts._prepare(cls) |
|---|
| 186 |
|
|---|
| 187 |
if opts.order_with_respect_to: |
|---|
| 188 |
cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True) |
|---|
| 189 |
cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False) |
|---|
| 190 |
setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls)) |
|---|
| 191 |
setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls)) |
|---|
| 192 |
|
|---|
| 193 |
# Give the class a docstring -- its definition. |
|---|
| 194 |
if cls.__doc__ is None: |
|---|
| 195 |
cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields])) |
|---|
| 196 |
|
|---|
| 197 |
if hasattr(cls, 'get_absolute_url'): |
|---|
| 198 |
cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url) |
|---|
| 199 |
|
|---|
| 200 |
dispatcher.send(signal=signals.class_prepared, sender=cls) |
|---|
| 201 |
|
|---|
| 202 |
_prepare = classmethod(_prepare) |
|---|
| 203 |
|
|---|
| 204 |
def save(self): |
|---|
| 205 |
dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) |
|---|
| 206 |
|
|---|
| 207 |
non_pks = [f for f in self._meta.fields if not f.primary_key] |
|---|
| 208 |
cursor = connection.cursor() |
|---|
| 209 |
|
|---|
| 210 |
# First, try an UPDATE. If that doesn't update anything, do an INSERT. |
|---|
| 211 |
pk_val = self._get_pk_val() |
|---|
| 212 |
pk_set = bool(pk_val) |
|---|
| 213 |
record_exists = True |
|---|
| 214 |
if pk_set: |
|---|
| 215 |
# Determine whether a record with the primary key already exists. |
|---|
| 216 |
cursor.execute("SELECT COUNT(*) FROM %s WHERE %s=%%s" % \ |
|---|
| 217 |
(backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), |
|---|
| 218 |
self._meta.pk.get_db_prep_lookup('exact', pk_val)) |
|---|
| 219 |
# If it does already exist, do an UPDATE. |
|---|
| 220 |
if cursor.fetchone()[0] > 0: |
|---|
| 221 |
db_values = [f.get_db_prep_save(f.pre_save(self, False)) for f in non_pks] |
|---|
| 222 |
if db_values: |
|---|
| 223 |
cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \ |
|---|
| 224 |
(backend.quote_name(self._meta.db_table), |
|---|
| 225 |
','.join(['%s=%%s' % backend.quote_name(f.column) for f in non_pks]), |
|---|
| 226 |
backend.quote_name(self._meta.pk.column)), |
|---|
| 227 |
db_values + self._meta.pk.get_db_prep_lookup('exact', pk_val)) |
|---|
| 228 |
else: |
|---|
| 229 |
record_exists = False |
|---|
| 230 |
if not pk_set or not record_exists: |
|---|
| 231 |
field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)] |
|---|
| 232 |
db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)] |
|---|
| 233 |
# If the PK has been manually set, respect that. |
|---|
| 234 |
if pk_set: |
|---|
| 235 |
field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)] |
|---|
| 236 |
db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)] |
|---|
| 237 |
placeholders = ['%s'] * len(field_names) |
|---|
| 238 |
if self._meta.order_with_respect_to: |
|---|
| 239 |
field_names.append(backend.quote_name('_order')) |
|---|
| 240 |
# TODO: This assumes the database supports subqueries. |
|---|
| 241 |
placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \ |
|---|
| 242 |
(backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.order_with_respect_to.column))) |
|---|
| 243 |
db_values.append(getattr(self, self._meta.order_with_respect_to.attname)) |
|---|
| 244 |
if db_values: |
|---|
| 245 |
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \ |
|---|
| 246 |
(backend.quote_name(self._meta.db_table), ','.join(field_names), |
|---|
| 247 |
','.join(placeholders)), db_values) |
|---|
| 248 |
else: |
|---|
| 249 |
# Create a new record with defaults for everything. |
|---|
| 250 |
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % |
|---|
| 251 |
(backend.quote_name(self._meta.db_table), |
|---|
| 252 |
backend.quote_name(self._meta.pk.column), |
|---|
| 253 |
backend.get_pk_default_value())) |
|---|
| 254 |
if self._meta.has_auto_field and not pk_set: |
|---|
| 255 |
setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) |
|---|
| 256 |
transaction.commit_unless_managed() |
|---|
| 257 |
|
|---|
| 258 |
# Run any post-save hooks. |
|---|
| 259 |
dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) |
|---|
| 260 |
|
|---|
| 261 |
save.alters_data = True |
|---|
| 262 |
|
|---|
| 263 |
def validate(self): |
|---|
| 264 |
""" |
|---|
| 265 |
First coerces all fields on this instance to their proper Python types. |
|---|
| 266 |
Then runs validation on every field. Returns a dictionary of |
|---|
| 267 |
field_name -> error_list. |
|---|
| 268 |
""" |
|---|
| 269 |
error_dict = {} |
|---|
| 270 |
invalid_python = {} |
|---|
| 271 |
for f in self._meta.fields: |
|---|
| 272 |
try: |
|---|
| 273 |
setattr(self, f.attname, f.to_python(getattr(self, f.attname, f.get_default()))) |
|---|
| 274 |
except validators.ValidationError, e: |
|---|
| 275 |
error_dict[f.name] = e.messages |
|---|
| 276 |
invalid_python[f.name] = 1 |
|---|
| 277 |
for f in self._meta.fields: |
|---|
| 278 |
if f.name in invalid_python: |
|---|
| 279 |
continue |
|---|
| 280 |
errors = f.validate_full(getattr(self, f.attname, f.get_default()), self.__dict__) |
|---|
| 281 |
if errors: |
|---|
| 282 |
error_dict[f.name] = errors |
|---|
| 283 |
return error_dict |
|---|
| 284 |
|
|---|
| 285 |
def _collect_sub_objects(self, seen_objs): |
|---|
| 286 |
""" |
|---|
| 287 |
Recursively populates seen_objs with all objects related to this object. |
|---|
| 288 |
When done, seen_objs will be in the format: |
|---|
| 289 |
{model_class: {pk_val: obj, pk_val: obj, ...}, |
|---|
| 290 |
model_class: {pk_val: obj, pk_val: obj, ...}, ...} |
|---|
| 291 |
""" |
|---|
| 292 |
pk_val = self._get_pk_val() |
|---|
| 293 |
if pk_val in seen_objs.setdefault(self.__class__, {}): |
|---|
| 294 |
return |
|---|
| 295 |
seen_objs.setdefault(self.__class__, {})[pk_val] = self |
|---|
| 296 |
|
|---|
| 297 |
for related in self._meta.get_all_related_objects(): |
|---|
| 298 |
rel_opts_name = related.get_accessor_name() |
|---|
| 299 |
if isinstance(related.field.rel, OneToOneRel): |
|---|
| 300 |
try: |
|---|
| 301 |
sub_obj = getattr(self, rel_opts_name) |
|---|
| 302 |
except ObjectDoesNotExist: |
|---|
| 303 |
pass |
|---|
| 304 |
else: |
|---|
| 305 |
sub_obj._collect_sub_objects(seen_objs) |
|---|
| 306 |
else: |
|---|
| 307 |
for sub_obj in getattr(self, rel_opts_name).all(): |
|---|
| 308 |
sub_obj._collect_sub_objects(seen_objs) |
|---|
| 309 |
|
|---|
| 310 |
def delete(self): |
|---|
| 311 |
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname) |
|---|
| 312 |
|
|---|
| 313 |
# Find all the objects than need to be deleted |
|---|
| 314 |
seen_objs = SortedDict() |
|---|
| 315 |
self._collect_sub_objects(seen_objs) |
|---|
| 316 |
|
|---|
| 317 |
# Actually delete the objects |
|---|
| 318 |
delete_objects(seen_objs) |
|---|
| 319 |
|
|---|
| 320 |
delete.alters_data = True |
|---|
| 321 |
|
|---|
| 322 |
def _get_FIELD_display(self, field): |
|---|
| 323 |
value = getattr(self, field.attname) |
|---|
| 324 |
return force_unicode(dict(field.choices).get(value, value), strings_only=True) |
|---|
| 325 |
|
|---|
| 326 |
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): |
|---|
| 327 |
op = is_next and '>' or '<' |
|---|
| 328 |
where = '(%s %s %%s OR (%s = %%s AND %s.%s %s %%s))' % \ |
|---|
| 329 |
(backend.quote_name(field.column), op, backend.quote_name(field.column), |
|---|
| 330 |
backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column), op) |
|---|
| 331 |
param = smart_str(getattr(self, field.attname)) |
|---|
| 332 |
q = self.__class__._default_manager.filter(**kwargs).order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name) |
|---|
| 333 |
q._where.append(where) |
|---|
| 334 |
q._params.extend([param, param, getattr(self, self._meta.pk.attname)]) |
|---|
| 335 |
try: |
|---|
| 336 |
return q[0] |
|---|
| 337 |
except IndexError: |
|---|
| 338 |
raise self.DoesNotExist, "%s matching query does not exist." % self.__class__._meta.object_name |
|---|
| 339 |
|
|---|
| 340 |
def _get_next_or_previous_in_order(self, is_next): |
|---|
| 341 |
cachename = "__%s_order_cache" % is_next |
|---|
| 342 |
if not hasattr(self, cachename): |
|---|
| 343 |
op = is_next and '>' or '<' |
|---|
| 344 |
order_field = self._meta.order_with_respect_to |
|---|
| 345 |
where = ['%s %s (SELECT %s FROM %s WHERE %s=%%s)' % \ |
|---|
| 346 |
(backend.quote_name('_order'), op, backend.quote_name('_order'), |
|---|
| 347 |
backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), |
|---|
| 348 |
'%s=%%s' % backend.quote_name(order_field.column)] |
|---|
| 349 |
params = [self._get_pk_val(), getattr(self, order_field.attname)] |
|---|
| 350 |
obj = self._default_manager.order_by('_order').extra(where=where, params=params)[:1].get() |
|---|
| 351 |
setattr(self, cachename, obj) |
|---|
| 352 |
return getattr(self, cachename) |
|---|
| 353 |
|
|---|
| 354 |
def _get_FIELD_filename(self, field): |
|---|
| 355 |
if getattr(self, field.attname): # value is not blank |
|---|
| 356 |
return os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname)) |
|---|
| 357 |
return '' |
|---|
| 358 |
|
|---|
| 359 |
def _get_FIELD_url(self, field): |
|---|
| 360 |
if getattr(self, field.attname): # value is not blank |
|---|
| 361 |
import urlparse |
|---|
| 362 |
return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/') |
|---|
| 363 |
return '' |
|---|
| 364 |
|
|---|
| 365 |
def _get_FIELD_size(self, field): |
|---|
| 366 |
return os.path.getsize(self._get_FIELD_filename(field)) |
|---|
| 367 |
|
|---|
| 368 |
def _save_FIELD_file(self, field, filename, raw_contents, save=True): |
|---|
| 369 |
directory = field.get_directory_name() |
|---|
| 370 |
try: # Create the date-based directory if it doesn't exist. |
|---|
| 371 |
os.makedirs(os.path.join(settings.MEDIA_ROOT, directory)) |
|---|
| 372 |
except OSError: # Directory probably already exists. |
|---|
| 373 |
pass |
|---|
| 374 |
filename = field.get_filename(filename) |
|---|
| 375 |
|
|---|
| 376 |
# If the filename already exists, keep adding an underscore to the name of |
|---|
| 377 |
# the file until the filename doesn't exist. |
|---|
| 378 |
while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)): |
|---|
| 379 |
try: |
|---|
| 380 |
dot_index = filename.rindex('.') |
|---|
| 381 |
except ValueError: # filename has no dot |
|---|
| 382 |
filename += '_' |
|---|
| 383 |
else: |
|---|
| 384 |
filename = filename[:dot_index] + '_' + filename[dot_index:] |
|---|
| 385 |
|
|---|
| 386 |
# Write the file to disk. |
|---|
| 387 |
setattr(self, field.attname, filename) |
|---|
| 388 |
|
|---|
| 389 |
full_filename = self._get_FIELD_filename(field) |
|---|
| 390 |
fp = open(full_filename, 'wb') |
|---|
| 391 |
fp.write(raw_contents) |
|---|
| 392 |
fp.close() |
|---|
| 393 |
|
|---|
| 394 |
# Save the width and/or height, if applicable. |
|---|
| 395 |
if isinstance(field, ImageField) and (field.width_field or field.height_field): |
|---|
| 396 |
from django.utils.images import get_image_dimensions |
|---|
| 397 |
width, height = get_image_dimensions(full_filename) |
|---|
| 398 |
if field.width_field: |
|---|
| 399 |
setattr(self, field.width_field, width) |
|---|
| 400 |
if field.height_field: |
|---|
| 401 |
setattr(self, field.height_field, height) |
|---|
| 402 |
|
|---|
| 403 |
# Save the object because it has changed unless save is False |
|---|
| 404 |
if save: |
|---|
| 405 |
self.save() |
|---|
| 406 |
|
|---|
| 407 |
_save_FIELD_file.alters_data = True |
|---|
| 408 |
|
|---|
| 409 |
def _get_FIELD_width(self, field): |
|---|
| 410 |
return self._get_image_dimensions(field)[0] |
|---|
| 411 |
|
|---|
| 412 |
def _get_FIELD_height(self, field): |
|---|
| 413 |
return self._get_image_dimensions(field)[1 |
|---|