Welcome to SpellCoder Sign in | Join | Help

A problem that we face everyday is creating CRUD interfaced for our databases. Creating a CRUD interface is always a boring and tedious job. Even if you want to initialize your data, you may break something by mistake if you use SQL Server Management Studio, because it doesn't know anything about your business rules.

Django - A web framework for Python - gives you the solution. It has an application called "Django Admin" that provides a customizable, production-ready, nice-looking UI on top of your DB.

If you want to see the final results, see the screenshots at the end of the post.

Setting the environment

  1. Install Python 2.5 or newer from http://python.org/download/.
  2. Install Django 1.0 or newer.
    1. Download the compressed archive from http://www.djangoproject.com/download/.
    2. From the root directory, run this command in the console: python setup.py install.
  3. Download and install pyodbc from http://sourceforge.net/project/showfiles.php?group_id=162557&package_id=183492. You should install the one that is compatible with your version of Python, e.g. if you installed Python 2.5, you should select pyodbc-2.0.58.win32-py2.5.exe.
  4. Checkout django-pyodbc from http://django-pyodbc.googlecode.com/svn/trunk/ using a Subversion client, then you should move the directory sql_server to C:\Python25\Lib\site-packages\.

Creating the Django project

A Django application is a complete component, including the DB model, views and templates. Good examples of Django application are user registration, tagging, search, etc. You can find a lot of reusable applications on http://djangoplugables.com/.

A Django project is a group of application that works together.

Now you have the environment setup for Django. The next step is creating a new Django project. On the console, write this command

C:\Python25\Lib\site-packages\django\bin\django-admin.py startproject sqlserveradmin

Then create an application using this command:

manage.py startapp mydb

To add the application to the project. You must change INSTALLED_APPS in settings.py to include the app.

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'mydb',
)

Setting the database

Now you have the project in place. You need to modify settings.py to point to your DB. Change these fields to be

DATABASE_ENGINE = 'sql_server.pyodbc'
DATABASE_ODBC_DRIVER = 'SQL Native Client'
DATABASE_NAME = 'db_name'
DATABASE_USER = 'webapp'
DATABASE_PASSWORD = 'sikrit'
DATABASE_HOST = r'test_server\SQLEXPRESS'

Generating DB models

On the console, run the following command

manage.py inspectdb > inspected_models.py

This will generate Python models for your tables. Unfortunately, they are not arranged. To arrange them, use the script that I posted to http://www.djangosnippets.org/snippets/1203/.

rearrange_models.py inspected_models.py models.py

Use the new models.py to replace the old one in the folder mydb.

Installing the admin

To install the admin, you need to do two things

  1. Add the following line to INSTALLED_APPS in settings.py. It should look like
    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.admin', 'mydb', )
  2. Run this command to add the tables necessary for the admin to work.
    manage.py syncdb

    It will ask you to add a superuser. This superuser will allow you to access the admin.

  3. You need to tell Django what models that you need to be viewed in the admin. For our case, we want to view all the tables. I will use a little bit of Python magic to add all the models to be viewed in the admin. Put the following code in a file called admin.py in your application folder.

    from django.contrib import admin
    from django.db.models import Model
    import models # This is your models' module
    for m in dir(models):
        class_ = getattr(models, m, None)
        if class_ and isinstance(class_, type) and issubclass(class_, Model):
            admin.site.register(class_)

Congratulations. We are done.

What you can do more

  1. Change the templates used to view the UI.
  2. Customize the names of the models and fields, for example you can supply correct pluralization.
  3. For enums, you can specify the available choices, and the admin will view it as a drop down.
  4. You can add more users to the admin, and specify their permissions.
  5. And a lot more. See http://docs.djangoproject.com/en/dev/

I hope this wets your appetite for learning Django.

Screenshots

The home page. I had to remove everything that reveals the application. As you can see, Django admin also stores a history of changes.

This is the view for students. I customized it a little bit to view students' emails, but you can customize it more.

