All pastes #616613 Raw Edit

Stuff

public text v1 · immutable
·published 2007-07-12 18:54 UTC
rendered paste body
#!/home/devlinsb/python
# DEVLINSBLOG.SYSTEM
'''A free-to-use hard-to-get Python blog-system.'''

import cgi
import MySQLdb
import time

class Blog:
    '''Contains all blog functions.'''
    
    # SQL connection
    sql = MySQLdb.connection(host="localhost", user="devlinsb_blog", passwd="jRml0s#", db="devlinsb_blog")
    
    # Configuration
    entries_on_index = 5 # the amount of entries that will be displayed on the index. set to a non-number to display all articles in archive view instead.
    
    
    def __init__(resource="index", modifier=False):
        '''Starts up the blog system using the resource variable resource and modifier variable modifier.'''
        
        if resource == "index" and not modifier:
            print "Content-Type: text/html\n"
            self.index()
        elif resource == "archives" and not modifier:
            print "Content-Type: text/html\n"
            self.archives()
        elif resource == "entry" and modifier.isdigit():
            print "Content-Type: text/html\n"
            self.entry(modifier)
        else:
            print "Content-Type: text/plain'n"
            print "No such resource/modifier '%s/%s'" % (resource, modifier)
            
    def index():
        '''Function that declares what will be shown on the front page (ie. the default.)'''
        
        if entries_on_index.isdigit():
            cursor = self.sql.cursor()
            cursor.execute("SELECT * FROM articles ORDER BY id DESC LIMIT %s" % self.entries_on_index)
            data = cursor.fetchall()
            
            for entry in data:
                # id=0, title=1, content=2, date=3
                content = ["      <p>%s</p>" % line for line in entry[2].split('\n')]
                print "    <h2>%s <small>%s</small></h2>" % (entry[1], entry[3])
                print content
        else:
            self.archives()
            
    def archives(amount=9999, order="ASC"):
        '''Prints an unordered list of entries.
           amount [int] Number of entries to show.
           order [string] Set to ASC or DESC.'''
        
        cursor = self.sql.cursor()
        cursor.execute("SELECT * FROM articles ORDER BY id %s" % order)
        data = cursor.fetchall()
        
        print '    <ul class="archives">'
        for entry in data:
            # id=0, title=1, content=2, date=3
            print '      <li id="archives-entry-%s"><a href="/entries/%s.html">%s</a></li>' % (entry[0], entry[0], entry[1])
        print "    </ul>"
        
    def entry(id):
        '''Prints an entry.'''
        
        cursor = self.sql.cursor()
        cursor.execute("SELECT * FROM articles WHERE id = %s" % id)
        entry  = cursor.fetchone()
        content = ["      <p>%s</p>" % line for line in entry[2].split('\n')]
        print "    <h2>%s <small>%s</small></h2>" % (entry[1], entry[3])
        print content

resource = cgi.FieldStorage().getvalue('resource')
modifier = cgi.FieldStorage().getvalue('modifier')

#print resource
#print modifier

Blog(resource, modifier)