Posted: July 14th, 2010 | Author: jaymz | Filed under: code, open source | Tags: django, ooyala, python | 1 Comment »

Ooyala is a feature rich video hosting platform. Something I needed recently was some way to link the data over at ooyala into a django site I’m building. So I wrote myself a library. It’s available over at github and it’s called (predictably) django-ooyala. Currently there is a management command syncooyala to pull in all the data using the Backlot Query API. These imported items are then linked to a specific URL. Finally in your templates there is a ooyala_video tag which when given the current path (via request.path) returns the <script> tags needed for it to render.
Expect some updates as I flesh it out into the front end over the next few days.
Updated (11th August): I have added in analytics support. You can now make requests for video stat’s for a given account or video. The facebook SDK has also been added with a new template tag to output the headers in your template for a given video. Remember to request whitelisting from facebook for SWF embeds to work.
from django.http import HttpResponse
from ooyala.library import OoyalaAnalytics
from ooyala.constants import OoyalaConstants as O
from ooyala.models import OoyalaItem
def backlot_query(request):
req = OoyalaAnalytics(video=OoyalaItem.objects.all()[0].embed_code, \
method=O.ANALYTIC_METHODS.VIDEO)
ooyala_response = req.process()
return HttpResponse(ooyala_response.toprettyxml(), mimetype="text/xml")
Posted: July 2nd, 2010 | Author: jaymz | Filed under: code | Tags: android, django, java | 1 Comment »
Quite a while back I was trying to get my head around the fairly complex ListView control for android and something I thought perfect for it was the set of radio shows over at spine magazine.
I have had this code sitting around for a while and I thought why not knock out the news code and release it and then just continue to work on it and update it. As the saying goes, release early, release often! I think it’ll be easy enough to add in reviews using the same sort of code to get the data and just re-using the show layout. This particular app was a lot more complicated than the django-reference thing, so here is some of what I’ve learned… You can get this in the marketplace now. Bug reports are expected & indeed welcome.

Warning! Technical crap follows.
It takes a bit of time to get your head around it, essentially you subclass the ArrayAdapter class with a container class to hold your list view elements. The ArrayAdapter is a templated class so you first need another class that is actually going to hold your data. This is were you’ll need to create a class for your data and then instantiate the ArrayList with this type. The code to hold my news post data is very simple:
package eu.jaymz.spinemag;
import android.util.Log;
public class NewsPost {
private String title;
private String description;
private String date;
private String author;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Then in my main activity class I cast the templated ArrayList to a NewsPost class:
ArrayList<NewsPost> spr_news = new ArrayList<NewsPost>();
With the data structure sorted I need a method in my ListView activity to actually fill in the ArrayList with data. This is very typically something along the lines of the following, here I read the JSON data from a URL, decode it and assign it to the class members, finally appending it to the ArrayList from earlier.
private void getPosts() {
try {
URL url = new URL("url for data");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
in.close();
spr_news = new ArrayList<NewsPost>();
JSONObject json = new JSONObject(str);
JSONArray posts = json.getJSONArray("posts");
for(int x=0; x < posts.length(); x++) {
JSONObject post_data = new JSONObject(posts.get(x).toString());
NewsPost post = new NewsPost();
post.setTitle(post_data.get("title").toString());
post.setDescription(post_data.get("description").toString());
post.setDate(post_data.get("date").toString());
post.setAuthor(post_data.get("author").toString());
spr_news.add(post);
//Log.d("NEW", "added a post - "+post.getTitle());
}
} catch (Exception e) {
}
runOnUiThread(setPosts);
}
With all the above in place the code is at the point where there is now an array full of structured data for our ArrayAdapter to show. To do this the ArrayAdapter needs subclassed to work with our earlier NewsPost classes members:
private class NewsAdapter extends ArrayAdapter<NewsPost> {
private ArrayList<NewsPost> items;
public NewsAdapter(Context context, int textViewResourceId, ArrayList<NewsPost> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.post, null);
}
NewsPost o = items.get(position);
if (o != null) {
/* hmm, this looks proper ugly */
TextView title = (TextView)v.findViewById(R.id.post_title);
TextView description = (TextView)v.findViewById(R.id.post_description);
TextView date = (TextView)v.findViewById(R.id.post_date);
TextView author = (TextView)v.findViewById(R.id.post_author);
description.setText(o.getDescription());
title.setText(o.getTitle());
author.setText(o.getAuthor());
date.setText(o.getDate());
}
return v;
}
}
All this code is really doing is loading the layout resource XML for our row and binding the data from the array elements members to views within the XML. Finally we need to tell the list view to update. Typically the above code has all been kicked off and ran in a thread, populating the ListView when it’s actually finished without stalling the phone itself.
for(int i=0;i<spr_news.size();i++) {
n_adapter.add(spr_news.get(i));
}
m_ProgressDialog.dismiss();
n_adapter.notifyDataSetChanged();
All the above is doing is updating the ArrayAdapter and adding our ArrayList elements to display. We then signal that the data has changed which will cause the ListView itself to update. Simple!
Again, I must make the point of how verbose this all feels when I’m coding in python all day. I’m sure it needs a good refactor to now but I’m a lot more familiar with the way android works with layouts and indeed strong typing
Posted: June 30th, 2010 | Author: jaymz | Filed under: code, open source | Tags: blogger, django, python | 3 Comments »
I’ve been working a lot on Vans recently and they have a large number of blogs that are currently hosted on the blogger platform. This works really well for them, they have a straightforward & easy to use blog platform that does what they need it to do. The sites themselves work great as they are but the integration into the main site isn’t quite as nice.
If you go to the blogs section on vans as it stands it will pull in an XML feed of all the current blogs and display them on one giagantic page. It can take a fair bit of time to load and its hard to see how each different blog gets displayed as its really one big list. For now it’s not so easy to pull in bits of content from the various blogs without someone having to mess with files or copy & paste, with the build in django I wanted to create a much cleaner & easier way to work with this content.