This is the form to change the student. The "UserId" is a foreign key to the "Users" table. Django admin is smart enough to grab them for your. If you want to add a new user, you can add it by clicking the small "+" sign beside the drop down list.

This shows the screen for users in the "Auth" application (which is added by Django itself). This shows a more advanced listing: you can select the columns that will be displayed, with sorting. You can add filters (on the right), and you can add search also.

This is the form for editing users.

 

A friend told me that there is an arabic book for Python, which is a translation of "Byte of Python", one of the most popular Python books.

This is a good step toward popularizing Python, specially among children.

Please go and download the arabic version. Read it, and tell the author about any enhancements that the book needs.
Download Day 2008
If List<> inherits IList<>, and MyIdentity inherits IIdentity, then why the heck the C# compiler cannot cast from List<MyIdentity> to IList<IIdentity>??????

It is the same, the compiler is not smart enough to know this.

So to satisfy the compiler - acting as a good compiler slave - I have to write this
IList<IIdentity> returnList = new List<IIdentity>(); // A variable name cannot be stupider
foreach (var item in originalList)
{
    returnList.Add(item);
}
return returnList;
And people were wondering why dynamic languages takes less lines of code.
There are some talks lately about the next version of C#, and what is should add. People has gone too far asking for new features, most notably the dynamic lookup
 
static void Main(string[] args)
{
    dynamic
    {
        object myDynamicObject = GetDynamicObject();
        myDynamicObject.SomeMethod();         // call a method  
        myDynamicObject.someString = "value"; // Set a field
        myDynamicObject[0] = 25;              // Access an indexer
    }
}

The above looks ridiculous to me. C# is statically-typed language, and it should stay like this. Trying to add dynamic typing to it - even if it was optional - is stupid. For me, a language must establish a few basic concept and stick to them, not try to satisfy every one out there.

C# has incomplete features. These should be completed first before adding any new features. Take type inference as an example. You cannot return anonymous types from methods, because you don't know their names. It should allow something like

public anonymous MyMethod(string email)
{
    return from user in Users
           where user.Email = email
           select new { FullName = user.FirstName + " " + user.LastName };
}

It can also add named parameters, and default values for them, just like Python. It can be supported indirectly now by passing anonymous types, but complete support for them would be better.

Dynamic languages are not just about dynamic lookup, and supporting broken dynamic lookup - just like the above mentioned example - is going to be, really, broken. What about dynamically adding new methods? What about using making a class as a proxy, say, for a web service? What about object-specific members - members which exists for a specific member? It can get very complex, and the only known way to allow dynamic features is to make your language dynamic.

The beauty of the CLR, is allowing different languages to run and inter-operate. Unfortunately, it was designed with static typing in mind, which appears clearly in the BCL design. I wish the DLR team has these limitations in mind so they address them better, specially the importance of providing libraries which fits the dynamic languages way of doing things.

One language is not enough, and one language which tries to fit all purposes is going to be very complex - just like C++. The solution is learning different languages and using the appropriate one when it fits, and integrating them when you need to.

Everyone uses youtube to see his favorite videos. Most of the time, you need to subscribe to a certain user’s feed using RSS. Unfortunately, the ‘Subscribe’ buttons subscribes using their own notification system, and you have to be a member to subscribe.

Contrary to popular beliefs, youtube provides RSS feeds, but they are hidden. It’s mentioned on http://www.youtube.com/rssls that you can subscribe to a user’s feed as

http://www.youtube.com/rss/user/[insert username here]/videos.rss

And to a certain tag using

http://www.youtube.com/rss/tag/[insert tag here].rss

The question is: why did they hide this?

Finally, the long-awaited Django book is released under the GNU Free Documentation license. Everybody can learn and suggest enhancements to the book or Django.

Does Django runs with IronPython? Yes, it runs on IPCE, but I don't think that everything runs well, because the different DBMS drivers are not ported to IronPython yet.

