Skip to main content

Posts

Showing posts with the label python

Running tasks with Celery on Heroku guide

An example project and a basic guide showing how to run Django/Celery on Heroku. Basic requirements First of all, let's actually set up a typical Django project for this. We would need virtualenvwrapper   for that. One could use any other particular method. I prefer this one. $ cd dev $ mkvirtualenv dch ( dch ) $ pip install django ( dch ) $ django-admin startproject djheroku ( dch ) $ cd djheroku # Make sure is working: ( dch ) $ ./manage.py runserver From now I will consider working on a terminal with this (dch) environment on. Heroku hosting setup We would need our project set up for heroku python server. The docs live HERE , as for moment of this guide writing. One would need to follow and setup a basic heroku project. I will not stop here rewriting official guide as it is good enough. Installing celery Assuming we have a basic django dyno at heroku here we will continue. Now let's install Celery and add it to our requirements list (as we had just sta...

POP3 Mock (Fake) server using python script

Having a need in POP3 server for my debugging purposes I have used this script. Letting it to be here in case of anyone would need to do something similar. Usage is: $ python pypopper.py 110 email_file.eml " " " pypopper: a file-based pop3 serve r Useage :     python pypopper.py <port> <path_to_message_file > " " " import logging import os import socket import sys import traceback logging . basicConfig ( format = " %(name)s %(levelname)s - %(message)s " ) log = logging . getLogger ( " pypopper " ) log . setLevel ( logging . INFO ) class ChatterboxConnection ( object ) : END = " \r \n " def __init__ ( self , conn ) : self . conn = conn def __getattr__ ( self , name ) : return getattr ( self . conn , name ) def sendall ( self , data , END = END ) : if len ( data ) < 50 : log . debug ( " send: %r " , data )    ...

Django adding custom widget to Django Admin

Sometimes you need to get out of standard behaviour of your Django admin. It means expanding its functionality with custom things. In our case we have a model that we want to add a custom html element. Button that must load something via AJAX from another place to be exact. We have a typical Django polls application for simplicity. Here is its structure: First of all we need to create a structure. So here comes our model: # models.py from django.db import models class Poll (models.Model): question = models.CharField(max_length= 200 ) pub_date = models.DateTimeField( 'date published' ) We have to have our django application admin configuration to display that model: # admin.py from django.contrib import admin from polls.models import Poll admin.site.register(Poll) It will look in Django admin like so: You can see polls available here in admin. You can do all the typical things with Polls model. Add/Delete/Edit... Using standard D...

Virtualenv for Django

One of main features and troubles for Django projects is the Django version you are using. Example for this would be the occasion of starting project for Django 1.4.x version. Trying to run it under Django 1.7.x would lead to various troubles, counting template changes, misconfiguration errors and so on. Solution is to use virtual environment. The tool for this is called Virtualenv . It is a wrapper for a python interpreter. It enables you to have multiple versions of python packages that run independently. The Virtualenv creates you a standalone environment. Basically it copies and keeps a system Python of yours in a specified directory. All the python packages will be installed there. (In case you have not forgot to activate it first, of course) Lets get started then. We need Virtualenv itself for the first. There are many ways to install it depending on your system . Most obvious to use pip. Your installation command will look something like that: pip install virtualenv Thi...

Implementing a Multiple (Radio) Select + “Other” widget in Django

I have had a task to implement Radio Select field with "Other" choice field in a Django Model Form. Here is my implementation in case someone would benefit from it. The idea is to have it all stored to Django's CharField type of data and have a preselected set of fields. We have a model: # models.py class Entry (models.Model): SET_OF_CHOICES = ( ( 'choice1' , 'choice1' ), ( 'choice2' , 'choice2' ), ( 'choice3' , 'choice3' ), ( 'Other' , 'Other Please Specify' ), ) choice = models.CharField(_( "Selected your choice" ), max_length= 250 ) That makes it ready for the set of fields for for our form. We are overriding Model form default field for this type of data (CharField) by intentionally specifying field with the same name. # forms.py from django.forms import ModelForm, ChoiceField, RadioSelect from models import Entry ...