Ubuntu tip: Save a list of installed packages for reinstallation purposes

If you have a nicely running Ubuntu system with all your favourite packages installed, it might not seem like a good idea to start all over or to replace your hard drive. But here’s a tip: To save a list of all packages that are currently installed! This should be a part of any good backup.

apt-cache --installed pkgnames

Save them in a file:

apt-cache --installed pkgnames > installed.packages.lst

To install all packages in a file:

sudo apt-get install `cat installed.packages.lst`
Posted in Ubuntu | Leave a comment

Overriding change_form.html and accessing the object instance

Here’s an example for a custom change_form.html located in app/templates/admin/appname/modelname/change_form.html – and the cool thing is that it accesses the actual instance of the object, which I found to be very convenient and undocumented. If you want it to be more explicit than access through the builtin context variable adminform.form.instance, you can also make your own change_view.

{% extends "admin/change_form.html" %}
 
{% block object-tools %}
  {{ block.super }}
  <h2>{{ adminform.form.instance }}</h2>
{% endblock %}
Posted in Django | 3 Comments

To all my Thinkpad friends running Linux

Hello there!

A little tip that can really improve your battery life and give you peace of mind while working: Install thinkfan: A simple fan control program.

Installation

Here is how it goes on Ubuntu: Step zero is to become superuser (sudo bash). Then you install the thinkfan package.

~# apt-get install thinkfan

Once the package is installed, you can try running it as a normal process to see how it works:

~# thinkfan -n

It will most likely give you an error because fan_control=1 has not been set for the thinkpad_acpi module. You can enable fan control like this:

~# echo "options thinkfan_acpi fan_control=1" > /etc/modprobe.d/thinkfan.conf
~# rmmod thinkpad_acpi
~# modprobe thinkpad_acpi

In order to run thinkfan as an upstart script (actually rc.d), you should set START=yes in /etc/defaults/thinkfan.

~# nano -w /etc/defaults/thinkfan

Customizing

What thinkfan does is to turn the speed of your fan up and down according to the currently highest measured temperature of all your sensors. If you wish to see the temperature of your sensors, simply run:

~# sensors

Now it’s time to look in /etc/thinkfan.conf. Besides instructions, you’ll see this matrix at the bottom:

(0,     0,      55)
(1,     48,     60)
(2,     50,     61)
(3,     52,     63)
(4,     56,     65)
(5,     59,     66)
(7,     63,     32767)

The default settings are very safe, but if when running thinkfan, you get too much or too little fan activity, you can adjust the numbers. The first vector (0, 0, 55) says that whenever a sensor reaches 55C, the fan should step up one level. The second vector (1,48,60) says that if we are at level 1 and a sensor reaches down to 48C or below, we can step back to level 0, however if we reach 60C we should go to level 2 etc.

Please keep in mind that your hard drive temperatures are very important
, and that you can add a line that automatically adds a safety value to those more sensible temperatures. For instance, to ensure that thinkfan is responsive to sensor value 5, we can add this adjustment vector:

sensor /proc/acpi/ibm/thermal (0, 0, 0, 0, 0, 15, 0, 0, 0, 0)

To identify which sensor number your hard drive has, go to ThinkWiki or run Disk Utility (System->Administration->Disk Utility), click your hard drive and click SMART Data to see the temperature of your hard drive.

Troubleshooting

If you have problems:

  • Check that /proc/acpi/ibm/thermal exists, otherwise you might have to look for something else
  • If you run T420 (or some other model that uses the coretemp module), check out mejo’s forum post.
Posted in Computers, Ubuntu | Tagged , , | Leave a comment

django-cms 2.0.2 and Django 1.2 – 1.3

If you are running an old version of django-cms, you can still upgrade Django. I would strongly suggest doing this since it’s very uncomplicated. However, if you are running django-cms 2.0, you should first upgrade to 2.0.2 and run the South migrations, which is also totally uncomplicated.

django-cms 1.x are most likely stuck on older versions of Django as well. I did once try to get them running on new Django versions but gave up.

Basically this guide just fixes a little bug in admin/pageadmin.py and a few issues regarding a missing csrf_token in the templates. It also means that django-cms 2.0.2 becomes SSL compatible. This is not backwards-compatible, however, so if you apply this stuff, your django-cms will no longer work with Django 1.1.

After upgrading to django 1.3.1 (also tested on 1.2.X btw), you need to make the following corrections manually in your django-cms installation.

1) Edit cms/admin/pageadmin.py, line 59 to say:

    exclude = []

2) Edit cms/templates/admin/cms/page/plugin_change_form.html, line 77, by adding the csrf_token tag:

<form id="{{ opts.module_name }}_form" action="{{ form_url }}" method="post" enctype="multipart/form-data">{% block form_top %}{% endblock %}
{% csrf_token %}

2.1) Do the same in cms/templates/admin/cms/page/change_form.html, line 99:

<form id="page_form" action="?language={{ language }}{%if request.GET.target %}&amp;target={{ request.GET.target }}{% endif %}&amp;{%if request.GET.target %}position={{ request.GET.position }}{% endif %}" method="post" enctype="multipart/form-data">{% block form_top %}{% endblock %}
{% csrf_token %}

2.2) Do the same in cms/templates/admin/cms/page/dialog/base.html, line 5:

                {% block form %}{% if form %}<form id="{{ dialog_id }}-form">{% csrf_token %}{{ form.as_p }}{% endif %}{% endblock %}

3) Ensure that you have CsrfViewMiddleware and CsrfResponseMiddleware installed. Your settings.py should contain something like this:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.csrf.CsrfResponseMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
)

5) Finally, you need to bring in a file from later django-cms, csrf.js (github revision that I used). Place it in media/cms/js/.

6) Add csrf.js to cms/admin/widgets.py in PluginEditor.media, around line 17:

    class Media:
        js = [join(settings.CMS_MEDIA_URL, path) for path in (
            'js/lib/jquery.js',
            'js/lib/ui.core.js',
            'js/lib/ui.sortable.js',
            'js/csrf.js',
            'js/plugin_editor.js',
        )]

7) Add this around line 97 in cms/media/cms/js/change_list.js:

	$(document).ready(function() {
		$.fn.cmsPatchCSRF();
	    selected_page = false;
	    action = false;

8) Do the same in cms/media/cms/js/plugin_editor.js at the first line:

$(document).ready(function() {
  	$.fn.cmsPatchCSRF();

9) In /cms/templates/admin/cms/page/change_list.html you should also include csrf.js, around line 33:

<script type="text/javascript" src="{{ CMS_MEDIA_URL }}js/csrf.js"></script>
<script type="text/javascript" src="{{ CMS_MEDIA_URL }}js/change_list.js"></script>
Posted in Django | Tagged , | Leave a comment

Stopforumspam Django Middleware

I have created a new Django app for keeping out spammers on the basis of data collected by stopforumspam.com. Read all about it here ยป

It’s free, open source, and I hope people will make it better on github.

Posted in Django, Ubuntu, Web | Leave a comment