What's new in Django community blogs?

Handling images as base64 strings with Django REST Framework

Jul 04 2015 [Archived Version] □ Published at Matthew Daly's Blog

I’m currently working on a Phonegap app that involves taking pictures and uploading them via a REST API. I’ve done this before, and I found at that time that the best way to do so was to fetch the image as a base-64 encoded string and push that up, rather than the image file itself. However, the last time I did so, I was using Tastypie to build the API, and I’ve since switched over to Django REST Framework as my API toolkit of choice.

It didn’t take long to find this gist giving details of how to do so, but it didn’t work as is, partly because I was using Python 3, and partly because the from_native method has gone as at Django REST Framework 3.0. It was, however, straightforward to adapt it to work. Here’s my solution:

import base64, uuid
from django.core.files.base import ContentFile
from rest_framework import serializers
# Custom image field - handles base 64 encoded images
class Base64ImageField(serializers.ImageField):
def to_internal_value(self, data):
if isinstance(data, str) and data.startswith('data:image'):
# base64 encoded image - decode
format, imgstr = data.split(';base64,') # format ~= data:image/X,
ext = format.split('/')[-1] # guess file extension
id = uuid.uuid4()
data = ContentFile(base64.b64decode(imgstr), name = id.urn[9:] + '.' + ext)
return super(Base64ImageField, self).to_internal_value(data)

This solution will handle both base 64 encoded strings and image files. Then, just use this field as normal.


django-nyt

Jul 03 2015 [Archived Version] □ Published at Latest Django packages added

Notification system for Django with batteries included: Email digests, user settings, JSON API


Django Rest Multiple Models

Jul 02 2015 [Archived Version] □ Published at Latest Django packages added

View (and mixin) for serializing multiple models or querysets in Django Rest Framework


Django Translation Manager

Jul 02 2015 [Archived Version] □ Published at Latest Django packages added


Einladung zur Django-UserGroup Hamburg am 08. Juli

Jul 01 2015 [Archived Version] □ Published at Arnes Blog under tags  django dughh hamburg python usergroup

Das nächste Treffen der Django-UserGroup Hamburg findet am Mittwoch, den 08.07.2015 um 19:30 statt. Achtung: Neue Location! Dieses Mal treffen wir uns in den Räumen der Smaato Inc., Valentinskamp 70, Emporio 19. Stock in 20355 Hamburg.

Auf diesem Treffen gibt es einen Vortrag über TDD für APIs von Michael Kuehne.

Bitte seid um ca. 19:20 unten im Foyer, wir fahren dann gemeinsam in den 19. Stock. Wer später kommt, sagt bitte am Empfang bescheid, dass er zur Smaato Inc möchte und wird dann hoch gelassen.

Eingeladen ist wie immer jeder der Interesse hat sich mit anderen Djangonauten auszutauschen. Eine Anmeldung ist nicht erforderlich, hilft aber bei der Planung.

Weitere Informationen über die UserGroup gibt es auf unserer Webseite www.dughh.de.

Die Organisation der Django-UserGroup Hamburg findet ab jetzt über Meetup statt. Um automatisch über zukünftige Treffen informiert zu werden, werdet bitte Mitglied in unserer Meetup-Gruppe: http://www.meetup.com/django-hh


django-connected

Jun 30 2015 [Archived Version] □ Published at Latest Django packages added


djangocms-forms

Jun 30 2015 [Archived Version] □ Published at Latest Django packages added


djangocms-instagram

Jun 30 2015 [Archived Version] □ Published at Latest Django packages added


djangocms-twitter2

Jun 30 2015 [Archived Version] □ Published at Latest Django packages added


Django Girls Birthday Survey!

Jun 30 2015 [Archived Version] □ Published at Django Girls Blog

In a three weeks Django Girls will be one year old. We honestly can’t believe it! From a one-time workshop we grew to a global movement and the Django Girls community grew incredibly in last months.

We have some crazy plans regarding Django Girls Birthday celebrations, but what matters to us right now the most is to know what you think about Django Girls.

We’ve created a survey that everybody could answer and give us feedback on what we are doing. If Django Girls changed something in your life, if you learned something or you like the community around it - let us know. No matter if you were an attendee, organizer, contributor, volunteer, sponsor, speaker or you went through the tutorial on your own at home - we appreciate your input. It will keep us going for the next year!

