Ejemplo n.º 1
0
 def selectEntry(self):
     #for entry in DownloadEntry:
         #print entry.show()
     if not self.normal:
         for entry in DownloadEntry.select(active=1):
             self.normal = True
             return entry
     self.normal = True
     for entry in DownloadEntry.select_creation(cond="finished=0 AND error=''"):
         return entry
     return None
Ejemplo n.º 2
0
    def __call__(self, environ, start_response):
      
        path = urllib.unquote(environ["PATH_INFO"])
        
        innerhtml = blank()
        status = "200 OK"
        headers = headers = [('Content-type', 'text/html; charset=UTF-8')]
        if path == "/" or path == "/list":
            query = helpers.parse_query_string(environ['QUERY_STRING'])
            if "delid" in query:
                try:
                    delid = int(query.get("delid")[0])
                    inst = DownloadEntry.select_id(delid)
                    DownloadEntry.delete(inst)
                except:
                    pass
            if "retryid" in query:
                try:
                    retryid = int(query.get("retryid")[0])
                    inst = DownloadEntry.select_id(retryid)
                    inst.error=''
                except:
                    pass       
            if "clearErrors" in query:
                for entry in DownloadEntry.select_cond(cond="NOT error=''"):
                    DownloadEntry.delete(entry)
            if "clearFinished" in query:
                for entry in DownloadEntry.select(finished=1):
                    DownloadEntry.delete(entry)
                    
           
            errorButton = form(action="/list")(input(type="submit", value="Clear errors"), input(type="hidden", name="clearErrors"))
            finishButton = form(action="/list?clearFinished")(input(type="submit", value="Clear finished downloads"), input(type="hidden", name="clearFinished"))
            
            dlist = self.downloadList()
            dlist.add(tr(td(finishButton), td(colspan="5")(errorButton)))
            innerhtml = dlist

        elif path == "/create":
            if environ["REQUEST_METHOD"] == "POST":
                msg = blank()
                query = helpers.parseURLEncoded(environ)
                print query
                url = query.get("url")
                destination = query.get("destination")
                ratelimit = query.get("ratelimit")
                if not "edit" in query:
                    if not url:
                        msg = b("No URL given!")
                    else:
                        if not destination:
                            msg = b("No destination given!")
                        else:
                            if not ratelimit:
                                ratelimit = 0
                            else:
                                ratelimit = ratelimit[0]
                            url = url[0]
                            destination = destination[0]
                            
                            if not url.startswith("http://"):
                                url = "http://"+url                       
                            dl = DownloadEntry.new(url=url,
                                                    destination=destination,
                                                    ratelimit=ratelimit,
                                                    active=0,
                                                    finished=0,
                                                    error='')
                            msg = "Added Entry"
                    innerhtml = blank(p(msg), self.creationForm())
                else:
                    innerhtml = self.creationForm(url[0], destination[0], ratelimit[0])
            else:
                innerhtml = self.creationForm()
                
        elif path == "/config":
            defaults = Defaults.select_id(1)
            msg = blank()
            if environ["REQUEST_METHOD"] == "POST":
                query = helpers.parseURLEncoded(environ)
                print query

                destination = query.get("destination")
                if not destination:
                    msg = b("Destination not given!")
                else:
                    ratelimit = query.get("ratelimit")
                    if ratelimit is None:
                        msg = b("Rate Limit not given!")
                    else:
                        destination = destination[0]
                        try:
                            ratelimit = int(ratelimit[0])
                            defaults.ratelimit = ratelimit
                        except ValueError:
                            msg = b("Rate Limit must be a number!")
                            ratelimit = defaults.ratelimit
                        if not os.path.isdir(destination):
                            try:
                                os.makedirs(destination)
                                defaults.destination = destination
                            except OSError:
                                msg = b("Invalid destination!")
                        else:
                            defaults.destination = destination
                               
            innerhtml = blank(
            h1("Defaults"), msg,
            form(action="/config", method="POST")(
                table(
                    tr(
                        td("Destination"), td(input(type="text", name="destination", value=defaults.destination))
                    ), 
                    tr(
                        td("Rate Limit (in B)"), td(input(type="text", name="ratelimit", value=defaults.ratelimit))
                    ),
                    tr(
                        td(input(type="submit"))
                    )
                )
            ))
        else:
            status = "404 Not found"
            headers = [('Content-type', 'text/html; charset=UTF-8')]
            innerhtml= b("404 File not found.")
        yield str(XHTML10DTD())
        src = \
            html(
                noEscapeBlank('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'),
                body(
                    table(
                        tr(
                            td(self.menu()), td(innerhtml)
                        )
                    )
                )
            )
        yield str(src)