<?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>Taras Mankovski Blog &#187; Python</title>
	<atom:link href="http://taras.cc/index.php/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://taras.cc</link>
	<description>Building Beecoop and generaly making developer&#039;s lives easier and more productive.</description>
	<lastBuildDate>Wed, 25 Nov 2009 03:41:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Testing REST API with restclient</title>
		<link>http://taras.cc/index.php/2009/10/07/testing-rest-api-with-restclient/</link>
		<comments>http://taras.cc/index.php/2009/10/07/testing-rest-api-with-restclient/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 20:58:34 +0000</pubDate>
		<dc:creator>Taras</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://taras.cc/?p=166</guid>
		<description><![CDATA[Here are some code snippets from the test that I wrote for testing the REST API for one of our projects.
I am using appengine-rest-server as API provider on the server.
Do not copy these example verbatim, I&#8217;m only showing them as examples.
Each of this methods is defined inside of a class
Imports

from nose.tools import assert_equals, assert_true, assert_false
from [...]]]></description>
			<content:encoded><![CDATA[<p>Here are some code snippets from the test that I wrote for testing the REST API for one of our projects.<span id="more-166"></span><br />
I am using <a href="http://code.google.com/p/appengine-rest-server/">appengine-rest-server</a> as API provider on the server.</p>
<p>Do not copy these example verbatim, I&#8217;m only showing them as examples.<br />
Each of this methods is defined inside of a class</p>
<h4>Imports</h4>
<pre><code>
from nose.tools import assert_equals, assert_true, assert_false
from restclient import GET, POST, PUT, DELETE
from lxml import etree, objectify
from StringIO import StringIO
</pre>
<p></code></p>
<h4>Verifying XML against XML Schema</h4>
<pre><code>
    def verify_against_schema(self, xml, schema):
        '''
        Check if data is valid for provided schema

        xml data to verify
        schema is url to the xml schema
        '''
        schema = etree.XMLSchema(etree.parse(StringIO(schema)))
        parser = etree.XMLParser(schema=schema)
        return etree.parse(StringIO(xml), parser=parser)

    def test_review_schema(self):
        'Vacation :: Verifying review schema'

        xml = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <service>4</service>
            <cleanliness>4</cleanliness>
            <reason>Wedding</reason>
            <value>4</value>
            <resort>34r3423f3rf32f3f</resort>
            <ta_id>334323443</ta_id>
            <rooms>300</rooms>
            <recommend>true</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''

        verified = self.verify_against_schema(xml, GET(self.url('/api/rest/metadata/Review')))
        assert_true(verified)

</code></pre>
<h4>Creating an item</h4>
<pre><code>

    def create_review(self, resort, full=False):
        'Create review for testing purposes'

        xml = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <service>4</service>
            <cleanliness>4</cleanliness>
            <reason>Wedding</reason>
            <value>4</value>
            <resort>%s</resort>
            <ta_id>334323443</ta_id>
            <rooms>300</rooms>
            <recommend>1</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''%resort

        if full:
            url = '/api/rest/Review?type=full'
        else:
            url = '/api/rest/Review'     

        return POST(self.url(url), body=xml, async=False, resp=True)
</code></pre>
<h4>Updating an item</h4>
<pre><code>
   def test_updating_review(self):
        'Vacation :: Updating review'

        response, resort = self.create_resort()
        response, review_id = self.create_review(resort)

        xml = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <service>5</service>
            <cleanliness>5</cleanliness>
            <reason>Wedding</reason>
            <value>5</value>
            <resort>%s</resort>
            <ta_id>334323443</ta_id>
            <rooms>520</rooms>
            <recommend>true</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''%resort
        response, content = PUT(self.url('/api/rest/Review/%s?type=full'%review_id), body=xml, resp=True, async=False )

        expected = '''<?xml version="1.0" encoding="utf-8"?>
        <Review>
            <key>%s</key>
            <service>5</service>
            <cleanliness>5</cleanliness>
            <reason>Wedding</reason>
            <value>5</value>
            <resort>%s</resort>
            <ta_id>334323443</ta_id>
            <rooms>520</rooms>
            <recommend>true</recommend>
            <date>2009-10-07</date>
            <type>Family</type>
            <location>4</location>
        </Review>
        '''%(review_id,resort)

        expected = objectify.fromstring(expected)
        expected = etree.tostring(expected)

        result = objectify.fromstring(content)
        result = etree.tostring(result)

        assert_equals(expected, result)
</code></pre>
<p>I hope you find these helpful</p>
]]></content:encoded>
			<wfw:commentRss>http://taras.cc/index.php/2009/10/07/testing-rest-api-with-restclient/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modifying Trac Project List with WSGI</title>
		<link>http://taras.cc/index.php/2009/09/30/modifying-trac-project-list-with-wsgi/</link>
		<comments>http://taras.cc/index.php/2009/09/30/modifying-trac-project-list-with-wsgi/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 15:30:44 +0000</pubDate>
		<dc:creator>Taras</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://taras.cc/?p=126</guid>
		<description><![CDATA[Here is my log of how I modified Project List on http://beecoop.com/projecs.
Terminology:
Trac calls this: Environment Index
from here: http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac
I got instructions to make following modification in my /var/lib/trac/apache/apache.wsgi


import trac.web.main
import sys
sys.stdout = sys.stderr

_application = trac.web.main.dispatch_request

def application(environ, start_response):
        environ['trac.env_parent_dir'] = '/var/lib/trac/sites'
        environ['trac.env_index_template'] = '/var/lib/trac/global/templates/projects.html'
 [...]]]></description>
			<content:encoded><![CDATA[<p>Here is my log of how I modified Project List on http://beecoop.com/projecs.<span id="more-126"></span><br />
Terminology:<br />
Trac calls this: Environment Index</p>
<p>from here: http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac</p>
<p>I got instructions to make following modification in my /var/lib/trac/apache/apache.wsgi</p>
<pre><code>

import trac.web.main
import sys
sys.stdout = sys.stderr

_application = trac.web.main.dispatch_request

def application(environ, start_response):
        environ['trac.env_parent_dir'] = '/var/lib/trac/sites'
        environ['trac.env_index_template'] = '/var/lib/trac/global/templates/projects.html'
        return _application(environ, start_response)

</code></pre>
<p><strong>environ['trac.env_index_template']</strong> indicates where the template for this page is located.</p>
<p>the default template is here: http://trac.edgewall.org/browser/trunk/trac/templates/index.html</p>
<p>I just copied it from there.</p>
<p>Now, I don&#8217;t want to show private projects on /projects page, so I modified my /var/lib/trac/apache/apache.wsgi in the following way.</p>
<pre><code>

import sys
import pkg_resources

from genshi.core import Markup
from genshi.builder import Fragment, tag
from genshi.output import DocType
from genshi.template import TemplateLoader

import trac.web.main
from trac import __version__ as TRAC_VERSION
from trac.util.datefmt import format_datetime, http_date, localtz, timezone
from trac.util.text import to_unicode

from trac.web.api import *
from trac import perm

sys.stdout = sys.stderr

def projects(environ, start_response):
	req = Request(environ, start_response)
	loadpaths = [pkg_resources.resource_filename('trac', 'templates')]
	template = environ['trac.env_index_template']

	data = {'trac': {'version': TRAC_VERSION, 'time': format_datetime()}, 'req': req}
	if req.environ.get('trac.template_vars'):
		for pair in req.environ['trac.template_vars'].split(','):
			key, val = pair.split('=')
			data[key] = val
			if use_clearsilver:
				req.hdf[key] = val
	try:
		href = Href(req.base_path)
		projects = []
		for env_name, env_path in trac.web.main.get_environments(environ).items():
			try:
				env = trac.web.main.open_environment(env_path,
									   use_cache=not environ['wsgi.run_once'])

				# check if project has anonymous access
				permissions = perm.PermissionSystem(env)

				if permissions.get_user_permissions('anonymous'):
					proj = {
						'name': env.project_name,
						'description': env.project_description,
						'href': href(env_name)
					}
					projects.append(proj)
			except Exception, e:
				proj = {'name': env_name, 'description': to_unicode(e)}

		projects.sort(lambda x, y: cmp(x['name'].lower(), y['name'].lower()))

		data['projects'] = projects

		loader = TemplateLoader(loadpaths, variable_lookup='lenient')
		tmpl = loader.load(template)
		stream = tmpl.generate(**data)
		output = stream.render('xhtml', doctype=DocType.XHTML_STRICT)
		req.send(output, 'text/html')

	except RequestDone:
		pass

_trac = trac.web.main.dispatch_request

def application(environ, start_response):
	environ['trac.env_parent_dir'] = '/var/lib/trac/sites'
	environ['trac.env_index_template'] = '/var/lib/trac/global/templates/projects.html'
	if environ['REQUEST_URI'] != '/projects/':
		return _trac(environ, start_response)
	else:
		return projects(environ, start_response)

</code></pre>
<p>Next, is going to be shared sessions across all projects and modifying this view to show public projects and projects that I have access to.</p>
]]></content:encoded>
			<wfw:commentRss>http://taras.cc/index.php/2009/09/30/modifying-trac-project-list-with-wsgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
