Hi! It's usually best to provide code examples at dpaste.com, and not to drop long code sections into the post.
I also prefer the decorator syntax for Model method attributes. The following paste contains the decorators the I use for this purpose. http://dpaste.com/67425/ These decorators are so short and trivial, that all four of them, with doc strings, are shorter, and much, much clearer, than the factory method above. I think the factory is a bit overkill in this situation and doesn't buy a whole lot of DRY. Anyhow, I'd also like to see these decorators leaving my utilities and landing in django.db.models sometime, but right now there are bigger fish to fry. Wayne On Jul 24, 9:54 am, zvoase <[EMAIL PROTECTED]> wrote: > Developers, > I've just looked at the latest revision (r8068) for Django and there > are a couple of things I'd like to suggest. > > First, could a ``write_to`` method be added to > ``django.core.files.uploadedfile.UploadedFile``, which would > automatically write the uploaded file to a file-like object? This > method could handle all the chunk-related logic of writing uploaded > files. Example usage would look something like this:: > > def handle_uploaded_file(uploaded_file): > destination = open('some/file/name.txt', 'wb+') > uploaded_file.write_to(destination) > > That example was lifted right out of the docs for 'File Uploads'. In > the documented version, a loop is needed, which writes the chunks > piecemeal to the file. A ``write_to`` method would handle all of this, > and could help cater to possible future changes in the way the chunk > system works. The ``write_to`` method would look something like this:: > > class UploadedFile(object): > ... > def write_to(self, filehandle): > for chunk in self.chunks(): > filehandle.write(chunk) > > It's very simple, but would save time and would allow the chunks > mechanism to make small (or even big) changes and not cause developers > to have to change their file-upload code. If you want, I can write a > patch against the current revision. > > Secondly, in admin definitions, it is often necessary to add some > extra attributes to methods on the ``ModelAdmin`` or ``Model`` > subclasses. The list of these (4) attributes is here: > > * short_description => User-defined string > * allow_tags => ``True`` > * boolean => ``True`` > * admin_order_field => User-defined string > > ``allow_tags`` and ``boolean`` are always going to be ``True``, > because if they are not there then the admin app considers them to be > ``False`` by default. > > These attributes are given to model methods in order to affect the > behaviour or presentation of the admin site, and they are usually done > like so:: > > class MyModel(models.Model): > field1 = ... > field2 = ... > > def custom_method(self): > return something() > custom_method.short_description = 'Custom method...' > > I propose the addition of 4 decorator functions, to > ``django.contrib.admin`` (or a relevant submodule), which could be > used to decorate functions so that these attributes could be set > automatically, like so:: > > class MyModel(models.Model): > ... > > @short_description('Custom method...') > def custom_method(self): > return something() > > I feel that this way is far more Pythonic, and makes code look a lot > nicer. I've put some Python source at the end of this message which > demonstrates the idea, and defines a generic attribute-setting > decorator-making function which allows more decorators to be created > if the admin changes in the future. > > I hope that these suggestions are useful, > > Regards, > Zack > > Here's the code for the proposed admin decorators: > > <code> > def attribute_decorator(attr, set_default=False, default=None): > def ad_wrapper(value): > def ad_wrapper_2(function): > setattr(function, attr, value) > return function > return ad_wrapper_2 > def ad_default_wrapper(function): > setattr(function, attr, default) > return function > if set_default: > return ad_default_wrapper > return ad_wrapper > > short_description = attribute_decorator('short_description') > admin_order_field = attribute_decorator('admin_order_field') > allow_tags = attribute_decorator('allow_tags', set_default=True, > default=True) > boolean = attribute_decorator('boolean', set_default=True, > default=True) > </code> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
