Skip to main content

Posts

[Django CMS] Adding plugins inside plugins programatically

I have a task to migrate a website. Old one is plain HTML and new one is Django CMS. I have a script that parses an HTML page from old website in to CMS page and places it into proper place. Task is to migrate all the page content (that is a CMS TextPlugin) into an interlinked pages setup. Like we have text <p>blah blah </p><a href="/page/url">Text</a><p> some other text</p> And I need to change it into CMS LinkPlugin that is nested inside of the TextPlugin. It have become a common standard in the Django CMS world now. Note we need to have a LinkPlugin because of the requirement to interlink pages. E.g. <a href="/page/url"> is a link to CMS Page object. The solution is divided into two major parts. First we need to have the link plugin added to a placeholder of the text plugin. It must also be nested by the TextPlugin, like Django CMS admin web UI would do.  So our plugin would look somehow like this in the CMS Pag...

Don't put html, head and body tags automatically into beautifulsoup content

Was making a parser recently with BeautifulSoup. Came to the final with rendering contents of the edited text. Like so: text = "<h1>Test text tag</h1>" soup = BeautifulSoup(text, "html5" ) text = soup . renderContents() print text It renders those contents with a result wrapped into the <html>, <head> and <body> tags. So print output looks like so: '<html><head></head><body><h1>Test text tag</h1></body></html>' That's a feature of the html5lib library, it fixes HTML that is lacking, such as adding back in missing required elements. The workaround is simple enough: text = soup . body . renderContents() This solution will return an inside of the <html> tag <body>. Result is: text = '<h1>Test text tag</h1>'

Error copying files in Finder of OS X

Problem: I have an SD flash card (8 GB Kingston) and a MacBook Pro 13" (Late 2011 model). I also use external card reader time to time. This problem persisted on all the conditions. The error message was stating: The Finder can’t complete the operation because some data in “” can’t be read or written. (Error code -36) This was happening while copying Photos from my camera (cr2 files). It worked, however, in case of copying files up to 200 MB in total size of batch. It dropped this error message and did stop to copy files upon selecting of lots of RAW files. E.g. all of them and attempting to copy them from a flash drive. Solution: Problem occurred with building miniatures of the CR2 files. Those files are quite heavy photos (25+ MB) and building miniature did take some time. While building those miniatures on both MAC and SD card finder windows it did die. For me it was enough to change the view from icons to list. E.g.: This did solve it for me. Other solution (Suspect...

5 most common Rsync command usage examples

rsync is a command for remote sync. It is used to synchronise one location to another in a simple way. Location is meant to be local directory, server or remote web server or whatever accessible by ssh. Advantages of using rsync over other tools is speed and bandwidth requirements. First time rsync copies entire contents of the directory provided and increments changes over next sync times. Command syntax: $ rsync options source destination 1. Synchronising local directories: $ rsync -zvr /tmp/logs/ /tmp/logs1/ building file list ... done created directory /tmp/ logs1 ./ log. log sent 98 bytes received 48 bytes 292.00 bytes/sec total size is 0 speedup is 0.00 $ Used rsync command options here: -z is for compression -v is for verbose output -r is for recursive directory scanning By default rsync does not preserve timestamps.  2. Preserve timestamps and permissions during sync: $ rsync -azvr /tmp/logs/ /tmp/...

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...

Django CMS custom Plugin ManyToMany fields problems.

Developing a website for Django CMS I have done a plugin. This plugin however have had a problem. It had an m2m field. While selecting images there (It was a gallery plugin). It was not saving it to production. This way main problem with this field was that plugin have displayed m2m choices on a draft page and while storing it to live it fails. Django CMS Pages is built like so it has multiple versions of pages. Each Page model that is changed is stored under new revision. And so plugin is copied and relinked to that page too. So plugin revisions multiply to. For e.g. in case you have a Draft page with two plugins that have model primary key number (pk in future) 12 and 14 accordingly. Say you hit publish changes. Not only PK of Page model rises and data are copypasted into new empty Page model instance. Those plugins are copied to. So mentioned plugins PK would change to 15 and 16 accordingly, assuming 14 is the latest plugin pk. New copied plugins will be linked to new page. Tha...