Thursday, February 5, 2009

Simulating post-request (using form-data) with file in C/C++

First I started to use WinInet. God, it sucks like most Windows API. I played about a day with http://support.microsoft.com/kb/165298
And I failed to post anything, from what django could make request. Aslo wierdshark shown that MS does a bit strange request (it looses some header about multipart form-data).
Today I took libcurl, it has a rich example who you can perform your POST-request: http://curl.haxx.se/libcurl/c/curl_formadd.html
Note, that it's just example and you have to defien record and record_length.
As for me, I tooked from there such piece of code:

CURL *curl;
CURLcode res;
//
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();


struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
struct curl_forms forms[2];
char file1[] = "d:\\backup\\thumb.jpg";

forms[0].option = CURLFORM_FILE;
forms[0].value = file1;
forms[1].option = CURLFORM_END;

curl_formadd(&post, &last, CURLFORM_COPYNAME,
"pictures",
CURLFORM_ARRAY, forms, CURLFORM_END);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);


Pretty enough. But python is always prettier: http://code.activestate.com/recipes/146306/
;)

Monday, February 2, 2009

Storing files in database with django

Yesterday I got much pleasure implementing DatabaseStorage class for django. Nothing special and a very simple thing, but I like it: http://www.djangosnippets.org/snippets/1305/

Thanks to python and django developers for marvelous duet: python and django :) It really rocks and doesn't have boundaries!

Thursday, January 22, 2009

ManyToMany relations in Django.

I didn't write anything for some time, since was very busy with my work and family problems.
But today I have some interesting thing about django, so I can't keep silence (I'm new to python/django, so it can be interesting for newbies only, but I hope it is not).

First of all I want to say sorry for indentation in quoting blocks... Will fix it later...

In django to define ManyToMany relation you should use ManyToManyField in your model. Consider folowing code:


class User(models.Model):
    #...
    groups = ManyToManyField('Group')
class Group(models.Model):
    #...


If you use admin or just manually create a form from the model, you will get a widget to edit M2M just for User. And it's normal since according to docs "By default, admin widgets for many-to-many relations will be displayed inline on whichever model contains the actual reference to the ManyToManyField". But in some rare cases you might need widgets be displayed on both models (some people think it's abnormal in common and some think you shouldn't tune your models for such tasks, but forms instead).

Since I was a newbie I had written:

class User(models.Model):
    #...
    groups = ManyToManyField('Group', related_name='groups')
class Group(models.Model):
    #...
    users = ManyToManyField(User, related_name='users')


And before I looked into database I thought I got expected result... Yes, django has created two different tables for two relations: User_Group and Group_User. And it's pretty logical for models.

Then you can try:


class User(models.Model):
    #...
    groups = ManyToManyField('Group', related_name='groups',
            db_table=u'USERS_TO_GROUPS')
class Group(models.Model):
    #...
    users = ManyToManyField(User, related_name='users',
            db_table=u'USERS_TO_GROUPS')


If you use it with existing db then it will be fine, but if you try syncdb it will try to create two identical tables. So we came to the final snippet:

class ManyToManyField_NoSyncdb(models.ManyToManyField):
    def __init__(self, *args, **kwargs):
        super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)
        self.creates_table = False

class User(models.Model):
    #...
    groups = ManyToManyField('Group',related_name='groups',db_table=u'USERS_TO_GROUPS')
class Group(models.Model):
    #...
    users = ManyToManyField_NoSyncdb(User,related_name='users',db_table=u'USERS_TO_GROUPS')


Here it is: now ManyToManyField widget is displayed on both forms and of cource on admin add/change pages for both models.

In case you use Django Admin you can try to inline intemediary model, but it will be just a kind of a many-to-many relation (no multiselect, but lot of inlines models). In our case it's not a good solution.

Since you work with m-2-m I want to point you to a good snippet for integrating legacy databases: http://www.djangosnippets.org/snippets/962/
In my code I have combined it with my snippet.

Monday, November 17, 2008

httpd library for embedding into your application

Next week I will have to implement something to transfer files between computers. I decided that http transfer could be a good idea, because clients would be able to get files from another sources and not from my server-application only.
So here is libmicrohttpd by GNU. It supports SSL/TLS, and licenced under LGPL (which is important for my company, since they don't know what licence will use). Looks like very useful and easy to use library.
FOSS rocks!

Tuesday, October 28, 2008

"Antitrust" movie and do THEY reuse free software in proprietary?

Just finished watching Antitrust movie (aka hackers 3). It's really amazing and it's worth to watch this movie. I remember there was similar story about lawyer and big company (maybe even two stories), but this one is about software development: you can see Unix systems running on movie's computers, Open Source hackers, and M$ (Titled "Nurd" in the movie).

But it's really interesting how much open sourced code is stolen by proprietary organizations? I'm not sure if there are special tools to check binaries (anywhere clean up should kill traces). And the code is just a form of ideas, so somebody can steal ideas and rewrite the code. Just imagine: they have all things to look for: process/memory management (Linux, *BSD, Hurd), filesystems, any multimedia applications, IDEs, browsers... They have all things to look for fresh ideas.
Just think: any professor who works with operating systems would say that he works with theory, and in a real projects most things are always done in another ways. You can have a cute design, emulators, whatever you want, but seeing the result of development is always better, than designing and analyzing. And what abilities do proprietary companies have?
They have an ability to analyze all technical decisions come from Open Software. What stops them from reusing it? Yeah, you might say there are some Windows sources. But I didn't heard somebody has built it. Of course they have enough money for reimplementing interesting ideas from open source, so I don't mind somebody porting linux kernel code to windows. And of cource I don't mind them spying on genius developers: just on their repositories ))
But...
And the last one: nobody knows what you can find in proprietary software. All computers (most) are connected to the network, most of them uses windows update service. Do you rememebr "I, robot movie"? What if all MS windows copies die tomorrow?..
It's obvious that MS impact on the WORLD is too huge. Much huger, than it should be (please, tell me: what idea did they produce? Just C#? And look on unix impact on technologies from 1970). Ok, they really have some gold ideas, but not revolutionary. Google (I like them, their sevices and they paid me $4500, presented a book and were very kind) has a big impact on the internet. Now we have two corporations are controlling more than 80% of users: do you use Microsoft Windows to run google services? ;)
Please don't tell that they have pretty buttons: open source sometimes lucks of usability, but not interesting technologies.
This movie made me a bit paranoiac and maybe it was better to use very rare (these days) spare time for coding few lines...

Saturday, October 25, 2008

Russian secret helicopter forces





Secret planes engines are here: http://pilot.strizhi.info/2008/10/14/5743
Enjoy :D

Friday, October 17, 2008

Unix commands pattern )

I was reading advance bash scripting today, when my friend Void pointed me to a strange pattern in the naming of unix commands:

gawk, talk, date, wine, touch, unzip, strip, finger, mount, fsck, yes, more, umount, make clean, sleep

Cewl!

By the way (from Void): funny man pages!