From 7e379e220fd462b492e37b5e18153a597a956316 Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Mon, 18 Mar 2013 16:39:12 -0400 Subject: [PATCH] adding management command to strip existing Note.html into raw note.text for previews and search --- karmaworld/apps/notes/management/__init__.py | 0 .../notes/management/commands/__init__.py | 0 .../management/commands/strip_html_to_text.py | 22 +++++++++++++++++++ reqs/common.txt | 1 + 4 files changed, 23 insertions(+) create mode 100644 karmaworld/apps/notes/management/__init__.py create mode 100644 karmaworld/apps/notes/management/commands/__init__.py create mode 100644 karmaworld/apps/notes/management/commands/strip_html_to_text.py diff --git a/karmaworld/apps/notes/management/__init__.py b/karmaworld/apps/notes/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/karmaworld/apps/notes/management/commands/__init__.py b/karmaworld/apps/notes/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/karmaworld/apps/notes/management/commands/strip_html_to_text.py b/karmaworld/apps/notes/management/commands/strip_html_to_text.py new file mode 100644 index 0000000..2fa00c0 --- /dev/null +++ b/karmaworld/apps/notes/management/commands/strip_html_to_text.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +# Copyright (C) 2012 FinalsClub Foundation + +from lxml.html import fromstring + +from django.core.management.base import BaseCommand +from apps.notes.models import Note + +class Command(BaseCommand): + args = 'none' + help = "Take all notes with the .html property and use that to fill Note.text by stripping html" + + def handle(self, *args, **kwargs): + notes = Note.objects.filter(html__isnull=False).filter(text__isnull=True) + cleaned_notes = 0 + for note in notes: + note.text = fromstring(note.html).text_content() + note.save() + cleaned_notes += 1 + self.stdout.write('Processed %s notes' % cleaned_notes) + diff --git a/reqs/common.txt b/reqs/common.txt index d1655ab..2a42a6b 100644 --- a/reqs/common.txt +++ b/reqs/common.txt @@ -10,3 +10,4 @@ oauth2client==1.0 urllib3==1.5 google-api-python-client==1.0 django-grappelli==2.4.3 +lxml==3.1.0 -- 2.25.1