Saturday, 20 September 2014

Accessing dictionary values from a list of dictionaries in django... Wait . What?

This post deals with a question that has already been answered on stackoverflow here . I had to google  a lot, trying to phrase the exact words that would lead to this result. So, I am explaining the answer here as well( apart from the link provided) for myself and the others who find this blog before facing that problem (or it's solution).


I was in a situation in which i had to display the details of some users on the admin page of my django app. The list of users can be obtained in 1 line :

                     users.objects.filter(#myfiltercriteria)

The queryset returned can be transformed to a list of dictionaries with :

                    users.objects.filter(#myfiltercriteria).values()


Now the problem was how to retrieve these values. The simpler way is to retrieving the required values in the view itself and compile it in a list and then use the built in template tag of django for list to access the elements. 

Okay, I maybe wrong depending on the use case. (I was wrong in my case, as I had to retrieve the SAME set of values for all the users.) 

The better and efficient way is to make custom template tags. For the sake of better explanation, let us access the name attribute of the users dictionary passed to a template. 

   So, the custom filter tag is (credits :culebron )
                 
                    @register.filter
                    def get_item(dictionary, key):
                    return dictionary.get(key)

So, inside your template, you would access the names like this :

                   for user in users:
                       {{ user|get_item:item.name }}

This is extremely useful and neat but is not offered by django by default.

For a reference to see how custom template tags are made see this


Thursday, 4 September 2014

Always check the error log

5 hours trying to debug a very simple problem.. I've learned my lesson.

Always check the error log

I cannot emphasize this point more, it can save you lots of time. That's why it is there!

So, I made a web app in Codeigniter framework. All the development took place on my windows computer. Everything went perfect, web app fully functional in on localhost. Then i decided to launch it, on a Linux server (Ubuntu to be precise). I configured the database settings and started the server. The web app uses a login on its first page itself . It displayed the form correctly. I entered the login id and password only to be met b a blank page .I have been greeted many a times by 404 pages, but this was a new guest. A frequent user of web developer console, I quickly found out that it was a 500 Internal Server Problem. After a google search i found that it happens because of some programming  issue on the page/site. This was the first time I thought google was wrog, and this thougt was not baseless.. the same app was running perfectly on my PC. So , i searched here and there and everywhere (:P) . Tried everything , no perfect solution.

Then i posted this question on Stackoverflow here . What i need was just 1 comment (thanks @Prix) .
My error log just threw the syntax error on my face. I was bewildered . Why?
Again google. I found that the code i had written took help of a feature that had been introduced in PHP 5.4 while my server was running PHP 5.2 . The feature is called  function array dereferencing. It means accessing the array values at a particular index, the array being returned by a function. It meant that i had to use a temporary variable to store the returned value of the array ad then access array index from it .

Obviously the other solution was to use PHP 5.4 But to my dismay, the truth was that PHP 5.4 repos are not available for ubuntu 12.04. So i followed the instructions  here . However it did not work and i am still using php 5.2, but without issues.

Friday, 22 August 2014

So, the delay has a reason! Windows password check

A large portion of the world population use Microsoft Windows  as their Operating System. To all those people, I ask..

Have you ever wondered why it takes a long time to display a error message when you enter a wrong login password whereas if you password is correct, you login in a reasonably smaller amount of time ?
         Most people think that it is their system fault , a slow processor( as if how big data crunching is going on :P).

The reason is so simple and intelligent .
The 2 major concepts are Password caching  and preventing Brute Force Attacks
 It is better explained in the microsoft blog . Go ahead and satisfy yourselves .:)

Tuesday, 19 August 2014

Facebook Bug? Maybe... Maybe Not

Recently, I was fiddling around with things and found something which according to me seems a bug, but is not according to the Facebook .

So what I found that, the visibility/ privacy settings as in this picture can be overruled.
fb privacy


I have tried reporting this to the Facebook team and according to them this is no security bug, so I can safely post my report over here.







So, I was using the developer tools provided in Google Chrome.  I asked my friend to change privacy setting of his profile picture such that all except me could see his image. Note: while a profile picture is visible to all when someone visits a person's profile, they only see the cropped portion of the profile picture. Only the people granted access can click on it to see the complete picture. Of course, this is what facebook claims or maybe we have deduced it incorrectly.

       I used the DOM inspector to find the unique url of the profile picture as in the picture below


So, the unique url is the portion that i have scribbled with blank (:P) .

I copied this url. Now, I went to my own account, and clicked on the profile picture. Again using the DOM inspector, I found the common url. It turned out to be this (the part which is not scribbled):
I then replaced the scribbled part of this url with the scribbled part of the previous image.

 And the result was, I could see the profile picture of my friend, not only the cropped part, the whole picture. Also going into incognito mode, I could still see it.


So according to me, this is incorrect and as mentioned above I reported this to facebook twice. But it seems I am wrong.. I'd like to hear your comments on this :)

EDIT:
Facebook offers an API to see the images as in here . However, the point to be noted there is :

Permissions

  • Any valid access token for any photo with public privacy settings.
  • For any photos uploaded by someone, and any photos in which they have been tagged - A user access token for that person with user_photos permission.

Wednesday, 13 August 2014

Using BigDump

 I recently was in a situation when I had to push code for a new website to my server. Simple enough, except that I could not access the phpmyadmin by remote or local method. (Local method means accessing phpmyadmin installed on the hosting server itself, whereas remote method means using phpmyadmin on your own system ,for eg. i had it installed as part of wamp, to access the mysql database of remote server.)

There were large queries to be made and i didn't want to type it all on the console. It was at this time that i came across bigdump script . What it actually does , is that it excutes your mysql dump( usually generated by an export of your database). That is, it imports a database to the server.It is a very good script made specially for problems like this .

The steps involved are as follows,

  1. Create a folder(let its name be dump).
  2. Transfer the bigdump.php and the dumped sql file in that folder.
  3. On your browser, enter the path to the script.
  4. The script will ask your permission to run the queries in database.

The script itself is customizable. Few things to be kept it mind,
  1. You need to change database login credentials in the script.
  2. If the dump contains CREATE DATABASE  query, we need to uncomment a line in the script.

With these things in mind, bigdump turns out to be a very useful tool

Wednesday, 9 July 2014

The easy way to deal with dates in PHP

One of the good things that PHP versions 5.1 and above come with is the utility-


 date_default_timezone_set('timezone');

What it means is that whenever you will use the date() function, it will take as reference the "timezone" you specified.

For a list of timezones , click here.

Once this is done, call date() with the necessary formatting parameters, for example, the format accepted by MySQL i.e, yyyy-mm-dd .You get it by,
date('Y-m-d')


If you want to know what that means,click here .

With this procedure you are ready to use dates easily !!

Sunday, 25 May 2014

PDO -> The PHP Database Object

The PDO represents a connection between PHP and the Database Server.

Given the word object  in the title, we know that to use PDO, we need to instantiate it first.

From the PHP manual , I got :

public PDO::__construct ( string $dsn [, string $username [, string $password [, array $options ]]] )

or you can use

$pdo = new PDO(string $dsn [, string $username [, string $password [, array $options ]]) ;

The PHP manual says that DSN  (Data Source Name) contains the information required to connect to a database.

In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax

I use mysql , so in my case, it will be 

"mysql:host=$databasehost;dbname=$databasename"
 
The username and password are clear.
 
Options :A key=>value array of driver-specific connection options.
       Ref :https://php.net/manual/en/ref.pdo-mysql.php