# Example #3: Pointless Portal
#  by Leonard Richardson (leonardr@segfault.org)
#   Business objects

import os
import random
import string

from IWantOptions import FileBackedContext
from IWantOptions import OptionWrapper 
from IWantOptions import LongStringOption

class Site(FileBackedContext):

    def getName(self):
        return self['siteName']

    def getText(self):
        return self['siteText']

    def getAds(self):
        return string.split(self['ads'], '|')

class User(FileBackedContext):

    def __init__(self, config, dataDir, username):
        self.username = username        
        FileBackedContext.__init__(self, config, os.path.join(dataDir, username))

    def isAdmin(self):
        """Incredibly sophisticated authorization system ACTUALLY DETERMINES
        whether or not the user name is "admin"."""
        return 'admin' == self.getUsername()

    def getUsername(self):
        "Returns a user's username."
        return self.username

    def getRealName(self):
        "Returns a user's real name."
        return self['realName']

    def getContactEmail(self):
        "Returns a user's contact email."
        return self['email']

    def getBackgroundColor(self):
        "Returns a user's preferred background color."
        return self['backgroundColor']

    def getTextColor(self):
        "Returns a user's preferred text color."
        return self['textColor']

    def getLinks(self):
        "Returns a list of user links."
        return string.split(self['myLinks'], ',')
    
    def getNewsSources(self):
        "Returns the user's selected news sources."
        return string.split(self['newsSources'], ',')

class SimpleOptionWrapper(OptionWrapper):

    #We mainly override so that IWO will scan this package for option
    #class definitions.

    def renderControl(self, option, context, presetValue=None):
        """Render an HTML depiction of the given option."""
        print '<p>'
        if hasattr(option, 'displayName'):
            print '<b><font color="blue">%s</font></b>:' % self.getOptionDisplayName(option, context)
        option.renderEditControl(context, presetValue)
        print '<br/>'
        if hasattr(option, 'description'):
            print self.getOptionDescription(option, context)
        print '</p>'

class ExtremeStringOption(LongStringOption):

    """Require a string to contain the substring "extreme"."""

    def validate(self, context, value):
        if string.find(string.lower(value), 'extreme') == -1:
            return "Embarassingly pseudo-hip, focus-group-tested descriptive text must contain the string 'extreme'."

class NewsSource:

    def getPossibleItems(self):
        """In a real app you'd actually go out over the web, but here
        we just provide a list of possible items and pick some at random."""
        return []

    def getItems(self):
        return [random.choice(self.getPossibleItems())]

class BNNNewsSource(NewsSource):

    def getPossibleItems(self):
        return ['50-foot python terrifies Idaho',
                'Guido van Rossum: European supervillain mastermind?',
                "The latest fad: we can't stop writing sneaky articles that sort of cover it while pretending not to!"]

class PythonNewsSource(NewsSource):

    def getPossibleItems(self):
        return ['Python Wins Some Award Or Other', 'All Bow To Python',
                'PyCon Attendees Run Amok']

class UpdatedWeblogsSource(NewsSource):

    def getPossibleItems(self):
        return ['Frog Blog', 'The Nonexistent Weblog',
                'Incredible Shrinking Weblog']
    
NEWS_SOURCES = { 'bnn' : BNNNewsSource,
                 'python' : PythonNewsSource,
                 'weblogs' : UpdatedWeblogsSource }                 
