2016 (old posts, page 1)

Django Tips & Tricks #6 - Make Deleting Easy In Admin

Lets say we have a model as shown in some project.
from django.db import models

class Resource(models.Model):
Model to hold resources for books.
book = models.ForeignKey('Book')
book_type = models.IntegerField()
url = models.URLField()
This model can be registered in admin as follows.
from django.contrib import admin

from . import models

class ResourceAdmin(admin.ModelAdmin):
list_display = ('book', 'book_type', 'url')

admin.site.register(models.Resource, ResourceAdmin)
This will generate a nice admin interface to modify the data.
Now if we want to delete a record, we need to follow these steps.
  • First, we have to select that record
  • Next, we have to click on the action dropdown
  • Next, we have to select delete action
  • Next, we have to click Go button.
  • In the next page we have to confirm that we have to delete.
Just to delete a single record, we have to click 5 times. That's too many clicks for a single action.
To simplify the process, we can have simple delete button for the objects as shown. This can be achieved by writing a function which will insert delete button for every record.
from django.contrib import admin

from . import models

class ResourceAdmin(admin.ModelAdmin):
def delete(self, obj):
return '<input type="button" value="Delete" onclick="location.href=\'%s/delete/\'" />'.format(obj.pk)

delete.allow_tags = True
delete.short_description = 'Delete object'

list_display = ('book', 'book_type', 'url', 'delete')

Now we have an admin with delete button for the records.
To delete an object, just click on delete button and then confirm to delete it. Now, we are deleting objects with just 2 clicks.

Read more django tips.
Read more posts about django.

Auto Convert & Upload Books To Kindle

I have ebooks in various formats like pdf, epub, mobi e.t.c. If I want to read books in laptop, I could use ebook reader like Calibre which can open supports most of the formats.
If I have to read them on Kindle paperwhite, it becomes a problem as it supports only few formats. So whenever I get an ebook, I have to convert it to mobi format and send it to kindle.
I can connect kindle to laptop via USB cable and I can copy books. I can also send books to kindle via email.
I found an IFTTT recipe, to automate all this process. I just need to create a folder to store all the books in dropbox. Then use IFTTT recipe to link dropbox folder which has books and gmail to send books to kindle email.
Now when I move required books into dropbox folder in my laptop, they will automatically appear in my kinde.

Automate Boring Stuff - Accepting Waitlist RSVPs

Meetup.com makes it easier to organize events. Before meetup you can specify how many people you can accomodate. Then you send mail to all people so that interested people can RSVP.
If RSVP is full, people can join waitlist. So if somebody changes RSVP to No, we can accomodate people in waitlist.
On the otherhand, if you find a place where you can accomodate more number than you have planned, you can accept all people in waitlist.
Unfortunately, there is no option in meetup for that. You have to accept people one by one which is a boring job if there are ~50 people in waitlist.
This where Python comes to rescue. Python has an excellent package called pyautogui which helps you to automate all boring tasks.
Here is the simple script I have used for this task.
import time

import pyautogui

print('Goto meetup event page and place cursor...')

x, y = pyautogui.position()

for i in range(100):
pyautogui.click(x, y)
The code is self explanatory. First we are importing pyautogui and then wait for 5 seconds so that we can go to meetup page and point mouse correctly. Get the cursor position and start clicking on that position with a gap of 5 seconds. This gap is needed as meetup shows a notification once you accept  RSVP.
Pyautogui is an interesting package which makes your boring job easier.
Just found that increasing Attendee limit auto changes RSVP of waitlist people to Yes.

Read more articles about Python!