views:

80

answers:

2

I have a django model with a text field. I'm using a rich text editor (nicEdit) on the admin site to allow the client to easily enter markup into the field. I'd like to process the contents of the field and perform a few actions before anything is inserted into the database.

For example, I want to strip junk generated by MS Word, font tags, etc. I hope this part should be easy, but I'm not sure what to override or hook to get this working.

I also want to detect remotely-linked images, download a local copy to MEDIA_ROOT, and relink the img src to the local image. I'm not quite sure how to go about fetching the remote image; I thought django.Storage might help but it looks like it's unable to fetch content from a remote URL.

Any suggestions?

+3  A: 

Stripping the junk and such should be done with a custom formfield.

Downloading the images... there are multiple ways to fix that problem.

  • If you choose to store the image location and original location in the database, than you should do it with a pre-save signal.
  • If you choose to store the images locally directly, than you can make it part of the formfield aswell. Simply download all remote images and replace the urls with a local url.
WoLpH
I should have mentioned that I haven't messed with forms at all yet, let alone creating custom form fields. If you could post a short example I'd be eternally grateful. Also, _how_ do I download the images?
no
@no: in that case, why don't you try something like this: http://code.google.com/p/django-richtext/
WoLpH
Thanks, I'll take a look at the source. The django-photologue project linked from there looks good too. This still doesn't solve the image downloading problem, but it looks like urllib can cover that.
no
re: django-richtext: how the heck do you install this thing?
no
Installing should be as simple as using `content = AdminRichTextField()` in your model and/or `message = RichTextField()` in your form.
WoLpH
It was... Somehow comments starting at the beginning of the line are significant in python, aren't they? They delineate modules or something? I think that's what was messing me up before.
no
Comments have no real importance in Python but they can be used for documentation (i.e. `help(function)` will return it's docoumentation) and testing with `doctests`. However, whitespace is very important in Python. So if you accidently mix tabs and spaces or use uneven indenting than your code can do unexpected things.
WoLpH
hmm, maybe some tabs snuck in there. looking at this now: http://docs.djangoproject.com/en/dev/topics/signals/
no
+4  A: 

To manipulate data in your model before saving it, use the save() method like:

    def save(self):
      self.NameOfTextField = myCustomCleanFunction(self.NameOfTextField)
      super(YourModelName, self).save()

Nothing will be saved until super(modelname, self).save() is executed.

If you want the possibility of raising some type of error instead of just processing it silently, you'll probably want to use the clean() method with raise ValidationError().

Downloading remote content is a new one for me, so I can't help you there. You might have to look past Django and find Python functions to do the job.

Jamie
This sounds like what I'm looking for. I'll try it later and report back.
no
It works, but it's not reusable, unless there's something I can replace `YourModelName` with in an abstract base class to get the right class.
no
@no: You can easily make it reusable by adding that code in a separate method with a `signal` instead of overwriting the `save` code.
WoLpH
So if I don't override save, how can they save it from the admin interface?
no
@no: You can use the pre_save signal instead of overriding `save`. http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.pre_save
WoLpH