My last post was about django-tagging.
After two days using it under: http://politlog.sveri.de our tags have become very fast confusing.
Adding a new entry and looking for tags in the entry and comparing them to tags already given away is a time consuming task. Nothing for me. So i started thinking and looking for automation. I found no solution for django, the webframework django-tagging is used for. I did not find another cool solution anyway.
My next thought was, hm, writing a parser is a task done in seconds, but writing a tag-list to parse could endure a whole life. Nothing for me too.
But then it came instantly to my mind, there is a very cool, fast and efficient semiautmatic solution.
Here is the general approach.
- Install a tagging system.
- Start using it and give away tags.
- Write a parser which parses your entry, comment, post whatsoever and compares them to the already given away tags.
- Take out the found tags and insert them into your tag field.
- Add the new tags.
Thats it. Fast, easy and after a short time you have inserted almost all tags that you’ll use most of the time. No more comparing, much less time consumption.
And here is a specific implementation into the django admin interface.
I have a model called Entries. There is a column explanation. Django-tagging is installed and stores its Tags in model called: tagging.models.Tags.
For javascript/ajax things i use jquery-1.3.2.
To enhance the admin interface you have to take some steps.
- In your templates directory create a folder admin.
- Then create the folder structure for the model you want to enhance, in my case it was:
mkdir entry
mkdir entry/entries
cp the change_form.html from django/contrib/admin/templates/admin/ to the created folder. - Open it up and insert a javascript into the block extrahead:
$(document).ready(function() {
$(‘#id_explanation’).change(function() {
data = {text:$(this).val()};
var args = {type:”POST”, url:”{% url ao_get_tags_from_text %}”, data:data, complete:done_tags};
$.ajax(args);
return false;
});
var done_tags = function(res, status){
if(status == “success”){
$(‘#id_tags’).val(res.responseText);
}
};
});
This script basically does the following. It checks the input field for explanation for a change and if so it calls via ajax the view ao_get_tags_from_text.
Afterwards it takes the data from the view and inserts it into the input field for the tags: id_tags. - And here comes the view:
def get_tags_from_text(request):
”’returns all matched tags that were found
in the given text as string, comma separated”’
if request.method == “POST”:
text = request.POST['text']
else:
return HttpResponseServerError(“invalid data in get politician from party view”)
tags = Tag.objects.all()
tags_in_text = ”
text = text.lower()
for tag in tags:
if tag.name.lower() in text:
tags_in_text = tags_in_text + tag.name + ‘, ‘
return render_to_response(‘admin/entry/entries/tags_return.html’,
{“tags”: tags_in_text,},RequestContext(request))
Thats it. Easy? Isnt it?
Of course, there is always a lot of room for improvement. But thats on your side.
Have fun with it.