Chaotic Notes About Python and Django

python django Chaotic Notes About Python and DjangoI have accumulated a few small notes / tips about the python and django, which in some topics do not draw, so published all at once.

Django: simplify vyuh code

The documentation and training examples for django usually write something like this:

def contact (request):
if request.method == ‘POST’:
form = ContactForm (request.POST)
if form.is_valid ():
# Process the data. For example, do form.save ()
# …
return HttpResponseRedirect (‘/ thanks /’) # after a POST-request to redirect
else:
form = ContactForm ()

return render_to_response (‘contact.html’, (
‘Form’: form,
), Context_instance = RequestContext (request))

Perhaps this is correct – so explain to people better understand what was happening.

Chips:

* Always use django.shortcuts.redirect for redirects. It knows the names of reversion URLs cause get_absolute_url or simply redirect to a URL
* Use django.views.generic.simple.direct_to_template instead render_to_response. They are doing almost the same thing, but direct_to_template use RequestContext instead of Context, which is already required in most cases. Instead direct_to_template can use decorator render_to of django-annoying, but it is a matter of taste, as you like.

Django: Draw graphics

In the article, the admin has promised to tell you about graphics, but still could not reach his hands, badly turned out. Yes, and tell something there is nothing special, all too simple and “stupid” – schedules are drawn through the google charts. In this case, you can do without libraries: design currently schedule to taste here (it’s semi-official tool from Google, it has a link with information on the google api charts), and then insert the resulting string into the template and substitute the variables in place of the test values.

There is a very thin wrapper over the google charts: django-chart-tools. Essence – is the same: to collect chart visually and replace the variables, just to django-chart-tools such charts are easier to maintain.

Data sampling can be done simply by django ORM, or for the convenience / speed through the django-qsstats-magic, depending on the task.

As a result (using django-chart-tools and django-qsstats-magic) users schedule on days can derive something like this:

# Original data
qs = User.objects.filter (is_active = True)
end = datetime.today ()
start = end-timedelta (days = 30)

# Prepare the data for the chart
data = QuerySetStats (qs, ‘date_joined’). time_series (start, end)
values = [t [1] for t in data]
captions = [t [0]. day for t in data]

then the variable values and captions convey to the template, and then derive the schedule as follows:

(% Load chart_tags%)
(% Bar_chart values captions «580×100»%)

Restrictions on the number of appeals from google, charts image api is not there just ask to contact them if 200tys hits per day will be that they have distributed the load. So that such graphs can not only use the admin panel.

Django: Tests

Use django-webtest for writing tests. I have wrote about this application in past, but since that time there has not been very important change: django-webtest now provides access to the context of patterns (in the same way as the standard test client).

Now you can write in this style:

# …
response = page.forms ['my-form-id']. submit (). follow ()
assert response.context ['user'] == self.user

works well as a standard assertTemplateUsed.

django-webtest is better than any integration with twill, because they do not have access to context templates and full support for Unicode, and will not develop.

django-webtest better than the standard test client, as provides a simple API (just try zasabmitit form with default values through the standard test-client). With a standard test client also can not test the absence csrf-token (or a very roundabout puyami black), and django-webtest is done is trivial (and even automatically). Use django-webtest)

There would pop up a sign with features done: the django-webtest will be everywhere the green check mark and a twill and a standard test client – red here and there.

Useful suggestions appreciated.

Facebook Virus: A Real Danger

Facebook Virus war Facebook Virus: A Real Danger

Part of the success of Facebook is its style of growth, similar to a virus under which I like and my friends see, and in turn, many of them also press the little button “I like” … and so on to infinity .

The dark side of this system is the ease to propagate computer dangers that are taking advantage of the confidence we have in our network. The same have antivirus and a long warnings of the risks inherent in social networks.

As the real world, Facebook also has its risks: over-confidence can cost you dearly.

In this article we will explain some of the most important hazards and how to avoid them.

Clickjacking: oh no, I liked!

facebook box Facebook Virus: A Real Danger

