On Sat, Aug 16, 2008 at 12:24 AM, arun chhetri <uhcops@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 user 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=/optional method="post">
        User Name <input type="text" name="username"><br/>
        Pass Word <input type="password" name="pswd"><br/>
        ServerAdd <input type="text" name="server"><br/>
        <input type="submit" Value="Submit"><br/>
    </form>
    </body>
    </html>"""
   

class Optional(resource.Resource):
    def __init__(self):
        resource.Resource.__init__(self)
        self.CalendarData = {}
        self.account = {}
    def render(self,request):
        user = request.args["username"]
        pswd = request.args["pswd"]
        server = request.args["server"]
        if user != None:
            if pswd != None:
                if server != None:
                    CalendarSource = None
                    CalendarSource = Calendar.CalendarObject(user[0],pswd[0],server[0])   
                    todayDate = datetime.datetime.now()
                    start = datetime.datetime((todayDate.year-1),01,01,00,00)
                    end = datetime.datetime((todayDate.year+1),12,31,23,59,59)
                    self.CalendarData['value'] = CalendarSource.readCalendarComponent(start,end)
                    self.account['value'] = CalendarSource
                    self.index = 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 = index
        self.CalendarData = CalendarData
        self.account = account
        self.putChild('Add',AddCalendarComponent(self.CalendarData,self.account))
       
    def render(self,request):
        global Calendarata
        global account
        if request.path == "/optional/Calendar" :
            request.redirect("/optional/Calendar/Month")
            return " "
      

           
    def getChild(self,path,request):
        if request.path == "/optional/Calendar/Month":
            return Month.month(self.CalendarData,self.index)
        elif request.path == "/optional/Calendar/Week":
            return Week.Week(self.CalendarData,self.index)
        elif request.path == "/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 = CalendarData
        self.account = account
       
    def render (self,request):
        #if request.args != None:
        queryString = len(request.args)
        if queryString !=0:
            try :
                summary1 = request.args['summary']
                location1 = request.args['location']
                startDate1 = request.args['startDate']
                endDate1 = request.args['endDate']
                CalendarSource = self.account['value']
                event = CalendarSource.convertToProperFormatAndAddToCalendar(summary1,location1,startDate1,endDate1)
                (self.CalendarData['value']).append(event)
                #CalendarData = {}
                #print "New Data Created"
                #print NewCalendar
                #CalendarData['value'] = NewCalendar         
                request.redirect("/optional/Calendar")
                return " "
            except KeyError:
                request.args = {}
                request.redirect('/optional/Calendar/Month/Add')
        else:
            return"""
        <html>
        <head>
        </head>
        <body>
       
        <form action="/optional/Calendar/Add" method="post">
        Summary = <INPUT type="text" name="summary"></br>
        Location = <input type="text" name="location"></br>
        Start Date = <input type="text" name="startDate">Should Be in format year/MM/date/HH/MM</br>
        End Date = <input type="text" name="endDate">Should Be in format year/MM/date/HH/MM</br>
        <input type="submit">
        </form>
        </body>
        </html>
        """
           



if __name__ == "__main__":
    from twisted.internet import reactor
    root = resource.Resource()
    root.putChild('',HomePage())
    root.putChild('optional',Optional())
    site = 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@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