[Twisted-web] newbie question

Graeme Glass graemeglass at gmail.com
Sat Aug 16 17:46:05 EDT 2008


On Sat, Aug 16, 2008 at 12:24 AM, arun chhetri <uhcops at gmail.com> wrote:

> hi,
>  I have developed some twisted.web application. It connects to the other
> server (CalDav) and then it displays back using twisted.web.
> My problem is when a user logs into my website which is made using
> twisted.web there is some data fetched from CALDAV server. When another u=
ser
> logs into my website, even his data is downloaded from CalDav server but
> this data overwrites the data of previous server. I think I am not able to
> fork the process on each request. This is my just guess.
>
> The Code is given below
>
> from twisted.web import resource, static, server
> import vobject
> import calendarWebUtilities
> import Calendar
> import datetime
> import re,calendar
> import Month
> import Week
> import Day
> import socket
>
>
> class HomePage(resource.Resource):
>     def render(self,request):
>         return"""
>     <html>
>     <head>
>     <title>VCalendar</title>
>     </head>
>     <body>
>     <form action=3D/optional method=3D"post">
>         User Name <input type=3D"text" name=3D"username"><br/>
>         Pass Word <input type=3D"password" name=3D"pswd"><br/>
>         ServerAdd <input type=3D"text" name=3D"server"><br/>
>         <input type=3D"submit" Value=3D"Submit"><br/>
>     </form>
>     </body>
>     </html>"""
>
>
> class Optional(resource.Resource):
>     def __init__(self):
>         resource.Resource.__init__(self)
>         self.CalendarData =3D {}
>         self.account =3D {}
>     def render(self,request):
>         user =3D request.args["username"]
>         pswd =3D request.args["pswd"]
>         server =3D request.args["server"]
>         if user !=3D None:
>             if pswd !=3D None:
>                 if server !=3D None:
>                     CalendarSource =3D None
>                     CalendarSource =3D
> Calendar.CalendarObject(user[0],pswd[0],server[0])
>                     todayDate =3D datetime.datetime.now()
>                     start =3D
> datetime.datetime((todayDate.year-1),01,01,00,00)
>                     end =3D
> datetime.datetime((todayDate.year+1),12,31,23,59,59)
>                     self.CalendarData['value'] =3D
> CalendarSource.readCalendarComponent(start,end)
>                     self.account['value'] =3D CalendarSource
>                     self.index =3D 1
>         request.redirect("/optional/Calendar")
>         return " "
>
>    def getChild(self,path,request):
>         return MainCalendar(self.index,self.CalendarData,self.account)
>
>
>
> class MainCalendar(resource.Resource):
>     def __init__(self,index,CalendarData,account):
>         resource.Resource.__init__(self)
>         self.index =3D index
>         self.CalendarData =3D CalendarData
>         self.account =3D account
>
> self.putChild('Add',AddCalendarComponent(self.CalendarData,self.account))
>
>     def render(self,request):
>         global Calendarata
>         global account
>         if request.path =3D=3D "/optional/Calendar" :
>             request.redirect("/optional/Calendar/Month")
>             return " "
>
>
>
>     def getChild(self,path,request):
>         if request.path =3D=3D "/optional/Calendar/Month":
>             return Month.month(self.CalendarData,self.index)
>         elif request.path =3D=3D "/optional/Calendar/Week":
>             return Week.Week(self.CalendarData,self.index)
>         elif request.path =3D=3D "/optional/Calendar/Day":
>             return Day.Day(self.CalendarData,self.index)
>
>
> class AddCalendarComponent(resource.Resource):
>     def __init__(self,CalendarData,account):
>         resource.Resource.__init__(self)
>         self.CalendarData =3D CalendarData
>         self.account =3D account
>
>     def render (self,request):
>         #if request.args !=3D None:
>         queryString =3D len(request.args)
>         if queryString !=3D0:
>             try :
>                 summary1 =3D request.args['summary']
>                 location1 =3D request.args['location']
>                 startDate1 =3D request.args['startDate']
>                 endDate1 =3D request.args['endDate']
>                 CalendarSource =3D self.account['value']
>                 event =3D
> CalendarSource.convertToProperFormatAndAddToCalendar(summary1,location1,s=
tartDate1,endDate1)
>                 (self.CalendarData['value']).append(event)
>                 #CalendarData =3D {}
>                 #print "New Data Created"
>                 #print NewCalendar
>                 #CalendarData['value'] =3D NewCalendar
>                 request.redirect("/optional/Calendar")
>                 return " "
>             except KeyError:
>                 request.args =3D {}
>                 request.redirect('/optional/Calendar/Month/Add')
>         else:
>             return"""
>         <html>
>         <head>
>         </head>
>         <body>
>
>         <form action=3D"/optional/Calendar/Add" method=3D"post">
>         Summary =3D <INPUT type=3D"text" name=3D"summary"></br>
>         Location =3D <input type=3D"text" name=3D"location"></br>
>         Start Date =3D <input type=3D"text" name=3D"startDate">Should Be =
in
> format year/MM/date/HH/MM</br>
>         End Date =3D <input type=3D"text" name=3D"endDate">Should Be in f=
ormat
> year/MM/date/HH/MM</br>
>         <input type=3D"submit">
>         </form>
>         </body>
>         </html>
>         """
>
>
>
>
> if __name__ =3D=3D "__main__":
>     from twisted.internet import reactor
>     root =3D resource.Resource()
>     root.putChild('',HomePage())
>     root.putChild('optional',Optional())
>     site =3D server.Site(root)
>     reactor.listenTCP(8000,site)
>     reactor.run()
>
> I can guess there should be some method by which I can fork the root for
> different request,, Please help.
>
> Thanks
>
>
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
>
>
Try using a factory for your session storage instead of storing it in the
actually resource, that way you will be able to access your session
information from the resources without overriding it on every request. Take
a look at twisted.internet.protocol.ServerFactory.

HTH

G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20080816/67=
275c6d/attachment.htm


More information about the Twisted-web mailing list