Django : resize an image on upload

This is a typical use-case : your site/app allows users to upload a picture. But you don’t want to store full size 8Mb picture, and still, it’s comfortable for the user to not have to manually resize the image before uploading it. The solution : resizing the image “on the fly” when receiving it on the server side.

Another use-case, also quite common : you need to generate a thumbnail for the uploaded picture. The following code will help you too.

This script is re-sizing the image only if it’s a new PicturePost (we check that the object key isn’t set).

We’re doing some calculation to get the right ratio before re-sizing the image. We can’t use the thumbnail() method because we want to resize based only on the width of the image. The thumbnail() method will resize the image based on the biggest between width and height.

We convert the image to jpeg with a 90 quality (the default is 75, 100 meaning no jpeg compression at all).

We also keep the exif information if its exists. By default, it’ll disappear, so we have to manually add them again when saving the resized file.

Once it’s resized, we save the object into the database.

You can also use the same logic to create a thumbnail of the image and store it into another ImageField field. 😉

Quick Start with Foundation

Never re-invent the wheel ! That’s a good moto for any developer out there. Let me show you how to quickly bootstrap you website stylesheet with Foundation. Its default components will help you to define a generic style very easily so you can concentrate on customizing to match your pixel-perfect design.

Setup Foundation on you machine

Install the foundation command line using npm

Creation of a new project

Setup your project with “foundation new” command.

You’ll have to choose between different options. I just choose the most common one, use Foundation for site with a simple template.

At the end, just type

Each time you’ll modify a SASS file, it’ll be compiled (and rather quickly !) into an app.css file.

All you main SASS code has too be written in app.scss file.

You have the choice to include all foundation, or just a few part of it. Or even nothing at all. But I recommend at least one :

Which is taking care of resetting all styles, setting default styling for HTML elements and so on.

Write your code without annoying default Foundation classes

The goal is to use our own classes with Foundation code. Here is a simple example :

For each of its components, Foundation gives you access to several mixins that you can include everywhere.

Anatomy of a good ExpressionEngine add-on

ExpressionEngine 3 has brought a lot of changes and if you’re coming from EE2, you might miss some that are really interesting. Ellislab team has a done a good job of bringing APIs and services for third party developers to use in their add-ons. Your code will be cleaner than ever 🙂

Here is just a selection of things that are used in almost every add-ons, the ones that you should really use in your next project.

Use Models

Documentation

Messing around with SQL queries might be ok, but if you add-on is quite complex and modify EE data, you might want to switch to using models. Models help you handling EE data (channel, members…) but you can also create your own ones. You’ll just rely on EE code and APIs, it’s way safer and will facilitate maintaining your code.

Plus it totally makes sense to use that service when doing OOP. It’s way easier to get back Objects and play with them, than getting back SQL results and process them before doing anything relevant.

Use shared form view

Documentation

Creating forms can be long and complex. But with the shared form view, it’s so easy you’ll even enjoy it. You basically need to declare your form using arrays. A lot of different field types are available. EE will generate all the HTML for you. Honestly, dealing with add-on settings is no more a pain now.

Use validation service

Documentation

Now it’s time to validate the user inputs. Again, no more pain, just use the validation service. It contains pre-defined rules and you can also create custom ones. Easy to write, easy to maintain, and a nice beautiful code. 😀

Add internal documentation

Documentation

This is not a code tip, but EE now includes internal doc. If you add a README.md file inside your add-on folder, you’ll be able to access that file from within EE. It might not be useful to you, but it’ll surely be for other developers out there that will need your add-on doc. 😉

Check the other services

ExpressionEngine 3 is providing other services that you might want to use, check them out.

EE3 architecture is working with dependencies, prefixes and providers.

There’s even a tree data structure if you ever need one.

Quick guide about Django translation

The documentation of translation in Django is pretty well done, but quite long. If you want to setup it up easily, here are the steps.

Translation Setup

Make sure you have gettext installed on your machine. I wrote an article about the OSX specificities.

Create a locale folder inside your main project folder

Django will create subfolders in the locale folder for each language you want to support.

Make sure you include the middleware class in your settings.py file, create or modify the languages settings and set the LOCALE_PATH variable :

Depending on your project structure, you might need to change the locale path to suit your needs. It’s often the cause of the translations not working !

Translation in .py files

We create an alias to it’s easier to use and I think it makes it more readable.

We can add a special comment which will be displayed in the translation file. It can be useful for your translators.

There are a lot of other possibilities. Please read the documentation.

Translation in templates

We need to indicate that we’re using translation, so put that code somewhere near the top of your template.

Then it’s pretty easy

Again, a lot of options and other methods are available, please read the doc.

Create the language files

Fortunately, Django is doing that for us ! Let’s create the french file :

Django will create the file in mysite/locale/fr/LC_MESSAGES/django.po

It’ll look like this :

You simply have to fill out msgstr with the translated phrase.

If you add new phrases in your code, just run

It’ll update all the languages.

Now, the last step : compiling the files into .mo files (binary files optimized for gettext)

And you’re pretty much done. It’s really awesome to see how easy it is.

gettext issue when using Django Translation in OSX

I ran into an issue when generating my language files for Django.

This is because OSX has an old version of gettext installed. To use the last one, install it with homebrew and force homebrew to override OSX gettext command.

And then, no more problems !

 

Source

Handling several PHP versions on OSX (including php7)

I was trying to use Phan, a PHP code analyser, on my Macbook, but it’s requiring PHP 7 cli. I never modified the PHP cli on my machine, mainly because I use MAMP to handle different environments. But here I had no choice. Here is the way I handled it.

Easy trick : use aliases

Yeah, it’s way too easy that way, just create an alias from your command line pointing to a php bin. In my case, I made it point to php7 bin from MAMP.

Note : you can name it whatever you want. php7 could be a good option too. 😉

To remove the alias :

To make the changes effective, either close and open your terminal again, or type in

Cleanest way : using brew

If you have Homebrew installed on your machine, it’s pretty easy to install whatever version of PHP with it. And it’ll take care of every cli bins.

In my case, I had a 5.5 version of PHP already installed, so I need to tell brew to unlink it before installing another one

And then, let brew do the dirty work for you

Et voilà !

 

Lazy way to make your WordPress content working with HTTPS

Yeah, we’re all lazy sometime. I recently added HTTPS support to this blog but the content saved might contain http links or image addresses. Instead of updating the whole database, you can just add this to your theme functions.php file :

Problem solved ! I know, it’s not very clean, but it’s working great.

Setup environment to work with Django CMS on OSX

Getting the whole python / environment / Django / server running is not that easy when you’re not familiar with it, especially if you’re used to use Apache/MySQL combo. I wrote that guide as a reminder, but I’m sure it can help you setup a Django CMS site pretty easily.

Continue reading Setup environment to work with Django CMS on OSX

Create your own seedbox with Transmission torrent client

Want a small seedbox on your server ? It’s really not complicated, thanks to Transmission.

First step, install it using your OS package manager

Then we need to stop the deamon to configure it.

To edit the configuration file (vi FTW):

Here is a list of important values to change. You can find documentation about the configuration file there.

Configure the firewall so we can actually access the web interface and send/receive packets. Be sure to set it up regarding the ports you entered in your configuration file.

Now, time to start the daemon again

To access transmission web interface, use your server IP address

http://1.2.3.4:8353/transmission

And you’re done ! Easy, isn’t it ?

iptables basic configuration for your webserver

Very important aspect of your web server security : the firewall. Most of web servers are using iptables to control what is going in and out. And it’s pretty easy to use.

Here is my basic configuration (you can also find it on Github) :