I have seen “chunks” of favorite pages in Facebook: news feeds, comments and, above all, buttons “I like it.” These plugins are easy to create and are socially embedded, they are part of the promotion strategy of the pages of Facebook.

But what would happen if you hit Like a page full of advertising and malware, possibly fooled by the design of it? Or if someone used the appearance of the Facebook button to trigger some harmful event? You would be falling into what experts called “click jacking“.

clickjack Facebook Virus: A Real Danger

One of the most notorious case was the image above, to which it came from a curious picture posted on the wall of other friends. The user was asked to find and press the blue button, the “Share” on Facebook. Unintentionally, those who did were strikingly marketing the page.

Another example of click jacking

hot Facebook Virus: A Real Danger
Thanks to an invisible button, this page “infected” the walls with a non-desired news.

In this case it was just advertising, but imagine for a moment what would happen if instead of a video or a banner, it had a virus. It is the possibility that has motivated perhaps alarmist articles believable: Sophos and Panda speaks Labs likejacking warning about the risks of compulsive clicking.

fbsecy Facebook Virus: A Real DangerTips for Using Facebook securely

In practice, how do you protect one of these traps? The combination of a secure browser extensions like NoScript or prevent accidental infection if you click where it touches. Many antivirus, in turn, recognize and clickjacking techniques as Troj / Iframe-ET or Troj / Iframe.P. But the best solution is still common sense:

1. Do not click on an image or link without having an idea about what it is.
2. Do not add applications randomly, some are spread by tricks.
3. Set the privacy of Facebook (we will explain how to do this in another article).
4. Be careful who you add as a friend or what you do with your fan page.
5. Check your surrounding regularly to remove unwanted activity. Remove wall news.
6. Perform regular cleaning from the Account menu > Edit friends : Clear message
7. Disregard chain letters, rumors or mass invitations : Do not panic

These are recent problems with Facebook Security, but social networks like Facebook are much more secure than it seems. Most of the “dangers and problems, in fact, have more to do with privacy and the use of the tools made and there is nothing that can not be resolved without some common sense.

To be aware of the risks, we recommend adding Facebook Security to your favorite pages. It contains valuable help of Facebook privacy and security recommendations. As always, you apply them or not depends on your day to day life.

And you?

Have you suffered a security problem in Facebook?

How to Set Up Your Own Proxy Server

set your own proxy server How to Set Up Your Own Proxy ServerA lot of surfers who use Proxy Servers frequently complain that they do not work at their school/office computers because their Web proxies are blocked by the system administrator.

A way of circumventing this is to make your own proxy server. Your proxy server should be hosted by a Web hosting service provider that maintains CGI or PHP at no additional fees.

Before you begin, you have to download a copy of cgiproxy and phpproxy.

To set up cgiproxy, your web hoster should be cgi enabled. Some free hosters do not provide cgi. A little set of preinstalled scripts will also be helpful. You should see to it that the hoster is enabled before you begin installing.

Afterwards, download cgiproxy and unload it to a local directory. Once you have opened the file, you can modify a lot of settings. For instance, you could set up the script in a manner where it would only enable text to go through the proxy and no image will be displayed.

Flick through the file. Change the options and save the new file. Next, upload the script to the directory of your cgi.

If you open your browser, you can then surf the net anonymously. To validate that you really are anonymous while browsing, open a website like whatismyip.com and try to see if the IP is same with the installed server script and not the IP of your computer. In this case, you have done it right and can browse the net without worrying about your privacy.

To set up phpproxy, you must first download it. Next, unload it to a home index in the hard drive. What you should do next is to upload the script to your webspace. Afterwards, open the new URL and check if it’s doing right.

Some filters might forbid files that contain ‘proxy’ in the filename. To stay away from these kind of filters, you need to rename your file.

When you open your script, just enter the IP of your client to ascertain that it is your client alone who will be able to hook up or could append a .htaccess file to the directory. This will oblige all else who desire to begin the script to type in a username and a password.