# Path: dinette/libs/postmarkup.py
# def render_bbcode(bbcode,
#                   encoding="ascii",
#                   exclude_tags=None,
#                   auto_urls=True,
#                   paragraphs=False,
#                   clean=True,
#                   tag_data=None):
# 
#     """ Renders a bbcode string in to XHTML. This is a shortcut if you don't
#         need to customize any tags.
# 
#         post_markup -- String containing bbcode.
#         encoding -- Encoding of string, defaults to "ascii" if the string is not
#         already unicode.
#         exclude_tags -- A collection of tag names to ignore.
#         auto_urls -- If True, then urls will be wrapped with url bbcode tags.
#         paragraphs -- If True then line breaks will be replaces with paragraph
#         tags, rather than break tags.
#         clean -- If True, html will be run through a cleanup_html method.
#         tag_data -- An optional dictionary to store tag data in. The default of
#         None will create a dictionary internally.
# 
#     """
#     return _postmarkup(bbcode,
#                        encoding,
#                        exclude_tags=exclude_tags,
#                        auto_urls=auto_urls,
#                        paragraphs=paragraphs,
#                        clean=clean,
#                        tag_data=tag_data)


from django.db import models
from django.contrib.auth.models import User, Group
from django.conf import settings
from django.contrib.sites.models import Site
from django.template.defaultfilters import slugify
from django.db.models.signals import post_save
from django.template.defaultfilters import truncatewords
from django.core.urlresolvers import reverse
from BeautifulSoup import BeautifulSoup
from dinette.libs.postmarkup import render_bbcode
from markupfield.fields import MarkupField
        from django.conf import settings
import hashlib
import datetime
        super(Reply, self).save(*args, **kwargs)
    
    def __unicode__(self):
        return truncatewords(self.message, 10)
    
    def search_snippet(self):
        msg = "%s %s"%(self.message.rendered, self.topic.subject)
        return truncatewords(msg, 100)
    
    
    @models.permalink
    def get_absolute_url(self):
        return ('dinette_topic_detail',(),{'categoryslug':self.topic.category.slug,'topic_slug': self.topic.slug})
    
    def get_url_with_fragment(self):
        page = (self.reply_number-1)/settings.REPLY_PAGE_SIZE + 1
        url =  self.get_absolute_url()
        if not page == 1:
            return "%s?page=%s#%s" % (url, page, self.reply_number)
        else:
            return "%s#%s" % (url, self.reply_number)
            
    
    def htmlfrombbcode(self):
        soup = BeautifulSoup(self.message.raw)
        #remove all html tags from the message
        onlytext = ''.join(soup.findAll(text=True))
        
        #get the bbcode for the text
        if(len(onlytext.strip()) >  0):            

# Path: dinette/libs/postmarkup.py
# def render_bbcode(bbcode,
#                   encoding="ascii",
#                   exclude_tags=None,
#                   auto_urls=True,
#                   paragraphs=False,
#                   clean=True,
#                   tag_data=None):
# 
#     """ Renders a bbcode string in to XHTML. This is a shortcut if you don't
#         need to customize any tags.
# 
#         post_markup -- String containing bbcode.
#         encoding -- Encoding of string, defaults to "ascii" if the string is not
#         already unicode.
#         exclude_tags -- A collection of tag names to ignore.
#         auto_urls -- If True, then urls will be wrapped with url bbcode tags.
#         paragraphs -- If True then line breaks will be replaces with paragraph
#         tags, rather than break tags.
#         clean -- If True, html will be run through a cleanup_html method.
#         tag_data -- An optional dictionary to store tag data in. The default of
#         None will create a dictionary internally.
# 
#     """
#     return _postmarkup(bbcode,
#                        encoding,
#                        exclude_tags=exclude_tags,
#                        auto_urls=auto_urls,
#                        paragraphs=paragraphs,
#                        clean=clean,
#                        tag_data=tag_data)


# Path: dinette/models.py
from django.db import models
from django.contrib.auth.models import User, Group
from django.conf import settings
from django.contrib.sites.models import Site
from django.template.defaultfilters import slugify
from django.db.models.signals import post_save
from django.template.defaultfilters import truncatewords
from django.core.urlresolvers import reverse
from BeautifulSoup import BeautifulSoup
from dinette.libs.postmarkup import render_bbcode
from markupfield.fields import MarkupField
        from django.conf import settings
import hashlib
import datetime

        super(Reply, self).save(*args, **kwargs)
    
    def __unicode__(self):
        return truncatewords(self.message, 10)
    
    def search_snippet(self):
        msg = "%s %s"%(self.message.rendered, self.topic.subject)
        return truncatewords(msg, 100)
    
    
    @models.permalink
    def get_absolute_url(self):
        return ('dinette_topic_detail',(),{'categoryslug':self.topic.category.slug,'topic_slug': self.topic.slug})
    
    def get_url_with_fragment(self):
        page = (self.reply_number-1)/settings.REPLY_PAGE_SIZE + 1
        url =  self.get_absolute_url()
        if not page == 1:
            return "%s?page=%s#%s" % (url, page, self.reply_number)
        else:
            return "%s#%s" % (url, self.reply_number)
            
    
    def htmlfrombbcode(self):
        soup = BeautifulSoup(self.message.raw)
        #remove all html tags from the message
        onlytext = ''.join(soup.findAll(text=True))
        
        #get the bbcode for the text
        if(len(onlytext.strip()) >  0):            

            return render_bbcode(onlytext)