<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GrenadePod &#187; programming</title>
	<atom:link href="http://www.grenadepod.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.grenadepod.com</link>
	<description>Dispersing the Seeds</description>
	<lastBuildDate>Mon, 22 Feb 2010 20:30:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Python web crawler</title>
		<link>http://www.grenadepod.com/2009/12/13/python-web-crawler/</link>
		<comments>http://www.grenadepod.com/2009/12/13/python-web-crawler/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 20:57:51 +0000</pubDate>
		<dc:creator>pulegium</dc:creator>
				<category><![CDATA[IT Technology]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.grenadepod.com/?p=633</guid>
		<description><![CDATA[For one of my (upcoming) projects I needed to write a simple webcrawler. Just as I always do, I searched Google (obviously), but couldn&#8217;t find anything simple enough. Or good enough for me for that matter. So spent an hr or so writing most simplistic webcrawler myself. Application logic is extremely simple: Retrieve specified page [...]


Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/10/changing-menu-order/' rel='bookmark' title='Permanent Link: Changing menu order'>Changing menu order</a></li>
<li><a href='http://www.grenadepod.com/2009/11/04/top-level-menu-in-arras-theme/' rel='bookmark' title='Permanent Link: Top level menu in Arras theme'>Top level menu in Arras theme</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p id="top" />For one of my (upcoming) projects I needed to write a simple webcrawler. Just as I always do, I searched Google (obviously), but couldn&#8217;t find anything simple enough. Or good enough for me for that matter. So spent an hr or so writing most simplistic webcrawler myself.</p>
<p>Application logic is extremely simple:</p>
<ul>
<li>Retrieve specified page</li>
<li>If cannot retrieve, try another from stored in the DB</li>
<li>Store all found URLs (&lt;a href&gt;) in the DB</li>
<li>Return one (and remove from DB)</li>
</ul>
<p>I store all pages in memory, but for more serious crawling, consider storing data on a physical file, that way it&#8217;ll be more memory efficient. Considering the size of the internets, even if you store only URL strings you will not last for very long&#8230;</p>
<p>Anyway, happy crawling! (and let me know if there are any issues, so far it worked fine for me)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> BeautifulSoup <span style="color: #ff7700;font-weight:bold;">import</span> BeautifulSoup
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>, <span style="color: #dc143c;">random</span>, <span style="color: #dc143c;">re</span>, sqlite3, <span style="color: #dc143c;">logging</span>
&nbsp;
DATABASE   = <span style="color: #483d8b;">':memory:'</span>
LOG_LEVEL  = <span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span>
ABS_URL_RE = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(?P&lt;url&gt;https?://.+?/)'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> WebCrawler:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, logging_level=LOG_LEVEL, database=DATABASE<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=logging_level<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">conn</span> = sqlite3.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>database<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'create table url_stack (url text)'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'create table url_visited (url text)'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _form_url<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, url, link<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> link<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'http'</span>:
            ret_url = link
        <span style="color: #ff7700;font-weight:bold;">elif</span> link<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'/'</span>:
            m = ABS_URL_RE.<span style="color: black;">search</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
            ret_url = <span style="color: #483d8b;">&quot;%s%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'url'</span><span style="color: black;">&#41;</span>, link<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            ret_url = <span style="color: #483d8b;">&quot;%s%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>url, link<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> ret_url
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _pop_from_db<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Retrieving one URL from the DB...&quot;</span><span style="color: black;">&#41;</span>
        res = <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'select url from url_stack limit 1'</span><span style="color: black;">&#41;</span>.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Query result: %s&quot;</span>, res<span style="color: black;">&#41;</span>
        url = res<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;delete from url_stack where url = ?&quot;</span>, <span style="color: black;">&#40;</span>url,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> url
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _push_to_db<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, url<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Inserting record into DB...&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Check if it hasn't been visited yet&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'select url from url_visited where url=?'</span>, <span style="color: black;">&#40;</span>url,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;URL not found in list of visited URLs, inserting&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;URL to insert: %s&quot;</span> <span style="color: #66cc66;">%</span> url<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into url_stack values (?)'</span>, <span style="color: black;">&#40;</span>url,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into url_visited values (?)'</span>, <span style="color: black;">&#40;</span>url,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;URL has already been visited or added for processing, skipping&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> crawl<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, url<span style="color: black;">&#41;</span>:
        work_url = url
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Work URL: %s&quot;</span> <span style="color: #66cc66;">%</span> work_url<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Trying to open and parse the URL...&quot;</span><span style="color: black;">&#41;</span>
                page = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>work_url<span style="color: black;">&#41;</span>
                soup = BeautifulSoup<span style="color: black;">&#40;</span>page<span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Parsed successfuly&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span>:
                <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Failed to parse, attempting to get next URL from DB&quot;</span><span style="color: black;">&#41;</span>
                work_url = <span style="color: #008000;">self</span>._pop_from_db<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">continue</span>
            links = soup<span style="color: black;">&#40;</span><span style="color: #483d8b;">'a'</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Found total of %d links (&lt;a href=...&gt;)&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>links<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> link <span style="color: #ff7700;font-weight:bold;">in</span> soup<span style="color: black;">&#40;</span><span style="color: #483d8b;">'a'</span><span style="color: black;">&#41;</span>:
                <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Processing link object: %s&quot;</span> <span style="color: #66cc66;">%</span> link<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                    <span style="color: #ff7700;font-weight:bold;">if</span> link<span style="color: black;">&#91;</span><span style="color: #483d8b;">'href'</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">''</span>:
                        <span style="color: #008000;">self</span>._push_to_db<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._form_url<span style="color: black;">&#40;</span>work_url, link<span style="color: black;">&#91;</span><span style="color: #483d8b;">'href'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span>:
                    <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;An exception has occured, this may be ok (href attribute may be missing)&quot;</span><span style="color: black;">&#41;</span>
                    <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;  ... but can also indicate error in insert code&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Finished adding URLs&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Getting a new URL for processing from DB&quot;</span><span style="color: black;">&#41;</span>
            work_url = <span style="color: #008000;">self</span>._pop_from_db<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Found URL: %s&quot;</span> <span style="color: #66cc66;">%</span> work_url<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">yield</span> work_url
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    wc = WebCrawler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> url <span style="color: #ff7700;font-weight:bold;">in</span> wc.<span style="color: black;">crawl</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.google.com/'</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>As you can see it&#8217;s very easy to use. Effectively it is an infinite generator (well, depending what you pass as initial URL). It&#8217;s then up to you what you&#8217;re going to do with the resulting URL&#8230;</p>
<p>One thing to bear in mind when you use it: this crawler pays absolutely no attention to the domain it&#8217;s searching. It just blindly collects links, selects one and follows it. So depending on the site link relative location it may stay for a while on a site, or may just wonder away quite quickly.</p>
<p>I wanted to have something that does not stick around for long on one site, so this suits me well, if you want to have something more pedantic, you may want to modify the code, so that it leaves current domain only when it has visited all pages and there are no new pages within the domain to analyse.</p>


<p>Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/10/changing-menu-order/' rel='bookmark' title='Permanent Link: Changing menu order'>Changing menu order</a></li>
<li><a href='http://www.grenadepod.com/2009/11/04/top-level-menu-in-arras-theme/' rel='bookmark' title='Permanent Link: Top level menu in Arras theme'>Top level menu in Arras theme</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.grenadepod.com/2009/12/13/python-web-crawler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing my first WordPress plugin</title>
		<link>http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/</link>
		<comments>http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 14:28:54 +0000</pubDate>
		<dc:creator>pulegium</dc:creator>
				<category><![CDATA[IT Technology]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.grenadepod.com/?p=435</guid>
		<description><![CDATA[It&#8217;s only couple of weeks that I&#8217;m using WordPress (actively!) and it&#8217;s been quite some time I last used PHP. There was a time when I was really comfortable with PHP, but that&#8217;s been oh so long ago. I&#8217;m all in Python nowadays. No stupid endless  curly brackets, or semicolons and the nice indentation is [...]


Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/21/securing-wordpress/' rel='bookmark' title='Permanent Link: Securing WordPress'>Securing WordPress</a></li>
<li><a href='http://www.grenadepod.com/2009/11/06/essential-wordpress-plugins/' rel='bookmark' title='Permanent Link: Essential WordPress plugins'>Essential WordPress plugins</a></li>
<li><a href='http://www.grenadepod.com/2009/11/23/use-ssh-to-upgrade-wordpress-plugins-automatically/' rel='bookmark' title='Permanent Link: Use SSH to upgrade WordPress plugins automatically'>Use SSH to upgrade WordPress plugins automatically</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p id="top" />It&#8217;s only couple of weeks that I&#8217;m using WordPress (actively!) and it&#8217;s been quite some time I last used PHP. There was a time when I was really comfortable with PHP, but that&#8217;s been oh so long ago. I&#8217;m all in Python nowadays. No stupid endless  curly brackets, or semicolons and the nice indentation is enforced&#8230; Ok, ok, it&#8217;s just me ranting, because I spent nearly 30 mins trying to figure out what&#8217;s wrong with my code. It appeared to be a missing $ in front of my variable name. How lame could that be?&#8230;</p>
<p>Right, back to business. So I decided to write a WordPress plugin. There were few reasons I wanted to do that:</p>
<ul>
<li>See how difficult it is and what WordPress looks like under the hood</li>
<li>Since I&#8217;m using WP, which is a PHP shop I needed to remind myself how PHP looks like</li>
<li>Get some more publicity and bump up the Google rating (that&#8217;ll never hurt, will it?)</li>
</ul>
<p>So, I remembered seeing this neat trick of reversing, or flipping over text by replacing regular ASCII latin characters with Unicode ones that resemble a mirror image of the original ones. There are numerous websites out there on the wild internets that would swallow your piece of texts and then spit out same text, but flipped over.</p>
 p&#x01DDs&#x0279&#x01DD&#x028C&#x01DD&#x0279 u&#x01DD&#x01DDq s&#x0250&#x0265 &#x0287&#x0131 &#x0279&#x01DD&#x0287&#x025F&#x0250 &#x01DD&#x029E&#x0131&#x0283 &#x029Eoo&#x0283 p&#x0283no&#x028D &#x0287x&#x01DD&#x0287 &#x01DD&#x0265&#x0287 &#x028Do&#x0265 s&#x0131 &#x01DD&#x0279&#x01DDH
<p>As you can see it is still text information, no magic with images or anything like that. You can select, copy-paste, etc.</p>
<p>So, I thought, right, let&#8217;s see if there was someone smart who has created a plugin for WordPress that would do this. Not that I&#8217;m a big fan of flipped over text, but just to make sure that I&#8217;m not reinventing the wheel. At the end of the day, what&#8217;s the point in creating something that&#8217;s already been build. If such plugin existed, I would have had to think of something else. Lucky for me, no one has yet created anything like it.</p>
<p>So, simple idea, just what I need to warm up.</p>
<p>First things first, go and read <a href="http://codex.wordpress.org/Writing_a_Plugin" target="_blank">official manual</a> from WordPress about how to develop plugins. It&#8217;s a really good manual, and everyone developing a plugin must read it. Or read it if you feel you need more details in following my manual&#8230; <img src='http://www.grenadepod.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>All plugins go into /wp-content/plugins/ directory in your WordPress installation. Each plugin has it&#8217;s own directory, but this isn&#8217;t a strict requirement, but if you&#8217;re not creating a directory for your plugin, it must be just a single PHP file. I&#8217;d advise to create a directory for your plugin.</p>
<p>Right, this is how I saw me implementing this: user types in any text in the post (or page, or comment, or even excerpt) and encloses any text he/she wants to flip over in my new (made up) tags, just like that:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;upsidedown&amp;gt;your text&amp;lt;/upsidedown&amp;gt;</pre></div></div>

<p>Because WordPress does some clever validation of the text and only certain tags are allowed, I had to include this code in my plugin to declare my new tags as valid ones:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>CUSTOM_TAGS<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// allow new tag in the posts</span>
    <span style="color: #000088;">$allowedposttags</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'upsidedown'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// ... and also in the comments</span>
    <span style="color: #000088;">$allowedtags</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'upsidedown'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next thing is to register new filter function with WordPress. Hold on, what is this filter function? Filter function is a function that WordPress calls and passes contents of the post to it. Filter function can do anything it likes to the text and then spits is back to WordPress engine. WordPress will then pass the result to another filter, and so on and on until there are no more filter registered. Each filter get a priority flag assigned, and those with priority 0 will get executed before those that have priority 10. By default each filter gets priority 6 assigned to them.</p>
<p>Right, going back to my filters, let&#8217;s register them:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flip_text_upside_down_filter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_excerpt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flip_text_upside_down_filter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'comment_text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flip_text_upside_down_filter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I register my filter with three types of content: post (or page), excerpt and comment.</p>
<p>Next thing is to define the filter function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> flip_text_upside_down_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'/\s*&amp;lt;upsidedown&amp;gt;(.*)&amp;lt;\/upsidedown&amp;gt;/siU'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'convert_to_upsidedown_unicode'</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$content</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>WordPress is going to call it with a parameter that contains content text in it. I pass the contents to PHP function preg_replace_callback, which matches the regular expression (any text in between my tags) and calls another function with the contents of the regular expression match (hope that makes sense to you, if not, read the sentence again, repeat until you got it&#8230; <img src='http://www.grenadepod.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).<br />
Finally, a function that actually does the conversion. Which is pretty simple &#8211; go through all characters and replace them with matching Unicode chars from the table. I&#8217;m not including the whole table here, you can download source code from WordPress plugins directory (in fact, if you install the plugin &#8211; you&#8217;ll have the sourcecode in your plugins/ directory):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> convert_to_upsidedown_unicode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// conversion table</span>
    <span style="color: #000088;">$flipTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #339933;">...</span>
    <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$origStr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$newStr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$origStr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$origStr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$flipTable</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$newStr</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$flipTable</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$newStr</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$newStr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is it. Well, ok nearly it. You still need to do the boring bit, ie put a formal header so WP recognises your plugin code (put this, replacing what&#8217;s necessary accordingly):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
Plugin Name: Upside Down Text
Plugin URI: http://www.grenadepod.com/projects/upside-down-text-plugin-for-wordpress/
Description: This plugin allows to &quot;flip&quot; any section of text upside down
Version: 1.1.0
Author: Grenadepod
Author URI: http://www.grenadepod.com
*/</span></pre></div></div>

<p>Now you can activate the plugin from your WP admin console, and start typing text that everyone struggles to read.</p>
<p>Sooner or later you&#8217;ll notice that the implementation is not ideal. When you switch over to visual editor (which I use most of the time) and then back to HTML editor, all &#8220;new&#8221; tags are gone. This is because the editor does not know about them and so it removes them. Solution was to type in text in Visual (if you prefer to use it) then switch to HTML and place tags where needed. Save without switching back, and publish.</p>
<p>This wasn&#8217;t really nice, and I wasn&#8217;t going to get lots of users with this approach. Some nice soul on WordPress forum suggested to have a look at <a href="http://codex.wordpress.org/Shortcode_API" target="_blank">Shortcode API</a>. This API allows plugin to register shortcode that looks like: [somethinghere]content[/somethinghere], and passes all content back to your function. And this would be immune to switching between editors issue.</p>
<p>So I decided to make a second release of the plugin, where shortcodes are supported alongside with tags. And in fact using shortcodes not only allows me to type them in directly in the Visual editor, but also coding is simpler, I don&#8217;t need to do any regexp magic.</p>
<p>Register my new shortcode:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'upsidedown'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flip_text_upside_down_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And define what function to call:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> flip_text_upside_down_shortcode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$atts</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> convert_to_upsidedown_unicode<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, I&#8217;m reusing same character conversion function, so the changes were minimal.</p>
<p>All seems to be working quite fine, at the end of the day, what can go wrong with only few lines of code?&#8230;</p>


<p>Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/21/securing-wordpress/' rel='bookmark' title='Permanent Link: Securing WordPress'>Securing WordPress</a></li>
<li><a href='http://www.grenadepod.com/2009/11/06/essential-wordpress-plugins/' rel='bookmark' title='Permanent Link: Essential WordPress plugins'>Essential WordPress plugins</a></li>
<li><a href='http://www.grenadepod.com/2009/11/23/use-ssh-to-upgrade-wordpress-plugins-automatically/' rel='bookmark' title='Permanent Link: Use SSH to upgrade WordPress plugins automatically'>Use SSH to upgrade WordPress plugins automatically</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Query data from Django site with jQuery</title>
		<link>http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/</link>
		<comments>http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:06:37 +0000</pubDate>
		<dc:creator>pulegium</dc:creator>
				<category><![CDATA[IT Technology]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.grenadepod.com/?p=331</guid>
		<description><![CDATA[Right, here&#8217;s what I needed to do: I have a list of IP addresses displayed on a web page and I want to show the status of every each of them. By &#8220;status&#8221; I mean just perform a simple ping and display OK next to the IP, or &#8216;no response&#8217; otherwise. Listing is a Django [...]


Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/' rel='bookmark' title='Permanent Link: How to use generic views in Django'>How to use generic views in Django</a></li>
<li><a href='http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/' rel='bookmark' title='Permanent Link: Developing my first WordPress plugin'>Developing my first WordPress plugin</a></li>
<li><a href='http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/' rel='bookmark' title='Permanent Link: Using OpenID for authentication in Django'>Using OpenID for authentication in Django</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p id="top" />Right, here&#8217;s what I needed to do: I have a list of IP addresses displayed on a web page and I want to show the status of every each of them. By &#8220;status&#8221; I mean just perform a simple ping and display OK next to the IP, or &#8216;no response&#8217; otherwise.</p>
<p>Listing is a Django application, so theoretically I could just go through the list of IPs, ping and add status to the list that gets passed to Django template. But nice it is not. It might take ages to query the whole list, and even if I spend time implementing multithreaded ping and ping them all concurrently it still going to take at leas couple of seconds to complete.</p>
<p>So obvious choice is to delay the ping action. I will serve the page with all IPs and other bits and pieces and then let my JavaScript to do the AJAX magic.</p>
<p>So, how do I do that?</p>
<p>Well, first I need a view that checks if the given IP responds to a ping and displays the result. To keep things simple, I&#8217;m going to return a string, which will be displayed on the list page:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> ping<span style="color: black;">&#40;</span>request, address=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> responding_to_ping<span style="color: black;">&#40;</span>address<span style="color: black;">&#41;</span>:
        msg = <span style="color: #483d8b;">&quot;Ping OK&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        msg = <span style="color: #483d8b;">&quot;No response&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span></pre></div></div>

<p>I then need to add URL pattern, so I can call the view:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^ping/(?P&lt;address&gt;<span style="color: #000099; font-weight: bold;">\d</span>{1,3}<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>{1,3}<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>{1,3}<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>{1,3})/$'</span>, views.<span style="color: black;">ping</span><span style="color: black;">&#41;</span>,</pre></div></div>

<p>Good, so now I can navigate to http://<hostname>/ping/<ip_address>/ and it&#8217;ll come back with either &#8220;Ping OK&#8221; or &#8220;No response&#8221; message.<br />
Right, now let&#8217;s add some JavaScript magic that&#8217;ll query the server and update my web page with details.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.address&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> curId <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            updateStatus<span style="color: #009900;">&#40;</span>curId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> updateStatus<span style="color: #009900;">&#40;</span>attrId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        address <span style="color: #339933;">=</span> attrId.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ip_'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        address <span style="color: #339933;">=</span> address.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/_/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/ping/'</span> <span style="color: #339933;">+</span> address<span style="color: #339933;">,</span>
            success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> attrId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>So, what exactly is happening here?</p>
<ul>
<li><strong>Line 3:</strong> This waits for a document too load and call the callback function when ready to proceed, calback function defined in the lines below</li>
<li><strong>Lines 4-6:</strong>Goes through each element of the HTML document (my web page) that are of class &#8220;address&#8221;, extracts the element&#8217;s &#8216;id&#8217; attribute, and calls &#8216;updateStatus&#8217; function passing the &#8216;id&#8217; to it. So all elements that are going to be updated should look like: &lt;span class=&#8221;address&#8221; id=&#8221;ip_address&#8221; /&gt;</li>
<li><strong>Lines 11-12:</strong> All id&#8217;s are going to be in the following format: ip_1_2_3_4, so before I pass this to the URL, I need to strip the ip_ bit, and replace underscores with dots.</li>
<li><strong>Lines 13-17:</strong> This is where I perform AJAX call to the URL. The &#8216;success&#8217; bit is a call to the function that gets executed when the result is successfully retrieved. This function is pretty simple, it just replaces the contents of the tag with whatever it receives from the server</li>
</ul>
<p>And this is it, all that is left to do is list all IPs that need checking. Below is the raw HTML, but I trust you can be creative in writing a decent Django template to generate the page.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
    &lt;li&gt;192.168.0.1 [Status: &lt;span class=&quot;address&quot; id=&quot;ip_192_168_0_1&quot;&gt;Unknown&lt;/span&gt; ]&lt;/li&gt;
    &lt;li&gt;192.168.0.2 [Status: &lt;span class=&quot;address&quot; id=&quot;ip_192_168_0_2&quot;&gt;Unknown&lt;/span&gt; ]&lt;/li&gt;
    &lt;li&gt;192.168.0.3 [Status: &lt;span class=&quot;address&quot; id=&quot;ip_192_168_0_3&quot;&gt;Unknown&lt;/span&gt; ]&lt;/li&gt;
    ...
&lt;/ul&gt;</pre></div></div>



<p>Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/' rel='bookmark' title='Permanent Link: How to use generic views in Django'>How to use generic views in Django</a></li>
<li><a href='http://www.grenadepod.com/2009/11/18/developing-my-first-wordpress-plugin/' rel='bookmark' title='Permanent Link: Developing my first WordPress plugin'>Developing my first WordPress plugin</a></li>
<li><a href='http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/' rel='bookmark' title='Permanent Link: Using OpenID for authentication in Django'>Using OpenID for authentication in Django</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to use generic views in Django</title>
		<link>http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/</link>
		<comments>http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 23:05:45 +0000</pubDate>
		<dc:creator>pulegium</dc:creator>
				<category><![CDATA[IT Technology]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.grenadepod.com/?p=256</guid>
		<description><![CDATA[This is my first &#8220;technical&#8221; post here, so bear with me on this one. Sooner or later in your Django development career you&#8217;ll notice that you keep on repeating and implementing same views over and over again: list objects display details for any particular object add/delete/modify the object When I was reading about Django, I&#8217;ve [...]


Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/' rel='bookmark' title='Permanent Link: Query data from Django site with jQuery'>Query data from Django site with jQuery</a></li>
<li><a href='http://www.grenadepod.com/2009/12/13/python-web-crawler/' rel='bookmark' title='Permanent Link: Python web crawler'>Python web crawler</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p id="top" />This is my first &#8220;technical&#8221; post here, so bear with me on this one.</p>
<p>Sooner or later in your Django development career you&#8217;ll notice that you keep on repeating and implementing same views over and over again:</p>
<ul>
<li>list objects</li>
<li>display details for any particular object</li>
<li>add/delete/modify the object</li>
</ul>
<p>When I was reading about Django, I&#8217;ve noticed something mentioned that was called &#8220;generic views&#8221;, but at that time it somehow escaped me, and I wrote numerous views, just to implement those functions I mentioned above. In some extreme cases I&#8217;ve had 3-4 different views, effectivelly implementing the same for different models.</p>
<p>So the time came to educate myself and learn about &#8220;generic views&#8221; that are designed to do just that: implement basic and common tasks, so you don&#8217;t have to write a single line of code.</p>
<p>Now let&#8217;s start with the model definition. Suppose I have the following model class:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Rule<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    rule = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    description = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">400</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">description</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span></pre></div></div>

<p>First of all I need to import generic view functions from the Django libraries, here&#8217;s what you need to add to your urls.py file:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">views</span>.<span style="color: black;">generic</span> <span style="color: #ff7700;font-weight:bold;">import</span> list_detail, create_update</pre></div></div>

<p><em>list_detail</em> provides two methods that are used to represent data:</p>
<ul>
<li><strong>object_list</strong> &#8211; to display a list of objects</li>
<li><strong>object_detail</strong> &#8211; to display details about any given object</li>
</ul>
<p><em>create_update</em> provides three methods to manipulate data:</p>
<ul>
<li><strong>create_object</strong> &#8211; creates new object</li>
<li><strong>update_object</strong> &#8211; modifies existing object</li>
<li><strong>delete_object</strong> &#8211; deletes any given object</li>
</ul>
<p>I then need to define a <em>queryset</em> dictionary that contains a list of objects which generic views will operate upon. Add this to your <em>urls.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">rule_info = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'queryset'</span>: Rule.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'template_name'</span>: <span style="color: #483d8b;">'display_rule.html'</span>,
<span style="color: black;">&#125;</span></pre></div></div>

<p>The only required field here is the queryset, others are optional. You don&#8217;t even need to define a template name as Django will automatically generate one. If you omit <em>template_name</em>, it will attempt to load a template <em>&lt;app_label&gt;/&lt;model_name&gt;_form.html</em>.</p>
<p>Right, now I&#8217;ll do the display bit first. Add the following URL patterns (as you might already guessed it, to ulrs.py file):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^rule/$'</span>, list_detail.<span style="color: black;">object_list</span>, rule_info, name=<span style="color: #483d8b;">'rule-displaytop'</span><span style="color: black;">&#41;</span>,
url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^rule/(?P&lt;object_id&gt;<span style="color: #000099; font-weight: bold;">\d</span>+)/, list_detail.object_detail, rule_info, name='</span>rule-display<span style="color: #483d8b;">'),</span></pre></div></div>

<p>As you can see, I instruct urls.py to call generic views <em>list_detail.object_list</em> and <em>list_detail.object_list</em> and also pass my <em>rule_info</em> dictionary as an argument. The dictionary contains a list of all objects that interest me. List view would simply pass the list on to the template, and the detailed view would select an individual object and pass it on to the template. Therefore the detailed view also expects <em>object_id</em> (which must be a primary key), so that it can find the required object.</p>
<p>When views call templates to display the information, they pass object list as <em>object_list</em> template variable and an instance of any object as <em>object</em>. You can override this by adding <em>template_object_name</em> to the dictionary and setting it to any name you like. If you set it to <em>foo</em>, the names would become: <em>foo_list</em> and <em>foo</em>. For now I leave it as it is. Below is my display_rule.html template:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{% extends &quot;base.html&quot; %}
{% block contents %}
  {% if object %}
    &lt;h1&gt;Rules details&lt;/h1&gt;
    &lt;ul&gt;
      &lt;li&gt;ID: {{ object.id }}&lt;/li&gt;
      &lt;li&gt;Description: {{ object.description }}&lt;/li&gt;
      &lt;li&gt;Rule text:
          {{ object.rule }}
      &lt;/li&gt;
    &lt;/ul&gt;
    ( &lt;a href=&quot;{% url rule-modify object.id %}&quot;&gt;modify&lt;/a&gt; |
      &lt;a href=&quot;{% url rule-delete object.id %}&quot;&gt;delete&lt;/a&gt; )
  {% else %}
    &lt;h1&gt;List of all Rules&lt;/h1&gt;
    {% if object_list %}
      &lt;ul&gt;
        {% for rule in object_list %}
          &lt;li&gt;{{ rule.description }} ( &lt;a href=&quot;{% url rule-display rule.id %}&quot;&gt;details&lt;/a&gt; |
                                       &lt;a href=&quot;{% url rule-modify rule.id %}&quot;&gt;modify&lt;/a&gt; |
                                       &lt;a href=&quot;{% url rule-delete rule.id %}&quot;&gt;delete&lt;/a&gt; )&lt;/li&gt;
        {% endfor %}
      &lt;/ul&gt;
    {% else %}
      No rules defined yet.
    {% endif %}
  &lt;h3&gt;&lt;a href=&quot;{% url rule-add %}&quot;&gt;Add new rule&lt;/a&gt;&lt;/h3&gt;
{% endif %}
{% endblock %}</pre></div></div>

<p>As you can see the logic is somewhat simple:</p>
<ul>
<li>Check if the template object <em>object</em> is defined</li>
<li>If it is, that means we&#8217;ve been called from the detailed object view method and so need to display object information</li>
<li>If not, then we&#8217;ve been called from the object list view, so we need to iterate through the list</li>
<li>Check if the list is not empty (very important! as the lists tend to come empty in some cases&#8230;)</li>
<li>If the list is not empty &#8211; display all objects and provide links to basic functions</li>
</ul>
<p>Let&#8217;s start with adding new objects. You will need to define <em>get_absolute_url</em> method for your class (this will be used to get a URL where to redirect the user once the form has been submitted) and also define Model Form class (not required, strictly speaking, but I like to define it, so that I don&#8217;t need to if I decide to change form behaviour). This is how these two classes are going to look now:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Rule<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    rule = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    description = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">400</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">description</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span>
&nbsp;
    @models.<span style="color: black;">permalink</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> get_absolute_url<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'rule-display'</span>, <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'object_id'</span>: <span style="color: #008000;">self</span>.<span style="color: #008000;">id</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RuleForm<span style="color: black;">&#40;</span>ModelForm<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        model = Rule</pre></div></div>

<p>For now ignore the &#8220;magic&#8221; with URL names, these are used to help reverse URL resolver, and I&#8217;m going to talk about these in other posts. Just note that <em>get_absolute_url</em> returns generated URL with <em>object_id</em> embedded in it.</p>
<p><em>add.html</em> template is very simplistic and contains only few lines:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;.&quot; method=&quot;POST&quot;&gt;
{{ form.as_p }}
&lt;input type=&quot;submit&quot; value=&quot;Add&quot; /&gt;
&lt;/form&gt;</pre></div></div>

<p>This is enough to display all fields and a submit button. If you need anything fancier than that, you&#8217;d need to alter the form to suit your needs.</p>
<p>Next, I&#8217;ll need to define a dictionary with settings, which will be passed to generic create and modify views, just like a queryset dictionary was used with display views:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">rule_form = <span style="color: black;">&#123;</span>
  <span style="color: #483d8b;">'form_class'</span>: RuleForm,
  <span style="color: #483d8b;">'template_name'</span>: <span style="color: #483d8b;">'add.html'</span>,
<span style="color: black;">&#125;</span></pre></div></div>

<p>And finally some URL patterns to handle the requests:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^rule/(?P&lt;object_id&gt;<span style="color: #000099; font-weight: bold;">\d</span>+)/modify/$'</span>, create_update.<span style="color: black;">update_object</span>, rule_form, name=<span style="color: #483d8b;">'rule-modify'</span><span style="color: black;">&#41;</span>,
url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^rule/add/$'</span>, create_update.<span style="color: black;">create_object</span>, rule_form, name=<span style="color: #483d8b;">'rule-add'</span><span style="color: black;">&#41;</span>,</pre></div></div>

<p>As you can see, these two are very similar with one exception, <em>update_object</em> requires <em>object_id</em>, which is embedded in the URL, so you don&#8217;t need to worry about it.<br />
Finally let&#8217;s sort out the delete object function. This function requires three fields: model name, post delete redirect and also a confirmation template name. All defined in a dictionary form:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">rule_delete = <span style="color: black;">&#123;</span>
  <span style="color: #483d8b;">'model'</span>: Rule,
  <span style="color: #483d8b;">'post_delete_redirect'</span>: <span style="color: #483d8b;">'../..'</span>,
  <span style="color: #483d8b;">'template_name'</span>: <span style="color: #483d8b;">'delete_confirm_rule.html'</span>,
<span style="color: black;">&#125;</span></pre></div></div>

<p>You also need additional URL pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^rule/(?P&lt;object_id&gt;<span style="color: #000099; font-weight: bold;">\d</span>+)/delete/$, create_update.delete_object, rule_delete, name='</span>rule-delete<span style="color: #483d8b;">'),</span></pre></div></div>

<p>Confirmation template just asks for a user confirmation and redirects to the same URL, however the method is POST, so the view knows it needs to delete the object rather than display the confirmation page (which happens if the request is GET):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;.&quot;&gt;
&lt;p&gt;Are you sure?&lt;/p&gt;
&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;</pre></div></div>

<p>This is it, now you know how to perform some of the common tasks without writing any views.</p>


<p>Related posts:<ol><li><a href='http://www.grenadepod.com/2009/11/13/query-data-from-django-site-with-jquery/' rel='bookmark' title='Permanent Link: Query data from Django site with jQuery'>Query data from Django site with jQuery</a></li>
<li><a href='http://www.grenadepod.com/2009/12/13/python-web-crawler/' rel='bookmark' title='Permanent Link: Python web crawler'>Python web crawler</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
