Skip to content

Humanizing the time difference ( in django )

django.contrib.humanize is a set of Django template filters that adds human touch to data. It provides naturalday filter that formats date to ‘yesterday’, ‘today’ or ‘tomorrow’ when applicable.

A similar requirement which the humanize pacakge does not address is to display time difference with this human touch. so here is a snippet that does so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django import template
 
register = template.Library()
 
MOMENT = 120    # duration in seconds within which the time difference 
                # will be rendered as 'a moment ago'
 
@register.filter
def naturalTimeDifference(value):
    """
    Finds the difference between the datetime value given and now()
    and returns appropriate humanize form
    """
 
    from datetime import datetime
 
    if isinstance(value, datetime):
        delta = datetime.now() - value
        if delta.days > 6:
            return value.strftime("%b %d")                    # May 15
        if delta.days > 1:
            return value.strftime("%A")                       # Wednesday
        elif delta.days == 1:
            return 'yesterday'                                # yesterday
        elif delta.seconds > 3600:
            return str(delta.seconds / 3600 ) + ' hours ago'  # 3 hours ago
        elif delta.seconds >  MOMENT:
            return str(delta.seconds/60) + ' minutes ago'     # 29 minutes ago
        else:
            return 'a moment ago'                             # a moment ago
        return defaultfilters.date(value)
    else:
        return str(value)

Tagged , , ,

Running Glassfish as a service on CentOS

Here is how you run glassfish as a service on CentOS:

  1. Create a user glassfish (you can call it anything you want) under which Glassfish will run.
    #useradd glassfish
  2. Install glassfish in /home/glassfish.
  3. Create the startup script /etc/init.d/glassifsh for glassfish.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    
    #!/bin/bash
    #
    # glassfish:          Startup script for Glassfish Application Server.
    #
    # chkconfig: 3 80 05
    # description:      Startup script for domain1 of Glassfish Application Server.
     
    GLASSFISH_HOME=/home/glassfish/glassfish;
    export GLASSFISH_HOME
     
    GLASSFISH_OWNER=glassfish;
    export GLASSFISH_OWNER
     
    start() {
            echo -n "Starting Glassfish: "
            echo "Starting Glassfish at `date`" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            su $GLASSFISH_OWNER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            sleep 2
            echo "done"
    }
     
    stop() {
            echo -n "Stopping Glassfish: "
            echo "Stopping Glassfish at `date`" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            su $GLASSFISH_OWNER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            echo "done"
    }
     
    # See how we were called.
    case "$1" in
            start)
                    start
                    ;;
            stop)
                    stop
                    ;;
            restart)
                    stop
                    start
                    ;;
            *)
                    echo $"Usage: glassfish {start|stop|restart}"
                    exit
    esac
  4. Install the service
    #chmod +x /etc/init.d/glassfish
    #chkconfig --add glassfish
    #chkconfig --level 3 glassfish on
  5. Start glassfish.
    #/etc/init.d/glassfish start

Tagged , , ,

Embedding flash object in Facebook Apps (FBML)

Yesterday I was trying to include a flash object in a facebook app using FBML fb:swf tag. The flash object needed to change the url of the page it was running on on a particular event. But since facebook prevents direct script access from flash, this could not be done.

Here is a simple workaround:

1. Create a simple html page containing only the flash object that you want to include.

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   </head>
   <body>
   	<object type="application/x-shockwave-flash" data="foo.swf" width="300" height="300">
   		<param name="movie" value="foo.swf">
         	<param name="quality" value="high">
         	<param name="scale" value="noscale">
         	<param name="salign" value="LT">
         	<param name="menu" value="false">
    	</object>
    </body>
</html>

2. Embed this html on the facebook page using the fb:iframe tag.

<fb:iframe src="http://anandnalya.com/foo.html" width="300" height="300" frameborder="0" scrolling="no"></fb:iframe>

Now you can make all the Actionscript calls you want to. Only caveat to this approach is that you will no longer be able to make Facebook API calls directly from flash.

Tagged , , , ,

Web 2.0: Tactical Questions

Tactical questions to ask about your Web 2.0 Strategy:

  • Are your web site, storefront, web presence, development ecosystem, and user experience aligned with and open to collective user value best practices?
  • Are you (or is your management) comfortable with letting users have their own independent voices on your site?
  • Do you allow users to participate on your site? Can they share their own questions and ideas there?
  • How do you attract users to participate on your site? What brings them there initially, and what encourages them to come back?
  • What features of your site help users make connections with each other? Can users form groups with other users?
  • How sense-rich is the participation you support? Can users present sound, video, or even just formatted text?
  • Do you provide mechanisms (tagging, for example) to help users create their own navigation through your site?
  • Do you support user annotation of your site?
  • How could your site learn from user behavior and adapt itself to users? Which categories of user behavior have the most potential in your situation?
  • How do you encourage users to bring other people to your site?
  • How do you encourage users to share information in public, where it can generate positive network effects? In other words, is your site “public by default”?
  • Can you create—or tighten—feedback loops between user requests and your company’s ability to fulfill them?
  • Can users keep up with your site through syndication—RSS or similar approaches?
  • Do you provide programming interface (API) access to developers who want to combine, or mash up, the contents of your site with complementary materials from elsewhere?
  • What licensing do you use for your site contents? And what licensing can users specify for their own contributions? (For example, All Rights Reserved versus Creative Commons with some rights reserved.)
  • Do users feel they can control their information once they’ve entrusted your site with it? Can they extract it later?
  • How do you support your active community members?
  • Is there an opportunity to charge for the services your site provides? If so, can you structure those charges so that the people benefiting most directly from your site are the ones paying, while those contributing pay less or nothing?

Addendum: Should you implement a Social Network? Ask these questions ( again from Web 2.0 Strategy )

  • How might your users relate to each other? (Or, how do they presently relate to each other?)
  • If you have an existing place for users to communicate, would you benefit from enriching it with profiles and contacts?
  • Do you provide mechanisms for your users to communicate among themselves?
  • Do you provide tools for users to invite others to join your site?
  • How much information are your users really willing to share about themselves?
  • What balance of openness and privacy is appropriate in your business’s context? What mechanisms do you have to maintain that balance?
  • What value might user information create for you in this context? Advertising value? User happiness value? A new business of people who want to contact your users?
  • Do you know how tightly knit your current users are? Are they tightly clustered, loosely clustered, or purely individuals who haven’t yet formed connections?
  • Have you identified key individuals in your user base who have developed the trust of others and can make things happen on your site?
  • Would your site benefit from a full-blown social networking component, with contact lists, degrees of separation, and more, or might simply adding a profile page for users provide more immediate benefits?
  • How does the current nature of your user base influence your potential for expansion? Are there specific subjects you should explore or projects you should undertake to maximize network effects building on who your current users know?
  • What are the ratios of different kinds of users on your site? How many are contributors, how many are readers, and how many are active in community-building or networking? How can you monetize some groups without alienating others?

Tagged

Hello world!

// Hello world

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!