Author Archives: Niklas - Page 35

Changing date format in Trac using mod_wsgi

Trac is a marvelous tool to use for us developers. Unfortunately there are some quirks to it. One is that you can not change the data format in a “normal” way eq. through the admin panel. To change it you have to add a bit of code to the trac.wsgi:

environ['trac.locale'] = 'sv_SE.UTF-8' # Any valid locale will do

Be sure to add it after the “def application” row like this:

...
import os

def application(environ, start_request):
    environ['trac.locale'] = 'sv_SE.UTF-8'
    if not 'trac.env_parent_dir' in environ:
        environ.setdefault('trac.env_path', '/var/trac/my_project')
...

After you have made the change be sure to restart the webserver!
For Apache on Debian:

/etc/init.d/apache2 restart

Tested on Debian Wheezy and Trac v0.12.3

Setup Trac project on Debian Wheezy with Apache using the mod_wsgi and Basic Authentication

I had a lot of trouble understanding the Trac install instruction on the Trac project homepage. Maybe I’m getting old 🙂 Anyhow, I decided to create this step by step tutorial so that I have something easy to return too the next time I need to setup a new Trac project. In this tutorial I assume that all the necessary programs (Apache (with mod_wsgi), Trac and SQlite) are already installed

So lets start off by creating a folder to put our project in:

mkdir /var/trac/my_project

I place my trac instances in /var/trac/ but you can use almost any location

Now lets use trac-admin to create the project

trac-admin /var/trac/my_project initenv
trac-admin /var/trac/my_project deploy /tmp/deploy

The project is now created and deployed, but I have deployed it to /tmp – strange? I certainly think so but it’s apparently the preferred way. Somehow trac-admin can not deploy the necessary script into you project folder. You have to copy them there yourself. Editors note: Why can’t this be done automatically in the creation of the project

mv /tmp/deploy/* /var/trac/my_project/

This now moves the created scripts in htdocs and cgi-bin folders to your project

Now we need to set the correct ownership (this is not my strong suite so please report any errors) of the project files:

chown -R www-data:www-data /var/trac/my_project

Now it’s time to create a password file for the project since I normally only use Basic Authentication for my Trac projects:

htpasswd -c /var/trac/my_project/.trac.htpasswd niklas

This creates the user niklas inside the password files (you will be promted for a password)

To add more users just drop the -c option like this

htpasswd /var/trac/my_project/.trac.htpasswd another_user

To tighten up the security somewhat we set owner and permission on the password file like this:

chmod 640 /var/trac/my_project/.trac.htpasswd
chown root:www-data /var/trac/my_project/.trac.htpasswd

Now lets add these users to the trac project also. First the admin, niklas

trac-admin /var/trac/my_project permission add niklas TRAC_ADMIN

and then a user with basic privileges (create tickets, read wiki, timeline, milestones and such):

trac-admin /var/trac/my_project permission add anotheruser authenticated

We are now finally done with the project files. Time to move on to the Apache configuration. For this I create a file in the conf.d folder of the Apache installation like this:

vim /etc/apache2/conf.d/my_project

In this file I put the following:

<Directory /var/trac/my_project/cgi-bin/trac.wsgi>
  WSGIApplicationGroup %{GLOBAL}
  Order deny,allow
  Allow from all
</Directory>

<VirtualHost *>
  WSGIScriptAlias /trac/my_project /var/trac/my_project/cgi-bin/trac.wsgi
  <Location '/trac/my_project'>
    AuthType Basic
    AuthName "Trac"
    AuthUserFile /var/trac/my_project/.trac.htpasswd
    Require valid-user
  </Location>
</VirtualHost>

Now its finally time to test the new project. Restart Apache

/etc/init.d/apache2 restart

If all goes well you should now be able to find your new Trac project at http://localhost/trac/my_project. You should also be promted for a login when you arrive there

Tested on Debian Wheezy v7.0 with Apache 2 v2.2.22-13 and Trac v0.12.3

Validate elements in any order and any number of times using XSD

Sometimes you just want to validate any number of elements any number of times. There is no intuitive way in XSD to accomplish this so we have to use a trick to get it to work. The solution looks a little like this:

<xs:element name="myElement">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:choice>
          <xs:element name="myId" type="xs:int" />
          <xs:element name="myName" type="xs:string" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

This lets us validate any of the elements in the <choice> (myId and MyName) list any number of times and in any order, so the following will validate:

<myElement>
  <myId>3</myId>
  <myName>Niklas</myName>
</myElement>

and:

<myElement>
  <myName>Niklas</myName>
  <myId>3</myId> 
</myElement>

and:

<myElement>
  <myId>3</myId>
  <myName>Niklas</myName>
  <myName>Anders</myName>
</myElement>

but not:

<myElement>
  <myId>hello</myId>
  <myName>Niklas</myName>
</myElement>

Last one does not validate since ‘hello’ is not a integer.

Tested with xmllint with libxml version 20708