For this I introduce django-blogger, a django application which will integrate Google Blogger blogs via their RSS feeds. As it is it will import the blogs for a given profile id and then sync up with the latest data via their RSS feeds. These can be enabled easily for your given blogs. When you first install it comes with some admin actions which will all ow you to sync up the blogs manually all at once. There is also a management command, syncblogs, which is more suited to scheduling an update via cron (if you’re going to use cron you might be interested in django-crontab).

This works on the feeds and not an archive, so it doesn’t require authentication, just access to the feed URL. A basic template is included to show how to render out the blog posts & blogs as a menu, I override these myself for the format I need. The app itself now lets me pull content from any of the blogs and use it within the rest of the django based site cleanly & easily. Rather than directly reading and displaying via the feed URL I’m creating actual objects for each post and blog so it’s easily extendible also, say to return posts in various formats or pulling images from each blog post to create blog galleries automatically.

The code is available from GitHub.
Posted: May 28th, 2010 | Author: jaymz | Filed under: android, code | Tags: android, apps, django, java | No Comments »
Very late last night, as I was enjoying “The Design & Evolution of C++” (an excellent book, even if you’re not a C++ dev), I took a short break and was browsing around on the android market place and noticed there was no django app. I’ve recently picked up an HTC desire and have been really enjoying developing on it. I’m currently working on an application for Crooked Tongues to allow people to post & comment their sneakers (don’t ask…) and I thought this would be a quick app to do and get out there. Anyone starting out developing apps will be aware that one of the hardest things is just thinking of something that isn’t already crowded out by what’s already available.
Basically, all I wanted was a more dedicated browser for the django docs. The content there is excellent so I quickly knocked together an application that lets you jump to major sections, set a page zoom by default and switch version easily. All in all it only took a few hours of development and thanks to the approval procedure it’s already available to install.

It’s a glorified web view but it’s a start point and its at least out there now. I’m getting a lot more used to the dev process now on android. The total code for this app weighs in around 400 lines – around 80 of those being XML to define the layout and strings used. The main activity class then manipulates the XML defined webview component to react to the (again XML) menu. My main constructor method looks like this:
@Override
public void onCreate(Bundle savedInstanceState) {
Resources res = getResources();
version = res.getString(R.string.default_version);
base_url = res.getString(R.string.url_base);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new DjangoWebClient());
final View zc = webview.getZoomControls();
FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
mContentView.addView(zc, ZOOM_PARAMS);
zc.setVisibility(View.GONE);
setZoom();
loadDocs();
}
The remainder of the methods are basically just reacting to the menu choices and directing the webview url appropriately. All in all quite simple.