If you know anyone who took part, contributed, coached or did anything related to Django Girls - make sure to send them a link to the survey.

Most of the questions are not required, so you can answer only those you are comfortable with.

We’d love to hear from you! Share your story with us!

And remember: you are awesome and your voice matters!

Answer the Django Girls Birthday Survey


django-rest-framework-social-oauth2

Jun 30 2015 [Archived Version] □ Published at Latest Django packages added


Creating a Web App From Scratch Using Python Flask and MySQL: Part 8

Jun 30 2015 [Archived Version] □ Published at tuts+

In the previous part of this tutorial series, we populated the dashboard page of our application with the wishes created by different users. We also attached a like button to each wish so that a user could like a particular wish.

In this part of the series, we'll see how to toggle the like/unlike display and show the total number of likes received by a particular wish.

Getting Started

Let's start by cloning the previous part of the tutorial from GitHub.

Once the source code has been cloned, navigate to the project directory and start the web server. 

Point your browser to http://localhost:5002/ and you should have the application running.

Adding a Like Count

We'll start by implementing a feature to show the total number of counts a particular wish has garnered. When a new wish gets added, we'll make an entry into the tbl_likes table. So modify the MySQL stored procedure sp_addWish to add an entry into the tbl_likes table. 

As seen in the above stored procedure code, after inserting the wish into the tbl_wish table, we fetched the last inserted ID and inserted the data into tbl_likes table.

Next, we need to modify the sp_GetAllWishes stored procedure to include the number of likes each wish has garnered. We'll make use of a MySQL function to get the total number of wishes. So create a function called getSum which will take the wish ID and return the total number of likes.

Now, call the above MySQL function called getSum in the stored procedure sp_GetAllWishes to get the total number of likes for each wish.

Modify the getAllWishes Python method to include the like count. While iterating the result returned from the MySQL stored procedure, include the like field as shown:

Modify the CreateThumb JavaScript method to create an additional span which we'll use to display the like count.

And append the likeSpan to the parent paragraph p. Here is the modified CreateThumb JavaScript function.

Include the like parameter while calling the CreateThumb JavaScript function from the success callback of the jQuery AJAX call to /getAllWishes.

Save the changes and restart the server. Once signed in to the application you should be able see the like count corresponding to each of the wishes.

Dashboard With Like

Show If a Wish Is Liked

Seeing the likes under each wish, it isn't very clear whether the logged-in user has liked the wish or not. So we'll show a proper message like You & 20 Others. In order to implement that, we need to modify our sp_GetAllWishes to include a bit of code indicating whether the logged-in user has liked a particular wish or not. To check if a wish has been liked, we make a function call. Create a function called hasLiked which takes in user ID and wish ID as the parameters and returns whether the wish has been liked by the user or not.

Now call the above MySQL function hasLiked inside sp_GetAllWishes to return an extra field in the returned data set indicating the user like status.

Open app.py and modify the call to the MySQL stored procedure sp_GetAllWishes to include the user ID as a parameter.

Now modify the getAllWishes method to include the like status of the user for a particular wish. Modify the code to include HasLiked in the created dictionary.

Inside the CreateThumb JavaScript function, we'll check for HasLiked and add the HTML accordingly.

As seen in the above code, we are showing the like count if the user has not liked a particular wish. If the user has liked the wish we are showing a more descriptive message.

Dashboard With Like Status

Refreshing the Like Count

At the moment when we click on the like button, the like status gets updated in the database, but doesn't change in the dashboard. So let's update it in the success callback of the AJAX call on the like button click.

We'll start by making a change in the MySQL stored procedure sp_AddUpdateLikes. Earlier we were passing in the like status, 1 for a like and 0 for unlike. We'll modify that and toggle the like/unlike in the stored procedure. Open sp_AddUpdateLikes and select the like status into a variable and check the variable status. If the variable status is a like, we'll update the status to unlike and vice versa. Here is the modified sp_AddUpdateLikes stored procedure.

In the CreateThumb JavaScript function, assign an ID to the likeSpan that we created earlier, so that we can update the status as required.

Open up app.py. Inside the addUpdateLike method, once the data has been updated successfully, we'll fetch the wish like count and status using another stored procedure call. So create a MySQL stored procedure called sp_getLikeStatus. Inside sp_getLikeStatus we'll be calling the already created MySQL functions getSum and hasLiked to get the status.

