Tuesday, 29 October 2013

Tagged under: , , , , , , , , , , , , , , ,

COOL PYTHON BLOGS TO FOLLOW

Ok so here is the list of Python blogs that I have been following since the time I started learning python. I have found out some pretty amazing stuff here. So, I guess a list of them would be helpful for those searching for some good resources:


          This is the official blog of Guido van Rossum (creator of Python, not that this mention was needed).


          This blog presents you with a tour of the Python standard library through short examples which is           pretty good for begginers. Well, ofcourse there is Python Docs to serve this purpose but it has               some nice explanations too.

          This one is not a blog in itself but has link to all nice and essential python blogs to explore.


          This is the blog of Armin Ronacher: Writer of the Flask and Werkzeug frameworks.

          It is a series of articles on the history of the Python programming language and its community.
          (It was started by Guido van Rossum)

Monday, 28 October 2013

Tagged under: , , , , , , , ,

ENEWSPAPER DOWNLOAD SCRIPT(PYTHON) [COOL PYTHON SCRIPTS]



Here is a simple script that took me about 30 minutes to write which can download the Hindi newspaper Dainik Jagran in pdf format. The code is a bit messy but it does the work. Here is the code:

import os
from pyPdf import PdfFileWriter, PdfFileReader
import urllib2
import shutil

def make_date():
 from datetime import date, timedelta
 today = str(date.today())
 yesterday = date.today() - timedelta(1)
 yesterday = yesterday.day
 if yesterday in range(10):
  yesterday = '0'+str(yesterday)
 else:
  yesterday = str(yesterday)
 return ''.join(reversed(today.split('-'))), yesterday

def create_dir(city):
 directory = 'C:\Users\Agnes\Desktop\' + city
 if os.path.exists(directory):
  shutil.rmtree(directory)
 os.makedirs(directory)
 os.chdir(directory)

def make_pages(city, url_date, url_val, city_short, page):
 continue_loop = True
 output = PdfFileWriter()
 while continue_loop:
  url = "http://epaper.jagran.com/epaperimages/" + url_date + "/" + city + "/" + url_val + city_short +"-pg"+ str(page) +"-0.pdf"
  print url
  try:
   request = urllib2.urlopen(url)
   print("Downloading %s page number %s \n" % (city, page))
   data = request.read()
   FILE = open('Page-' + str(page) + '.pdf', "wb")
   FILE.write(data)
   FILE.close()
   input1 = PdfFileReader(file("Page-"+ str(page)+".pdf", "rb"))
   output.addPage(input1.getPage(0))
  except urllib2.HTTPError, err:
   if err.code == 404:
    continue_loop = False
  page +=1
 outputStream = file("Full-Paper.pdf", "wb")
 output.write(outputStream)
 outputStream.close()
 print("Download for %s completed!!!\n\n\n" % city)


def make_pdf(city):  
 make_pages(city, date, day) 
 
 
########################## Execution ##################
date, day =  make_date()

create_dir("Delhi")
make_pages("delhi", date, day, 'del', 1)
create_dir("Jhajjar")
make_pages("panipat", date, day, 'jhr', 1)
It can download any city edition provided by the website. All you need to do is replace the city name in "make_page" function at the end to the the desired city name. The 4th parameter of the function needs to be setaccording to url city code. The 5th parameter takes the page no. argument which can be set to any page.

The code makes use of additional module that doesn't come within python libraries which is pyPdf to save the PDFs.

This code is Windows OS specific but can used on any other platforms easily by changing the directory paths in the code.


Sunday, 27 October 2013

Tagged under: , , , , , , , ,

MY FAVORITE RICH TEXT EDITORS


So recently I have been trying to add a Rich Text Editor to my Django-powered project. I tried and tested the vast majority of editors available. Here are the ones that are easy to use and I liked the most:


  1. TinyMCE
  2. It is free, opensource and has an active community for its maintenance. This is how it looks.
    To include it it in your project all you need to do is add this to your template:
<html>
<head><!-- CDN hosted by Cachefly -->
<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
        tinymce.init({selector:'textarea'});
</script>
</head>
<body>
        <textarea>Your content here.</textarea>
</body>
</html>
TinyMCE comes with many free plugin which you can use to customise your editor.

Pros:

  • Opensource and free
  • Large Community
  • Easy to use

Cons:

  • The only problem with TinyMCE is to write your own handler for file/image uploads which can be quite frustrating and time consuming if you are new to it.
Also, if you are looking for an app that already does the all the work for you then here it is.


2. The next one to make it to the list is Redactor.
This is how it looks:


The best possible way to use it according to me is with this django-app. It does all the work for you from adding redactor your project to file handling and much more. All you need to do is download and add it your INSTALLED_APPS setting.
Pros:

  • Beautiful and complete. One stop for all your needs.

Cons:

  • Not FREE


Tagged under: , , , , ,

DJANGO-TAGGING TUTORIAL



In this post I write a simple django app which uses the django-tagging app. First you need to install django-tagging from this link or you can install it with pip installer using:

pip install django-tagging

Next add 'tagging' to INSTALLED_APPS setting in settings.py. To add tags to a your model say 'Entry' in models.py, you'll need to import a custom field type defined in django-tagging, so add following import statement in yout models.py file:

from tagging.fields import TagField

Next, create the Entry model:

from django.db import models
class Entry(models.Model):
   title = models.CharField(max_length=250)
   slug = models.SlugField(unique_for_date='pub_date') 
   body = models.TextField()
   tags = TagField()

This implements the tagging feature. Next you can setup your views and templates.


Tagged under: , , ,

COOL DJANGO APPS TO EXPERIMENT WITH

Its been three months since I started learning Django. Here is one of the  Django-Packages that I have experimented with since then:

 Django-Oscar

Its an full-fledged ecommerce app and is very well documented. Its features include:
  1. Paying for an order with multiple payment sources (e.g., using a bankcard, voucher, gift card and business account).
  2. Complex access control rules governing who can view and order what.
  3. Supporting a hierarchy of customers, sales reps and sales directors - each being able to “masquerade” as their subordinates.
  4. Multi-lingual products and categories.
  5. Digital products.
  6. Dynamically priced products (eg where the price is provided by an external service).
Its easy to setup and comes with a sandbox site and a demo site. 
The only problem I have come across is product images uploading which is caused due to improper settings of 'MEDIA_URL' and 'MEDIA_ROOT'. Also, it may arise because of the wrong installation of Pillow. If PIL is installed in your system before and without pillow then you may encounter this problem. I resolved this with the help of this handy tutorial.
Here are some of the screenshots of the sandbox site: