import grok

from myproject import resource
from datetime import datetime

class Myproject(grok.Application, grok.Container):
    def __init__(self):
        super(Myproject, self).__init__()
        self.titre=''
        self.mainteneur=''
        self.creation_date=datetime.now()
        self.modification_date = self.creation_date
        self.bugs = []
    def addBug(self, bug):
        self.bugs.append(bug)
        self._p_changed = True

class Index(grok.View):
    def update(self):
        resource.style.need()
    def creation_date(self):
        if self.context.creation_date is None:
            return ''
        return self.context.creation_date.strftime('%Y-%m-%d %H:%M')
    def modification_date(self):
        if self.context.modification_date is None:
            return ''
        return self.context.modification_date.strftime('%Y-%m-%d %H:%M')
    def afficher_bugs(self):
        print self.context.bugs
        return '<ol>%s</ol>' % "".join(['<li>%s</li>' % b for b in self.context.bugs])

class Edit(grok.View):
    def update(self, titre=None, mainteneur=None):
        resource.style.need()
	if titre is not None:
            self.context.titre = titre
        if mainteneur is not None:
            self.context.mainteneur = mainteneur
        self.context.modification_date = datetime.now()
        if titre is not None or mainteneur is not None:
            self.redirect(self.url('index'))

class Add(grok.View):
    def update(self, bug=None):
        resource.style.need()
        if bug is not None:
            self.context.addBug(bug)
            self.redirect(self.url('index'))

