ReportScript.py :  » Issue-Tracker » IssueTracker » IssueTrackerProduct » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Issue Tracker » IssueTracker 
IssueTracker » IssueTrackerProduct » ReportScript.py
# IssueTrackerProduct
#
# www.issuetrackerproduct.com
# Peter Bengtsson <mail@peterbe.com>
# License: ZPL
#

# python
from urllib import quote
import urllib2

try:
    import timeoutsocket
    # set a timeout for TCP connections
    timeoutsocket.setDefaultSocketTimeout(60)
except ImportError:
    timeoutsocket = None
    

# zope
from Products.PythonScripts.PythonScript import PythonScript
from AccessControl import ClassSecurityInfo
from Acquisition import aq_inner,aq_parent
from DateTime import DateTime

try:
    # >= Zope 2.12
    from App.special_dtml import DTMLFile
    from App.class_init import InitializeClass
    from App.Common import package_home
except ImportError:
    # < Zope 2.12
    from Globals import DTMLFile,InitializeClass,package_home


from Shared.DC.Scripts.Script import defaultBindings# Script, BindingsUI,

from Constants import *
from Permissions import VMS
import Utils

#-------------------------------------------------------------------------------

manage_addIssueReportScriptForm = DTMLFile('dtml/addReportScript', globals())

_default_file = os.path.join(package_home(globals()),
                             'www', 'default_report_script.py')
                        
def _suck_url(url):
    return urllib2.urlopen(url).read()
        
def manage_addIssueReportScript(self, id, name='', url2script=None, 
                                REQUEST=None, submit=None):
    """ Add Isse Report Script object """
    url = url2script
    
    if not id and name:
        name = name.strip()
        if name.find('-') > -1:
            id = name.replace(' ','_')
        else:
            id = name.replace(' ','-')
        id = Utils.safeId(id)
    elif not id:
        id = url.split('/')[-1]
        if id.endswith('.py'):
            id = id[:-3]
        
    id = str(id)
    id = self._setObject(id, ReportScript(id))
    if REQUEST is not None:
        file = REQUEST.form.get('file', '')
        if type(file) is not type(''): file = file.read()
        
        
        if not file:
            
            if url:
                # download it from the net
                file = _suck_url(url)
            else:
                file = open(_default_file).read()
        self._getOb(id).write(file)
        if name:
            self._getOb(id).ZPythonScript_setTitle(name)
        try: u = self.DestinationURL()
        except: u = REQUEST['URL1']
        if submit==" Add and Edit ": u="%s/%s" % (u,quote(id))
        REQUEST.RESPONSE.redirect(u+'/manage_main')
    return ''


class ReportScript(PythonScript):
    """ A ReportScript is a script where issue objects are passed
    through. The script looks at the issue and decides if it should be
    included in this particular report.
    """

    meta_type = REPORTSCRIPT_METATYPE

    manage_options = (PythonScript.manage_options[0],) + \
                     ({'label':'Test', 'action':'manage_TestReport'},) + \
                     (PythonScript.manage_options[1],) + \
                     PythonScript.manage_options[3:]
    
    security = ClassSecurityInfo()

    def __init__(self, id):
        self.id = id
        self.ZBindings_edit(defaultBindings)
        self._makeFunction()
        
        self.last_yield_parentpath = None
        self.last_yield_count = None
        self.last_yield_date = None
        

    def getId(self):
        return self.id
    
    def getTitle(self):
        """ return title """
        return self.title_or_id()
    
    security.declareProtected(VMS, 'Download2FS')
    def Download2FS(self, REQUEST=None, RESPONSE=None):
        """ return as a file for download """
        if RESPONSE is not None:
            RESPONSE.setHeader('Content-Type', 'text/x-python')
            _inline = 'inline;filename="%s.py"'
            RESPONSE.setHeader('Content-Disposition', _inline % self.getId())
        return self.read()
    
    
    def manage_afterAdd(self, REQUEST, RESPONSE):
        """ clear the last_yield_* in case this script has been copied from 
        another issuetracker. """
        if getattr(self, 'last_yield_count', None):
            if self.last_yield_parentpath != '/'.join(aq_parent(aq_inner(self)).getPhysicalPath()):
                self.last_yield_count = None
                self.last_yield_parentpath = None
                self.last_yield_date = None
                
        
    def setYieldCount(self, count):
        """ a yield count is an integer number of how many issues were returned
        when this report is run. This number is stored with a date and which 
        issuetracker path was used. The last thing is required because the script
        might be moved to a different issuetracker where the count doesn't 
        make sense because the issues are different. """
        count = int(count)
        assert count > -1, "count must be 0 or greater"
        self.last_yield_parentpath = '/'.join(aq_parent(aq_inner(self)).getPhysicalPath())
        self.last_yield_count = count
        self.last_yield_date = DateTime()
        
    def getYieldCountAndDate(self):
        """ return a tuple of the the (count, date) if possible """
        if getattr(self, 'last_yield_count', None):
            return (self.last_yield_count, self.last_yield_date)
        return None
        
    
        
        
    security.declareProtected(VMS, 'manage_TestReport')
    manage_TestReport = DTMLFile('dtml/test_report', globals())
    
    ZPythonScriptHTML_editForm = DTMLFile('dtml/ReportScriptEdit', globals())
    manage = manage_main = ZPythonScriptHTML_editForm
    ZPythonScriptHTML_editForm._setName('ZPythonScriptHTML_editForm')
    
    
        
InitializeClass(ReportScript)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.