Once a call to sp_AddUpdateLikes from the Python method addUpdateLike has been made, close the cursor and connection.

Now make a call to the stored procedure sp_getLikeStatus.

Return the like count and like status along with the response.

In dashboard.html, in the success callback of the AJAX call made to the addUpdateLike method, parse the returned response and based on the like status show the like count.

Save the changes, restart the server, and sign in using valid credentials. Once on the dashboard page, try to like a particular wish, and see how the like status gets updated accordingly.

Wrapping It Up

In this part of the series, we implemented the like/unlike functionality for the wishes displayed in the dashboard page. In the coming parts of the series, we'll implement some more new features in the application and refine some of the existing features.

Do let us know your thoughts and suggestions, or any corrections, in the comments below. Source code from this tutorial is available on GitHub.


Observations on the nature of time. And javascript.

Jun 30 2015 [Archived Version] □ Published at Isotoma Blog under tags  debugging django javascript

In the course of working on one of our latest projects, I picked up an innocuous looking ticket that said: “Date pickers reset to empty on form submission”. “Easy”, I thought. It’s just the values being lost somewhere in form validation.And then I saw the ‘in Firefox and IE’ description. Shouldn’t be too hard, it’ll […]


Security advisory: simple_tag does not do auto-escaping

Jun 29 2015 [Archived Version] □ Published at The Django weblog

As per our documentation, the simple_tag decorator used for creating custom template tags does not run auto-escaping on its contents (up to and including Django 1.8). The team has noticed, however, that this makes it very easy to introduce XSS vulnerabilities when using simple_tag, and we have found examples of vulnerable code in the wild.

For this reason, Django 1.9 will change this behavior to improve security. In the mean time, all users are encouraged to check every usage of simple_tag in their own template tags and ensure they are not vulnerable, as per the instructions in the 1.9 release notes.


django-su

Jun 29 2015 [Archived Version] □ Published at Latest Django packages added


django-planet aggregates posts from Django-related blogs. It is not affiliated with or endorsed by the Django Project.

Social Sharing

Feeds

Tag cloud

admin administration adsense advanced ajax angularjs apache api app appengine app engine apple application security aprendiendo python architecture articles asides audrey authentication automation backup bash basics binary bitbucket blog blog action day blogging book books buildout business cache capoeira celery celerycam celerycrawler challenges chat cherokee choices class-based-views cliff cloud cms code codeship coding command community computer configuration continuous deployment continuous integration couchdb coverage css custom d data database databases db debian debugging deploy deployment deployment academy design developers development devops digitalocean django django1.7 djangocon django-nose django-readonly-site django-rest-framework django-tagging django templates django web framework tutorials documentation dojango dojo dotcloud dreamhost dughh easy_install eclipse education elasticsearch email encoding english error events extensions fabric facebook family fashiolista fedora field file filter fix flash flask form forms frameworks friends fun gae gallery games geek general gentoo gis git github gmail gnome google google app engine guides gunicorn hack hackathon hacking hamburg haskell heroku holidays hosting howto how-to how-tos html http i18n image imaging indifex install installation intermediate internet ios iphone java javascript jobs journalism jquery json justmigrated linear regression linkedin linode linux login mac machine learning mac os x math memcached mercurial meta migration mirror misc model models mod_wsgi mongodb months mozilla multi-language mvc mysql nelenschuurmans newforms news nginx nodejs nosql ogólne openshift opensource open source open-source orm osx os x ottawa paas patterns pelican performance personal philosophy php pi pil pinax pip piston planet plone plugin pony postgis postgres postgresql ppoftw presentation private programmieren programming programming & internet project projects pycon pygrunn pyladies pypi pypy python python3 quick tips quora rabbitmq rails rant ratnadeep debnath redis refactor release request resolutions rest reusable app review rtnpro ruby scala scaling science screencast script scripting security server setup simple smiley snaking software software collections software development south sphinx sql ssh ssl static storage supervisor support svn sysadmin tag tag cloud talk nerdy to me tastypie tdd techblog technical technology template templates template tags test testing tests tip tools tornado transifex travel tumbles tutorial tutorials twisted twitter twoscoops typo3 ubuntu uncategorized unicode unittest unix user authentication usergroup uwsgi uxebu virtualenv virtualenvwrapper web web 2.0 web application web design & development webdev web development webfaction whoosh windows wordpress work workshop yada znc