ActiveState, the leader in dynamic languages development tools, has released the OpenKomodo intiative. OpenKomodo is targeted at creating an open source IDE for dynamic languages and the web, based on ActiveState Komodo IDE (which is proprietary).
Manning Publishing is working on the first book about IronPython, called IronPython in Action. You can download the first chapter for free. They have provided the chapters 2, 3 and 4 under their early access program.
Here at Silverkey, we use LLBLGen Pro as our ORM. One of the nicest features of it is prefetching related entities, so if you have a "CompanyEntity", you can tell it to fetch all related "Employee Entity". This makes our jobs easier, except when it doesn't!!!

Today I had a few bugs with LLBL: Trying to save an entity threw NullReferenceException deeply from inside LLBL code. Even though it worked in some circumstances, it didn't work on others. It was easy to blame LLBL, since it was the source of the exception. So instead of letting LLBL load the related entities, I loaded them myself.

This worked OK for sometime, but it started to through the same exception again. So it really appeard that the problem was not what I thought.

First, I recognized two things
  1. It happens in with a certain class, so this class was the source of the problem
  2. It happens only when I changed the objects. If I didn't change them, it works OK.
The code for saving was like this
using (Transaction transaction = new Transaction(System.Data.IsolationLevel.ReadUncommitted, "SaveCompany"))
{
    try
    {
        // Save related entities
        foreach (Employee employee in CompanyEmployees)
        {
            employee.Transaction = transaction;
            employee.Save();
        }

        // Save modified fields
        _entity.Transaction = transaction;
        _entity.Save();

        transaction.Commit();
    }
    catch
    {
        transaction.Rollback();
        throw;
    }
 }
Trying to access any member of the entity now will throw a NullReferenceException. Though the entity is not null, it will try to connect to the database using the connection of the closed transaction, which will throw the NullReferenceException.

It should be like this
using (Transaction transaction = new Transaction(System.Data.IsolationLevel.ReadUncommitted, "SaveCompany"))
{
    try
    {
        // Save related entities
        foreach (Employee employee in CompanyEmployees)
        {
            employee.Transaction = transaction;
            employee.Save();
        }

        // Save modified fields
        _entity.Transaction = transaction;
        _entity.Save();

        transaction.Commit();
    }
    catch
    {
        transaction.Rollback();
        throw;
    }
finally
{
_entity.Transaction = null;
}
 }
The lessons learnt: It's easy to blame the tools you use, but probably it is your fault.
Last Friday was the date for the 1st EgyPy meeting. Few people showed up. We discussed the release of IronPython 2 Alpha 3, IronRuby, Microsoft support for dynamic languages & DLR.
Also, Dody showed us Havana, our CMS which we built using C# & IronPython 1.1. Havana is different because
  1. Havana is not strictly a CMS. It's a content management framework with a helper interface.
  2. It supports uploading of content in zipped files
  3. Command-line interface: So you can type "zip *.jpg" and get all your .jpg images compressed in a zip file!!!
  4. It supports IronPython scripting: It's not just for designers
We discussed how Havana uses IronPython & the chances of integrating DLR with it. Writing a workflow DSL in IronRuby will be very nice Big Smile [:D]
What: 1st Egypy (Egyptian Python Users Group) meeting
Where: Cilantro Cafe in Mesaha Square, Dokki, Cairo, Egypt
When: Friday, 27th of July, 2007, 6:00 PM
Why:
  1. For the community members to know each other.
  2. To define the activities for the next months
  3. Share experience with Python
  4. Have fun Smile [:)]
If you don't know the place, it's here

For any questions, you can contact me at Attachment: email.PNG (783 bytes)
posted Wednesday, July 25, 2007 10:42 AM by tayseer | 2 Comments
Filed Under:
Attachment(s): email.PNG
I am planning to start a Python community in Egypt. Python is a high-level dynamic programming language which values programmers' productivity over machine performance.

You can register here & find the Python forum here

Update 16/7/2007: A version of my presentation in PowerPoint 2003 format is attached with this post. The demos are attached with the original one here

For audience opinons, see Korayem or Ahmad Shreef
To know how we got the idea of the demo day, read Dody's story.

