Ejemplo n.º 1
0
 def save(self):
     '''
     Save the image in the file system and add it to the database
     '''
     user_id = self.user_id
     self.create_images()
     fname = os.path.join(self.fullpath, self.name)
     database = connect_to_database()
     cursor = database.cursor()
     if os.path.exists(fname):
         #File exists. we don't need to save it anymore.
         cursor.execute("SELECT id FROM phoo_image WHERE "
                      "filename = %s",(self.name,))
         result = cursor.fetchone()
         if result:
             return 
     #Save the object in database.
     if not self.title:
         self.title = ".".join(self.name.split('.')[:-1])
     cursor.execute("INSERT INTO phoo_image (user_id, filename, title, "
                  "description) VALUES (%s,%s,%s,%s)",
                  (self.user_id, os.path.join(self.partial_path, self.name), 
                   self.title, self.description))
     database.commit()
     cursor.execute("SELECT LAST_INSERT_ID()")
     self.image_id = cursor.fetchone()[0]
     cursor.execute("INSERT INTO phoo_image_album (phoo_image_id,"
                  "phoo_album_id) VALUES (%s,%s)",(self.image_id, 1))
     database.commit()
     cursor.close()
     database.close()
Ejemplo n.º 2
0
 def link_tags(self, post_id, tags):
     '''Link the post with a category (we call it here as a tag), if the tag does not
     exist then it is created, search for tags is performed in lower case'''
     if not tags:
         return
     database = connect_to_database()
     cursor = database.cursor()
     cursor.execute("SELECT id, name FROM blog_category")
     categories = cursor.fetchall()
     for tag in tags:
         #Exists??
         matches = [k[0] for k in categories if unicode(k[1].lower(),'latin1') == tag]
         if not matches:
             #Add the new category:
             query = ("INSERT INTO blog_category (name, createtime, updatetime) "
                     "VALUES (%s,NOW(),NOW())")
             cursor.execute(query,(tag,))
             database.commit()
             cursor.execute("SELECT LAST_INSERT_ID()")
             matches.append(cursor.fetchone()[0])
         #link
         for match in matches:
             query = ("INSERT INTO blog_entrycat (entry_id, category_id) "
                     "VALUES (%s,%s)")
             cursor.execute(query, (post_id, match))
     database.commit()
     cursor.close()
     database.close()
Ejemplo n.º 3
0
    def new_post(self, title, summary='', content=''):
        '''
        Create a new post, returns the post ID
        @param title: Title of the post
        @param summary: Summary of the post
        @param content: content of the post
        '''
        fast_url = ""
        for char in title:
            if char not in ALLOWEDCHARS and char not in ALLOWEDCHARS.upper():
                char = "_"
            fast_url += char
        fast_url = fast_url.decode("utf8")
        database = connect_to_database()
        cursor = database.cursor()
        createtime = datetime.datetime.now()
        summary = self.__youtube(summary)
        summary = self.__vimeo(summary)
        summary = self.__flickr(summary)
        tags = []
        summary, tags = self.__tags(summary)
                    
        if summary.find("[more]") != -1:
            tmpsummary = summary.split("[more]")
            content = "".join(tmpsummary)
            summary  = tmpsummary[0]
            
########tmpsummary = ''
########for line in summary.split("\n"):    
########    if line.strip():
########        tmpsummary += " " + line
########        continue
########    tmpsummary += "\n\n"
########summary = tmpsummary.replace("\r",'')
########
########tmpcontent = ""
########for line in content.split("\n"):    
########    if line.strip():
########        tmpcontent += " " + line
########        continue
########    tmpcontent += "\n\n"
########content = tmpcontent.replace("\r",'')
        #Check if there is another title with fast_url"
        cursor.execute("SELECT 1 FROM blog WHERE fast_url=%s",(fast_url))
        append = 0
        while cursor.fetchone():
            append += 1
            tmpfasturl = "%s%d"%(fast_url,append)
            cursor.execute("SELECT 1 FROM blog WHERE fast_url=%s",(tmpfasturl))
        if append:
            fast_url = "%s%d"%(fast_url,append)
        cursor.execute('INSERT INTO blog (title, fast_url, text, summary, '
                       'user_id, createtime, publishtime, updatetime, published) '
                'VALUES (%s,%s,%s,%s,%s,NOW(),NOW(),NOW(),1 )', (title, 
                                 fast_url, 
                                 content.encode("latin1",errors="ignore"), 
                                 summary.encode("latin1",errors="ignore"), 
                                 self.get_user_id(self.sender)))
        database.commit()
        cursor.execute("SELECT LAST_INSERT_ID()")
        post_id = cursor.fetchone()[0]
        self.link_tags(post_id, tags)
        cursor.close()
        database.close()
        return post_id