Posted: May 16th, 2010 | Author: jaymz | Filed under: android, code | Tags: android, django, java | 2 Comments »
I recently picked up an HTC desire and have been loving android as a platform. I’m not entirely sure why I didn’t go for android to begin with (apart from doing my friend Spangsberg a favour by buying out his iPhone contract before he left the country). I dabbled a little with objective C but not owning a full on mac for dev’ing put me off spending a significant time at home coding for the device. That and it seemed like a lot of work to get going (probably because I use Linux almost all day so am not used to the Mac toolchain).
Working with android on linux has been a total pleasure. Mostly. I’m still getting re-used to the verbosity of java compared to my day-to-day python but the experiance has been a good one so far. I’ve basically been learning by looking at the sample apps supplied combined with a lot of googling and reading of android dev forums. There is a real wealth of documentation out there and there’s an absolute ton of general java code to solve most problems. This is where I’m at about a week into it (spending a couple of hours every few days on it).

The HTTP posts are working now as is uploading a camera shot, you can store user/passwords and then it’ll do the form posts with that data. For now I have it just “logging” in to a dummy setup on my own server – that side is just some PHP for now. The real backend runs python (django), for now its just easier to run that data through some PHP on my blog server. The data for the views comes via a JSON array, that data is served up via python and is essentially just a REST interface to the TIA area. I’m mainly concentrating on just learning my way around the SDK but already I’m seeing that theres a lot of power in combining the android platform with django (and maybe django-pistons on top) to create rather powerful connected mobile apps.
Posted: September 14th, 2009 | Author: jaymz | Filed under: django, musing | Tags: django, magento, php, python | 2 Comments »
sigh… Recently I have had to (more than once) explain why we’re choosing Django to develop a e-commerce & web app platform rather than using something off the shelf like Magento. I’ve heard lots of negative things regarding Magento and here’s what I’ve outlined in an email to PM’s etc which others might find useful if they have a similiar “justify it” experience.

