Andrey Zhukov's Tech Blog

NGINX for static files for dev python server

| Comments

When you work on the backend part of django or flask project and there are many static files, sometimes the development server becomes slow. In this case it’s possible to use nginx as reverse proxy to serve static. I’m using nginx in docker and the configuration is quite simple.

Put in some directory Dockerfile and default.conf.tmpl.

Dockerfile

1
2
3
4
5
FROM nginx:1.9
VOLUME /static
COPY default.conf.tmpl /etc/nginx/conf.d/default.conf.tmpl
EXPOSE 9000 
CMD envsubst '$APP_IP $APP_PORT' < /etc/nginx/conf.d/default.conf.tmpl > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'

default.conf.tmpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
    listen 9000;
    charset utf-8;

    location /site_media {
        alias /static;
    }

    location / {
        proxy_pass http://${APP_IP}:${APP_PORT};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Build image with docker build -t dev-nginx . command.

To run it:

1
docker run --rm -it -v `pwd`/static:/static -p 9000:9000 -e APP_IP=<your ip from ifconfig> -e APP_PORT=8000 dev-nginx

Then you can access your development server though http://<localhost|docker-machine-ip>:9000.

strftime for datetime before 1900 year

| Comments

Recently I’ve got an error for birthdays before 1900 year

1
ValueError: year=1890 is before 1900; the datetime strftime() methods require year >= 1900

for this code:

1
2
import datetime
datetime.date(1890,7,1).strftime('%d.%m.%Y')

And it’s described in the documentation

The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation.
The exact range of years for which strftime() works also varies across platforms. Regardless of platform, years before 1900 cannot be used.

One of the ways to solve this:

1
2
birthday = datetime.date(1890,7,1)
'{0.day:02d}.{0.month:02d}.{0.year:4d}'.format(birthday)

Test django view with cookies

| Comments

To test some view which use cookies:

1
2
3
4
5
6
7
8
9
10
from Cookie import SimpleCookie
from django import test

class SomeTest(test.TestCase):

  def test_some_view(self):
      self.client.cookies = SimpleCookie({'test_cookie': 'test_value'})
      response = self.client.get('/some-url/')

      self.assertEqual(response.client.cookies['test_cookie'].value, 'test_value')

My current tools

| Comments

MacBook Air 13” instead of iMac 24” 2007
It’s faster and provides me mobility, but I’m lacking in screen size, I plan to solve this with external monitor.

Sublime Text3 with next packages:

  • package control
  • djaneiro (django support)
  • django-docsearch
  • git (checkout branch python3)
  • jsformat
  • modific (highlighting lines changed since the last commit)
  • sidebar enchancement
  • sublimelinter (checkout sublime-text-3 branch)
  • theme phoenix

Before this I used PyCharm, it’s very good, but slower.

SourceTree - GUI for Git and Mercurial
I prefer to use command line to add, commit, push and this app for history. It’s a little slow sometimes.

OmniFocus for GTD
Usually I use just two perspectives Business and Personal grouped by due date. It’s useful to have filled estimate field, it gives me possibility to better plan my day.

Toggl for time tracking
Good reports, desktop and iphone apps, I know how I spent my time.

nvALT for notes
Simple and fast.

Alfred
It looks like a spotlight but much better. I’m using mostly for clipboard history and to:

  • open apps
  • paste notes to nvALT
  • find files
  • calculate
  • search contacts
  • change status in Skype and Messages simultaneously

Installing Django on Ubuntu memo

| Comments

Here are a few notes and links after moving to the new Ubuntu server on linode.com.

Some steps for security: My First 5 Minutes On A Server

Install packages:

1
2
3
4
apt-get install apache2 libapache2-mod-wsgi
apt-get install postgresql postgresql-server-dev-9.1 python-dev
apt-get install mysql-server mysql-common mysql-client libmysqlclient-dev
apt-get install git git-core

Setup virtualenv:

1
2
3
4
apt-get install python-setuptools
apt-get install python-pip
pip install virtualenv
virtualenv --no-site-packages /path/to/venv

Install PIL in virtualenv:

1
2
3
4
5
apt-get install libjpeg libjpeg-dev libfreetype6 libfreetype6-dev zlib1g-dev
ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/
pip install PIL

How to update Django user profile from ForeignKey to OneToOneField

| Comments

In one of my projects for the user profile model was used ForeignKey field.

1
2
class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)

This leads to a lot of unnecessary queries when you want to access to related data, like:

1
2
3
{% for item in items %}
  {{ item.user.get_profile.company }}
{% endfor %}

StackOverflow Charts

| Comments

A month ago I began to participate in StackOverflow process. So I was interested to collect some statistics data and find where am I in this game. StackOverflow has a good API with limits for 300 requests/days for anonymous user and 10 000 requests/day for authorized. I created a simple bot on python, collected data to MongoDb and built these charts. With 10 000 requests and page size 100 I got users from highest reputation (524k) to 269. So I built these charts starting from reputation of 300.

The number of users with reputation > 300 is 96 654. And total number of users is about 1 375 000. So the percent of active users is about 7%.

A simple CRUD app with Django and Mongoengine

| Comments

There are several possibilities to use MongoDB in Django:

Django MongoDB uses django-nonrel, which is a fork based on Django 1.3.
I don’t like this idea, because now Django 1.5 is ready to out. Beetween Mongoengine and MongoKit, I like more Mongoengine. There are several comparative articles:

So I created a simple CRUD app using Mongoengine.