using the for loop counter in python Nov
17
1
0

Even with all the powerful tools Python loops do for you, sometimes you need to grab the good ol' loop counter.

There are a couple of different ways you can do this. The cleanest method that I found so far is to use the enumerate function. You can do something like this:

test = ['a', 'b', 'c']

for counter, value in enumerate(test):
    print counter, value

comments

detect and change html named anchor tags in the url through javascript and jquery Oct
06
1
0

You may already be familiar with using html named anchor tags to auto-scroll the page to a certain section in an html page, which would look something like this:

<a id="button" href="#tag">link</a>

...

<a name="tag">anchor</a>

An alternative use for anchor tags is to change and detect the tag value in the url on the fly in order to initialize certain states with javascript-driven content. This is handy as it lets people bookmark the location and the page will initialize properly even in heavy ajax environments. It's surprisingly easy to interact dynamically with the url anchor tag object.

You can detect the current anchor value of the url in javascript through "location.hash" like this:

if(location.hash == '#tag') {
    // initialize content
    ...
}

You can also dynamically change the anchor tag in the url just by setting "location.hash" like so:

// location.hash = 'value';

$('a#button').click(function(event) {
    event.preventDefault();

    // set the anchor value
    // I like to strip the hash out of the href value; you can use your own method of choice to determine the anchor.
    location.hash = $(this).attr('href').replace('#', '');

    // do something important
    ...
});

These examples use JQuery to simplify the code.

comments

restricting views to staff users in django Sep
02
1
0

Hopefully you've already found the login_required decorator for Django views, which make it incredibly simple to integrate authentication into the site.

One thing that's not documented well is there's another decorator @staff_member_required. This works the same way @login_required does except it restricts access to users who are both authenticated and has staff status set in their settings.

This comes in handy when extending the admin or writing custom moderation applications.

You can do something similar to:

from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib.admin.views.decorators import staff_member_required


@staff_member_required
def view(request):

    ...

    return render_to_response('template.html', context_instance=RequestContext(request))

comments

setting a default virtual host in nginx Aug
20
1
0

If there are multiple conf files included in the sites-enabled directory you can set the Nginx server to default to a particular site by adding a listen directive:

server {
listen 80 default;
... }

comments

install and remove .deb packages in ubuntu Aug
20
1
0

You can use the <dpkg> package manager to install downloaded .deb packages on Ubuntu and other debian-based systems.

To install a package pass in the -i flag:

sudo dpkg -i <package.deb>

You can list installed packages with the -l and an optional pattern:

dpkg -l <optional pattern>

This list can be quite long, so it might be helpful to redirect the output into a file.

dpkg -l > dpkg.txt

Use -r to remove a package.

sudo dpkg -r <package>

-r uninstalls the package, but it leaves any configuration files in place so if you ever install that package again it'll already be initialized.

To remove all associated files use -P to purge all the data for the desired package.

sudo dpkg -P <package>

comments

getting the browser to detect rss and atom feeds Aug
17
1
0

When writing sections that has periodic updates, like a blog, it's commonly requested to create an rss or an atom syndication feed for the section.

I develop with Django, and fortunately it comes with a great and easy to use syndication feed framework out-of-the-box to automate much of this process. However, the documentation doesn't talk about how to get browsers to automatically detect that a feed is available so that the syndication feed icon automatically shows up in the user's browser.

The technical term for this is "autodiscovery", and not surprisingly it's easy to implement.

All that needs to be done is to add something similar to this into the <head>  of your HTML page for RSS:

<link href="http://www.example.com/blog.rss" rel="alternate" type="application/rss+xml" title="<title>"/>

or for ATOM:

<link href="http://www.example.com/blog.atom" rel="alternate" type="application/atom+xml" title="<title>"/>

comments

password protect nginx directory/location with htpasswd Jul
12
1
0

In apache you can password protect a directory with an .htaccess file. It's a little different in nginx, although I think it's more elegant.

All you need to do is add the following two entries in the location listing in the server conf settings.

location = / {
auth_basic "Restricted";
auth_basic_user_file /path/to/htpasswd; ...
}

and then run htpasswd in the proper location:

htpasswd -c -b htpasswd <user> <pass>

comments

dynamically create an xml file in django Jun
29
1
0

Sometimes when communicating to external systems like Flash I am required to dynamically create an XML file. You can utilise the template framework to do the heavy lifting.

It's worth noting that if you need to create syndication feeds you should use the syndication framework, and if you want to create a site map you should use the sitemap framework.

