Monday, August 5, 2013

error: unable to unlink old '' (Permission denied)

Today when I try to pull my updated files from github to local, I encounter this problem.
error: unable to unlink old 'templates/competitions.html' (Permission denied)
After a simple google search for the solution and easily found out that need to change file permission to -rw-rw-r--. However, it doesn't workout, and the error still the same. Do
ls -la
and notice at the top of the output, the permission still the old one. Change the folder's permission to -rw-rw-r-- by using
sudo chgrp www-data .
and problem fixed!

Thursday, July 25, 2013

Generate sitemap using Flask

It's quite easy to generate a sitemap using Flask route map. You can refer this link to see the idea how to generate a sitemap.xml. The code below is my implementation for my website.


def sitemap():
    """Generate sitemap.xml """
    pages = []
    # All pages registed with flask apps
    for rule in app.url_map.iter_rules():
        if "GET" in rule.methods and len(rule.arguments) == 0:
            pages.append(rule.rule)

    sitemap_xml = render_template('sitemap_template.xml', pages=pages)
    response = make_response(sitemap_xml)
    response.headers["Content-Type"] = "application/xml"

    return response

Wednesday, July 24, 2013

Add extra object to Tastypie returned json results

It's really hard to find a straight answer on the Internet. Both page1 and page2 point me to the right direction. Adding alter_list_data_to_serialize(self, request, data) function could make it work.

def alter_list_data_to_serialize(self, request, data):
    # Example iteration through data
    for item in data['object']:
        # Add extra object to it
        item.data['extra'] = 'data you want to add'
    # Return data
    return data
Be caution if you try to add extra data like this: item['extra'] = 'data you want to add', you will get error: "Bundle" objects does not support item assignment. Since item is a bundle type object, you should use Bundle.data to get access to it.

You may curious why don't I use dehydrate(self, bundle) function to do that. Because I don't know how to iterate through the bundle object.

Monday, July 22, 2013

Fetch blogs from Blogger

First, you need to get the Blogger's api-key. login to code.google.com/apis/console and choose Services, then click Blogger API v3 request access. Fill out the request form. Wait until you get the api-key. Once you get the reply email and if Google grant you the API Key, then go back to Google APIs Console, follow the activate instruction in the email. After you activate the service, you will find the API Key in the API Access.
Use python-requests to fetch the data from your blog would be very easy. You can find my implementation on fan-zhang.appspot.com/blog.

Looks like every time when you create the blog, the blogID will show up at the address bar. However, once you publish the blog, the blogID disappear.

How to find blogID after it's published?
I think the easiest way is go to the dashboard and then click Posts, choose the blog you want to get its ID, click Edit. The blogID will show up at the address bar.

Welcome to Fan's blog

I am trying to note down the coding problem I met during my works and study. Hope someone will get benefits from it.



Disqus for blog