[Hepserver-commits] hep/hep/web __init__.py,NONE,1.1 messages.py,NONE,1.1 models.py,NONE,1.1
Status: Alpha
Brought to you by:
abefettig
|
From: <abe...@us...> - 2003-07-10 16:22:36
|
Update of /cvsroot/hepserver/hep/hep/web
In directory sc8-pr-cvs1:/tmp/cvs-serv6713/hep/web
Added Files:
__init__.py messages.py models.py
Log Message:
imported files
--- NEW FILE: __init__.py ---
pass
--- NEW FILE: messages.py ---
import os
from twisted.internet import app, defer
from twisted.web.woven import page, input, guard, widgets, view, model, interfaces
from twisted.web import server, static, microdom, domhelpers, resource
from twisted.web.microdom import lmx
from twisted.python import components
from twisted.python.util import sibpath
import messaging
import hep
import hep.web
import time
from hep.services.datamanager import DataManagerService, HepUser
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
class Calendar(widgets.Widget):
def setUp(self, request, node, data):
yearNo, monthNo, today = time.localtime()[:3]
date = request.args.get('date',[''])[0]
if date.count('-') == 1:
yearNo, monthNo, today = time.strptime(date, "%Y-%m")[:3]
elif date.count('-') == 2:
yearNo, monthNo, today = time.strptime(date, "%Y-%m-%d")[:3]
node.tagName = "table"
curTime = time.localtime()
curMonth = calendar.monthcalendar(yearNo, monthNo)
month = lmx(node)
row = month.tr()
row.th().a(href='prev').text('< ')
row.th(colspan='5').text(time.strftime("%B %Y", curTime))
row.th().a(href='prev').text('> ')
headers = month.tr()
for dayName in ["Su", "M", "T", "W", "Th", "F" , "S"]:
headers.th(
_class="dayName", align="right"
).text(dayName)
for curWeek in curMonth:
week = month.tr(_class="week")
for curDay in curWeek:
if curDay == 0:
week.td(_class="blankDay")
else:
if curDay == today:
className = "today"
else:
className = "day"
td = week.td()
td['class'] = className
td['align'] = 'right'
if data.count(curDay):
formattedDate = str("?date=%s" % time.strftime('%Y-%m-%d', (yearNo, monthNo, int(curDay), 0, 0, 0, 0, 0, 0)))
td.a(href=formattedDate).text(str(curDay))
else:
td.text(str(curDay))
print "DONE GENERATING CALENDAR"
class MessageWidget(widgets.Widget):
def setUp(self, request, node, message):
node.tagName = "div"
div = lmx(node)
div['class'] = 'message'
head = div.div(_class='messageHeader')
if message.link:
head.h4().a(href=message.link).text(message.title)
else:
head.h4().text(message.title)
if 0: #for header in ('author', 'to', 'link'):
headerValue = getattr(message, header, '')
if headerValue:
head.strong().text(header.capitalize())
head.text(": ")
if header == 'link':
head.a(href=headerValue).text(headerValue)
elif header == 'author':
fromText = headerValue.address and headerValue or headerValue.name
head.text(headerValue.name)
else:
head.text(headerValue)
head.br()
head.br()
div.text(message.getHTML(bodyOnly=1, safeTagsOnly=1) or "", raw=1)
div.div(_class='timestamp').small().text("%s, %s" % (
message.author.address and message.author or message.author.name,
time.strftime('%I:%M %p', message.timestamp)))
print "DONE GENERATING MESSAGE"
class BlogView(view.View):
templateDirectory = sibpath(hep.web.__file__, 'templates')
templateFile = "blogview.html"
def __init__(self, *args, **kwargs):
view.View.__init__(self, *args, **kwargs)
self.setSubviewFactory("message", MessageWidget)
self.setSubviewFactory("calendar", Calendar)
class MessagesPage(page.Page):
templateDirectory = sibpath(hep.web.__file__, 'templates')
templateFile = "messages.html"
isLeaf = 1
def initialize(self, *args, **kwargs):
self.setSubviewFactory("blogview", BlogView)
def wvupdate_folderList(self, request, node, data):
stores = data.items()
item = lmx(node)
list = item.ul()
stores.sort(lambda a, b: cmp(
[a[0].startswith('Connections'), a[0].lower()],
[b[0].startswith('Connections'), b[0].lower()]
))
depth = 1
for storePath, store in stores:
difference = storePath.count('/') - depth
if difference == 1:
list = item.ul()
elif difference < 0:
for n in range(abs(difference)):
if list.node.parentNode:
list = lmx(list.node.parentNode.parentNode)
else:
break
item = list.li()
a = item.a(href="/messages//" + storePath)
if "/".join(request.postpath[:-1]) == storePath[1:]:
a['class'] = 'selected'
a.img(src='/images/icons/small/%s.png' % store.url.protocol, align='absmiddle')
storeName = storePath.split('/')[-1]
a.text(len(storeName) > 18 and (storeName[:16] + '...') or storeName)
depth = depth + difference
#components.registerAdapter(StoreModel, messaging.MessageStore, interfaces.IModel)
--- NEW FILE: models.py ---
from twisted.internet import defer
from twisted.web.woven import model
import time
class UserModel(model.MethodModel):
def initialize(self, *args, **kwargs):
self.user = kwargs['user']
def getStore(self, request):
path = "/".join(request.postpath)
if path:
return self.user.messages.openChildStores().addCallback(lambda stores: stores.get(path) or self.user.messages)
else:
return defer.succeed(self.user.messages)
def wmfactory_messages(self, request):
finished = defer.Deferred()
self.getStore(request).addCallback(
lambda store: store.listWithHeaders().addCallback(self.gotList, store, request, finished)
)
return finished
def wmfactory_connectionList(self, request):
allChildren = self.user.messages.openChildStores()
return allChildren
def wmfactory_storeName(self, request):
return self.getStore(request).addCallback(lambda store: store.name)
def printError(self, err):
print "***********************ERROR!**********************", err
def gotList(self, messageList, store, request, finished):
messageList = messageList.items()
if not messageList:
finished.callback([])
return
# sort messageList by date
messageList.sort(lambda a,b: cmp(a[1]['timestamp'], b[1]['timestamp']))
finished.count = 0
finished.messages = []
date = request.args.get('date',[''])[0]
if date:
day = time.strptime(date, "%Y-%m-%d")[:3]
else:
day = messageList[-1][1]['timestamp'][:3]
idsToFetch = []
for id, headers in messageList:
if headers['timestamp'][:3] == day:
idsToFetch.append(id)
if not idsToFetch:
finished.callback([])
return
finished.count = len(idsToFetch)
for id in idsToFetch:
d = store.getMessage(id)
d.addCallback(self.gotMessage, finished)
d.addErrback(self.printError)
def gotMessage(self, message, finished):
print "GOT MESSAGE (%s of %s)" % (len(finished.messages), finished.count)
finished.messages.append(message)
if len(finished.messages) == finished.count:
finished.messages.sort(lambda a,b: cmp(b.timestamp, a.timestamp))
print "GOT ALL MESSAGES, CALLING BACK"
finished.callback(finished.messages)
def wmfactory_calendarDates(self, request):
finished = defer.Deferred()
self.getStore(request).addCallback(
lambda store: store.listWithHeaders().addCallback(
self.gotListForCalendar, request, finished
).addErrback(self.printError)
)
return finished
def gotListForCalendar(self, messageList, request, finished):
print "GOT LIST FOR CALENDAR!"
month = request.args.get('date',[''])[0]
if not month:
month = time.localtime()[1]
else:
month = time.strptime(month, "%Y-%m-%d")[1]
days = []
for id, headers in messageList.items():
if headers['timestamp'][1] == month:
days.append(headers['timestamp'][2])
print "CALLING BACK CAL DATES"
finished.callback(days)
|