The number one reason though I’d rather not work with Magento is that time and again developers express difficulty in making magento work for them and not their magento consultant.
Anywayz, I’m sure someone will take offense to this and I’m well aware I’m comparing a framework to a specific application but whatever…
Actually come to think of it, I’m not even comparing it really django, just listing some gripes and hate I’ve read/heard elsewhere
Code extension & extendability
June 2009 : pickledshark.com
This guy is basically critical of the codebase being complex and difficult to work with. He ends with this:
Due to the Zend/OOP/MVC influence on Magento it is impossible to follow the code. Classes are referenced dynamically, various aspects are contained in XML files and there is no clear flow that you can just debug through. The sheer volume of files and folders makes finding something unbelievably tedious.
Even the database is a minefield. In every other system I have used finding data is easy. In Magento, the use of EAV means that data is split amongst hundreds of abstract tables. Again, it doesn’t flow and it doesn’t make sense without a great deal of time developing a solid understanding of what they have done.
A final Cynical Note… Many people claim that the complexity of Magento is somewhat intentional. The profitability of Magento relys on consulting, technical support and installations. Making the codebase complex could mean that many developers will start out, get stuck and pay for help. If this is the intention of Varien then perhaps they have been very goal focused
Hmm…… Another post here says that for most bits that arent in the free or enterprise version you can probably find a plugin to do it for you at magento-connect. Here’s something you’d imagine would be useful “get the lowest price free” and there is an extension for it. Problem being its $150.
Lowest Price Free plugin
The comments that I find over & over are that the system itself is a nightmare to work from from a developer viewpoint unless you are some magento expert consulting for a fee (big surprise). Eg, here: (april 2009)
The big thing for me right now is the documentation(specifically around customization) – it is nearly non-existent. The forums have TONS more questions that(sic.) answers, most of the Wiki articles focus on simple store administration or VERY simple theme level customizations (via CMS pages or edits to layouts).
Multiple products with similiar data & attributes (eg sizes)
Last post 1 HOUR AGO!
A long discussion about how much of a ball-ache it is to manage multiple products that share a common base (i.e. different sizes of the same product). There is some talk about configurable products, grouped products etc but it seems that even all these still require duplication/entry of the same data per SKU:
This problem is driving me nuts! I have a shop to build with over 100,000 possibilities of one single configurable product. Now… Could anyone tell me, how exactly I am supposed to manage this? I just can’t seemto find anyone dumb enough to add those products manually and I’m certainly not doing it myself! This is magento’s great weakness!
AJAXifing Templates and core features
Last post 3 Weeks ago
Lots of talk here trying to work out how to add ajax functionality to the magento site and surprise surprise, its not supported out of the box and rather complex for most people to try and work out. Instead, well:
Vivendo has a great extension and it’s very affordable!
Whoop-ee! Who’s Vivendo? Oh yeah, the people that write the magento core.
PCI DSS Compliance / Gift Certs / Logging of admin data
For processing cards onsite your cart system should comply to the PCI-DSS regulations. Magento community does not and should be used in situations where you hand payment to another system like paypal or do not store card data on site if using a gateway. The Enterprise (the $8000/yr) system is scheduled to be certified to comply to PCI-DSS soon. Partly because the Enterprise version includes data encryption which the community one does not.
While a Django system would not be “out of the box” PCI-DSS compliant we would be writing the code to store attributes etc and would be able to add an encryption layer (such as 3DES or blowfish etc) to that data. It should also be noted that only the Enterprise edition supports admin logging, gift certs and some nice “extras” such as store credit.
Also the enterprise version is the only one to allow for a proper “walled garden” of catalogues so for example having a section of the store for dealers and not general public orders is not possible with the stock-standard magento. Its also not possible to have sub-admins (could only add new products for example) either something which the django admin site has out of the box (and is easily customizable/extended).
Compare magento versions
Licensing
This is valid at the moment Magento Licesnsing:
As such, you must disclose any changes you make to the OSL 3.0-licensed copyrighted works whenever you distribute Magento or make your Magento store or software available over the web to a third party.
We would probably require a commercial license for magento (i.e. enterprise) if we did not want to re-release any changes to core. Judging from the way people talk about working with magento you will probably end up having to modify core at some point. A Django solution would be completely our own code and even hacking django core, its licensed under BSD meaning commercial works need not release derivative works back to the original source branch.
BSD License Terms
Due to the extremely minimal restrictions of BSD-style licenses, software released under such licenses can be freely modified and used in proprietary (i.e., commercial) software for which the source code is kept secret.
Payment Systems
Magento may or may not support the gateway that we want to use. They have support for a lot (most with an additional fee for the module, seeing a pattern here?):
Supported magento payment systems
Paypal, google checkout, sagepay (protx) are supported with free/paid extensions. The protx one (updated for sage pay) is in beta at the minute and there are 3 pages worth of reviews pointing out problems people are having with it. This is an extra reason for concern for me personally – having to rely on a load of extensions written all by different people, some charged, some not just to get the core functioning. What happens if there’s a bug in one of them?
This is a great extension and have Just upgraded to latest version and now it dont work? Using vewrsion 1.3.1.1 beta.
Fin
Ok, thats probably enough for now. Main point being that this system is customisable – for a fee. If they are mainly concerned about linking systems together then they really need to understand that the data can come as and how they want it. If they want XML of all their orders based on a SKU then we can provide that. If they have custom systems or expect data in such a way then we will fit output to that schema! It still feels like they think magento is this software like microsoft office that is going to somehow solve a load of problems off the bat.
One of the main things for me is that Magento is a dual licensed, essentially paid for system with a big consultant/community that work to push magento. Django is an open source BSD licensed framework built for developers to develop web applications on. Django does not offer paid-for licensed versions, its in django’s interest to have as much and as transparent documentation as possible. For Magento its not quite in their interest to have that same level of documentation, something which seems to be reflected when you read about developers experiences trying to extend it. Invariably it ends with a “magento expert” doing the work or the person struggling to fix it and ending up hacking core or finding some extension that just about does what they want and they live with it.
I know I’m going to end up having some “out of the ordinary” use cases when it comes to discounts, cart functioning and products themselves and the idea of working with magento does not thrill me. When you see posts on stackoverflowasking if theres any other documentation but the source code for the ORM then alarm bells ring. Especially when you’re used to the sort of quality & thorough documentation django provides
End rant! So yeah, I’m not the worlds biggest fan of magento. And I’ve not even touched on the actual quantity & scale of the db tables it installs on a fresh version. Magento fanatics please keep your “its brilliant & you are a knob” mail to yourself.