With that being said, lets say you have a model that looks something like this (I'll use a blog post concept for the demo):

# models.py

class Post(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    publish = models.BooleanField(default=True)
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    edited = models.CharField(max_length=255, blank=True)
    tags = models.ManyToManyField('Tag')
    body = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        post_date = self.date_created
        return_vars = {
            'year': post_date.year,
            'month': post_date.month,
            'day': post_date.day,
            'slug': self.slug,
        }

        return('blog_post', (), return_vars)

    ....

    @classmethod
    def get_posts(self, limit=None):
        if limit:
            return Post.objects.filter(publish=True).order_by('-date_created')[0:limit]
        else:
            return Post.objects.filter(publish=True).order_by('-date_created')

You can link a reference to an xml file in urls.py:

# urls.py

urlpatterns = patterns('project.quotes.views',
    ....
    url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[\w-]+)/, 'post', name='blog_post'),
    url(r'^xml/latest.xml, 'xml_latest', name='blog_xml_latest'),
    ....
)

You can load and render the template which is in the xml format, and return it with mimetype="text/xml". Your view can be something like:

# views.py

template_vars = {...}

def xml_latest(request):
    """
    returns an XML of the most latest posts
    """
    template_vars['posts'] = Post.get_posts(30)
    template_vars['site'] = Site.objects.get_current()

    t = loader.get_template('blog/xml/posts.xml')
    c = Context(template_vars)

    return HttpResponse(t.render(c), mimetype="text/xml")

and your template can be something like:

# posts.xml

<?xml version="1.0" encoding="UTF-8"?>
<posts>
  {% for post in posts %}
  <post>
    <date>{{ post.date_created|date:"M j, Y" }}</date>
    {{ if post.edited }}
    <edited>{{ post.edited|date:"M j, Y" }}</edited>
    {{ endif }}
    <title>{{ post.title }}</title>
    <body>{{ post.body|striptags }}</body>
    <tags>
        {% for tag in post.tags %}
        <tag>
            <name>{{ tag.name }}</name>
            <url>http://{{ site.domain }}{{ tag.get_absolute_url }}</url>
        </tag>
        {% endfor %}
    </tags>
    <absolute_url>http://{{ site.domain }}{{ post.get_absolute_url }}</absolute_url>
  </post>
  {% endfor %}
</posts>

And that's that. There's probably a better more dynamic way to generate the XML file, but this works for me when I need something quick and dirty.

Please let me know if you have any suggestions on how to improve this process.

comments

postgresql cheatsheet Jun
25
1
0

I'm transitioning my databases from MySQL to PostgreSQL. Overall it's been an easy transition. One of the main differences that I had to adjust to is that by default postgres uses the unix user accounts to handle authentication into the CLI. This can be changed in the settings, and it also means that you might want to set the password for the postgres unix user.

Also it's good to note that postgres automatically translates non-strings to lowercase. This is an issue if you do things like camel casing your tables and columns, so it's best to avoid doing so if you're into this habit.

I do find the command lines to be more streamlined and the config to be more straight forward, but that's really just a personal preference.

Here's a rundown of the commands I often use. I'm posting them here for my convenience, but perhaps someone else might also find these useful.

# login
psql -U <user>
psql -U <user> -d <database>
psql -U <user> -h <host> -d <database>


# user management
CREATE USER <user> WITH PASSWORD '<password>'
ALTER USER <user> WITH PASSWORD '<password>'
DROP USER <user>

createuser -D -A -P <user>
psql -U <admin> -c "CREATE USER <user> WITH PASSWORD '<password>'" -d template1


# database
CREATE DATABASE <database>
CREATE DATABASE <database> OWNER <user>
DROP DATABASE <database>

GRANT ALL PRIVILEGES ON DATABASE <database> TO <user>
ALTER TABLE <table> OWNER TO <user>

createdb -O <user> <database>


# install
apt-get install posgresql
apt-get install python-psycopg2

or

apt-get install libpq-dev
pip install psycopg2


# configuration
# pg_hba.conf
host all all 0.0.0.0/0 md5 # open up all connections, used for dev

# postgresql.conf
listen_addresses = '*' # listen for addresses


# dump and load data
pg_dump <database> > <file.sql>
psql <database> < <file.sql>

# from http://developer.postgresql.org/pgdocs/postgres/app-pgdump.html
To dump a database called mydb into a SQL-script file:

$ pg_dump mydb > db.sql

To reload such a script into a (freshly created) database named newdb:

$ psql -d newdb -f db.sql

To dump a database into a custom-format archive file:

$ pg_dump -Fc mydb > db.dump

To reload an archive file into a (freshly created) database named newdb:

$ pg_restore -d newdb db.dump

To dump a single table named mytab:

$ pg_dump -t mytab mydb > db.sql

To dump all tables whose names start with emp in the detroit schema,
except for the table named employee_log:

$ pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb > db.sql

To dump all schemas whose names start with east or west and end in gsm,
excluding any schemas whose names contain the word test:

$ pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb > db.sql

The same, using regular expression notation to consolidate the switches:

$ pg_dump -n '(east|west)*gsm' -N '*test*' mydb > db.sql

To dump all database objects except for tables whose names begin with ts_:

$ pg_dump -T 'ts_*' mydb > db.sql

To specify an upper-case or mixed-case name in -t and related switches,
you need to double-quote the name; else it will be folded to lower case (see Patterns).
But double quotes are special to the shell, so in turn they must be quoted.
Thus, to dump a single table with a mixed-case name, you need something like

$ pg_dump -t '"MixedCaseName"' mydb > mytab.sql


# export csv
psql -U <user> <database>
\f ','
\a
\t
\o file.csv
SELECT <statement>
\o
\q

comments

random numbers in javascript Jun
21
1
0

This is a stepwise procedure to generate a random number within a given range in JavaScript.

var number = Math.random(); // generate a random decimal between 0 and 1
number *= 10; // scale the number so that it falls between the range 0 and 10 (or any number you'd like to make the max limit)
number = Math.ceil(number); // round the result up to the nearest whole number

You can combine this into one line to easily create a random number generator:

var number = Math.ceil(10 * Math.random());

comments