The Idea

The idea of the 2nd demo day started after MIX 07. We were very excited about the new technologies: Silverlight, Jasper & DLR. We got more excited after we used these technologies in our own products. We chose topics 7 weeks before the demo day. At that time we already started using Jasper in one of our projects & was thinking about porting another one to the DLR, which was using IronPython 1.1.

I'm using Python since 2004 & I have presented it to a lot of my friends. I have presented it also at an internal demo day, which yielded this post. This was a good chance to present Python to the egyptian developers community. Dody suggested that it should be a comparison between static & dynamic languages. I was very worried about this, because it's hard to control it.

Preparation

We started preparing for demo day 2 one week before the end of May. Everybody had his topics. Taher, Ayman & Dody started looking for the suitable place. The rest of us worked on reading everything we can find about the new technologies, using them (in Silverkey applications, if possible), making the presentations ready, etc. Some changes happened to the topics & materials. Some people declined, for several reasons. We have been rehearsing the sessions daily for 2 weeks before the demo day.

The Day

I arrived at the place at 8:10 AM. I was wearing a 3-peaces suite (& it was about 40-something °C). People were telling a lot of jokes about me, including Dody calling me the "Mafia Guy" Smile [:)].

We started checking for everything in the place: the microphones, the projectors, the demos, the business card, etc. We were worried: Only Hossam Zain, Mohammad Hossam & Mohammad Meligy gave presentation in the previous demo day. For the rest of us, this was the first time to present to a large audience. The size of the croud is large this time: almost 400 people.

The action started at 10:00 PM with the keynote, where Taher - our CEO - introduced Silverkey & why we are making a second demo day.

The first technical session was about WCF, by Mohammad Hossam. People liked the session & how he presented it.

After that, we had 2 parallel tracks. The problem with this is that you cannot attend all the sessions you like. So I'm describing only the sessions I attended. We are going to upload the video sessions very soon.

I attended the Jasper, by Ahmad Ali. Jasper is an ORM based on Microsoft Entity Framework for dynamic languages. I liked this session & Ahmad style was really good.

The next session was about WF, by Mohammad Nour. He presented it well, but he was frustrated a little bit at the end because he felt that people didn't get it, but the questions at the end showed that some people were really interested.

The next session was LINQ to XML, by Mai Soliman. She presented the material very well & she answered a lot of questions.

The next session was by me & Mohammad Hossam about comparing IronPython with C# 3. I was worried as hell about this one. AFAIK, this is the first time somebody talks about Python in Egypt. What scared me more is that it was a dialog, not a one-sided presentation. I thought that this would be the best or the worst session in demo day Smile [:)]. The audience were interested & some of them were already using Python, so this will be a good start for a Python community here in Egypt.

We had an open-mic session after that. The people took the mic & started discussing their ideas. We started by asking this question "What do you think will be the next big thing?". As usual, it turned into the boring java-vs-.net discussion, sometimes mentioning PHP. We heard the usual argument: Java is more suitable than .net for enterprise applications. We tried to move it to another subject, but to no avail.

Finally, I attended the Blackbelt Javascript by Mohammad Meligy. Though Meligy can always keep the audience interested, he always takes more time that he should Smile [:)].

A final note: we are not Microsoft. We are not a Microsoft partner or a training company. We made this only because we love it. It's a chance for all of us to share what we know. Being the presenter doesn't mean that I'm the smartest guy. By the way, we are not the only company which shares knowledge with the community. It's just that we have the biggest event.

We really appreciate everyone who came, specially people who came from places far-aways as Alexandria. It was a great chance to meet all of these people. We are waiting for your suggestion



Dear Pythoneers

Silverkey is going to hold its second demo day on 7/7/2007 in Cairo, Egypt. You can see the demo day agenda here. We are going to make a discussion about static vs. dynamic languages, using C# and Python as examples. I'm going to represent Python side, and Mohammad Hossam is going to represent C# side

Waiting for you there

More Posts Next page »