Changeset 29

Show
Ignore:
Timestamp:
08/17/06 15:40:15 (2 years ago)
Author:
johnbywater
Message:

Added tests for meta domain model module.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/README

    r3 r29  
    22++++++++++++++++++++++ 
    33 
    4 DomainModel is an open-source (LGPL) framework for enterprise applications. 
     4DomainModel is an open-source framework for enterprise applications. 
    55 
    66It uses existing patterns of enterprise applications such as Model- 
     
    2323    http://www.kforgeproject.com/ 
    2424 
     25The DomainModel framework is written in Python. 
     26 
    2527 
    2628DomainModel Installation 
  • trunk/bin/domainmodel-admin

    r5 r29  
    1 #!/usr/bin/python 
     1#!/usr/bin/env python 
    22 
    3 # todo: support for verbosity 
     3import dm.cli.admin 
    44 
    5 import sys 
    6 import os 
    7 import cmd 
    8 import optparse 
     5dm.cli.admin.UtilityRunner() 
    96 
    10 class KforgeAdmin(cmd.Cmd): 
    11     "KForge command line interface." 
    12      
    13     def __init__(self, envPath=None, systemPath=None): 
    14         cmd.Cmd.__init__(self)  # Class cmd.Cmd isn't new style. 
    15         self.interactive = 0 
    16         self.envPath = None 
    17         self.systemPath = systemPath 
    18         if envPath is not None: 
    19             self.set_environment(envPath) 
    20      
    21     def set_environment(self, envPath): 
    22         self.envPath = os.path.abspath(envPath) 
    23         os.environ['KFORGEHOME'] = self.envPath # system dictionary needs this 
    24         self.prompt = 'Kforge [%s]$ ' % self.envPath 
    25      
    26     def run_interactive(self): 
    27         """Run an interactive session. 
    28         """ 
    29         print 'Welcome to the kforge-admin interactive mode\n' 
    30         print 'Type:  "?" or "help" for help on commands.\n' 
    31         self.do_about() 
    32         while 1: 
    33             try: 
    34                 self.cmdloop() 
    35                 break 
    36             except KeyboardInterrupt: 
    37                 raise 
    38             #    print '\n** Interrupt. Use "quit" to exit **' 
    39  
    40     # ========================= 
    41     # START OF Commands section 
    42  
    43     def do_data(self, line=None): 
    44         args = self._lineToArgs(line) 
    45         if len(args) != 1: 
    46             print 'ERROR: Insufficient arguments\n' 
    47             self.help_data(line) 
    48             return 1 
    49         elif args[0] == 'create': 
    50             from kforge.utils.admin import InitialiseEnvCmd 
    51             cmd = InitialiseEnvCmd(self.envPath, self.systemPath) 
    52             cmd.execute() 
    53             return 0 
    54         else: 
    55             self.help_data() 
    56             return 1 
    57  
    58     def help_data(self, line=None): 
    59         usage = \ 
    60 '''data <action> 
    61   action = create 
    62  
    63 Create environment data 
    64 ''' 
    65         print usage 
    66  
    67     def do_backup(self, line): 
    68         args = self._lineToArgs(line) 
    69         import kforge.application 
    70         app = kforge.application.Application() 
    71         if len(args) != 1: 
    72             print 'ERROR: Insufficient arguments\n' 
    73             self.help_backup(line) 
    74             return 1 
    75         else: 
    76             cmd = app.commands['Backup'](args[0]) 
    77             cmd.execute() 
    78             return 0 
    79  
    80     def help_backup(self, line=None): 
    81         usage  = 'backup dest\n' 
    82         usage += '\tdest is the path to which you wish to backup' 
    83         print usage 
    84       
    85     def do_db(self, line=None): 
    86         """Run db commands 
    87         """ 
    88         args = self._lineToArgs(line) 
    89         if len(args) != 1: 
    90             print 'ERROR: Insufficient arguments\n' 
    91             self.help_db(line) 
    92             return 1 
    93         # let's do this reflectively 
    94         from kforge.utils.db import Database 
    95         db = Database() 
    96         try: 
    97             Database.__dict__[args[0]](db) 
    98         except Exception, inst: 
    99             print 'Command failed. Details: %s' % inst 
    100             return 1 
    101      
    102     def help_db(self): 
    103         usage  = \ 
    104 '''db <action> 
    105   action = create | delete | init | rebuild  
    106  
    107 NB: a known issue is that due to a persisted db connection once you 
    108 have run init or rebuild you cannot run any of the other commands 
    109 again in the same session''' 
    110         # [[TODO: display docstrings for each function from db class here 
    111         # Database.__dict__[cmd].__doc__ 
    112         print usage 
    113  
    114     def do_upgrade(self, line=None): 
    115         # TODO use KForge version to specify upgrade script used 
    116         args = self._lineToArgs(line) 
    117         import kforge.utils.upgrade 
    118         if len(args) != 1: 
    119             self.help_upgrade(line) 
    120             return 1 
    121         elif args[0] == 'data': 
    122             cmd = kforge.utils.upgrade.UpgradeDataTo0Point11( 
    123                     self.envPath, self.systemPath) 
    124             cmd.execute() 
    125             return 0 
    126         elif args[0] == 'db': 
    127             cmd = kforge.utils.upgrade.UpgradeDbTo0Point11() 
    128             cmd.execute() 
    129             return 0 
    130         else: 
    131             print 'Unknown arguments: %s' % args 
    132             self.help_upgrade() 
    133  
    134     def help_upgrade(self, line=None): 
    135         usage = \ 
    136 '''upgrade <object> 
    137   object = data | db 
    138  
    139 Upgrade a KForge environment (data and db)''' 
    140         print usage 
    141      
    142     def do_www(self, line=None): 
    143         args = self._lineToArgs(line) 
    144         if len(args) == 0: 
    145             self.help_www(line) 
    146             return 1 
    147         import kforge 
    148         kforge.getA() # have to do this to set up features (dictionary etc) 
    149         from kforge.apache.apacheconfig import ApacheConfigBuilder 
    150         configBuilder = ApacheConfigBuilder() 
    151         if args[0] == 'build': 
    152             configBuilder.buildConfig() 
    153         elif args[0] == 'reload': 
    154             configBuilder.reloadConfig()  
    155         else: 
    156             self.help_www(line) 
    157             return 1 
    158  
    159     def help_www(self, line=None): 
    160         help = 'www [ build | reload ]\n' 
    161         help += '\tbuild: build web server configuration\n' 
    162         help += '\treload: reload web server configuration\n' 
    163         print help 
    164  
    165     def do_shell(self, line=None): 
    166         import code 
    167         code.interact() 
    168  
    169     def help_shell(self, line=None): 
    170         help = \ 
    171 '''shell (or !): run a python shell 
    172  
    173 Used to administer domain objects. Read docs/cli_shell.txt for a full 
    174 guide to use of the shell for administration of the domain model. 
    175  
    176 Preferred to direct invocation of a python shell as this ensures all relevant 
    177 environment variables are correctly set. 
    178 ''' 
    179         print help 
    180  
    181     def do_about(self, args=None): 
    182         import kforge 
    183         version = kforge.__version__ 
    184         about = \ 
    185 '''KForge version %s 
    186 Copyright the Open Knowledge Foundation. KForge is open-source software 
    187 licensed under the GPL v2.0. See COPYING for details. 
    188 ''' % version 
    189         print about 
    190  
    191     def do_help(self, line=None): 
    192         cmd.Cmd.do_help(self, line) 
    193          
    194     def do_quit(self, line=None): 
    195         sys.exit() 
    196      
    197     def do_EOF(self, *args): 
    198         print '' 
    199         sys.exit() 
    200  
    201     def _lineToArgs(self, line): 
    202         args = line.strip().split() 
    203         args = [ x.strip() for x in args] 
    204         return args 
    205  
    206 def main(): 
    207     usage  = \ 
    208 '''usage: %prog [options] [cmd] 
    209  
    210 Administer a KForge environment/instance and its associated domain objects.  
    211  
    212 Can be run in two modes: 
    213    1. single command: run the command provided and exit (Default) 
    214    2. interactive 
    215  
    216 To obtain information about the commands available run the "help" command. 
    217  
    218 Domain objects (e.g. persons, projects, etc) are administered by starting 
    219 a python shell from within interactive mode. Run "help shell" for more details. 
    220 ''' 
    221     parser = optparse.OptionParser(usage) 
    222     parser.add_option('-i', '--interactive', 
    223         action='store_true', dest='interactive', default=False, 
    224         help='Run in interactive mode. If this option is specified any commands at invocation will be ignored.' 
    225     ) 
    226     parser.add_option('--version', 
    227         action='store_true', dest='version', default=False, 
    228         help='Display version information' 
    229     ) 
    230     parser.add_option('--system', 
    231         action='store', dest='system_path', default=None, 
    232         help='If you have installed the KForge system to a path other\n' + \ 
    233           'than the default (sys.prefix) you need to specify the path here.' 
    234     ) 
    235     parser.add_option('--env', 
    236         action='store', dest='env_path', default='', 
    237         help='Path to environment you wish to administer. ' + \ 
    238           'If not provided use shell environment variable KFORGEHOME' 
    239     ) 
    240      
    241     (options, args) = parser.parse_args() 
    242     # concatenate all arguments back together for cmd.Cmd 
    243     line = '' 
    244     if len(args) > 0: 
    245         line = ' '.join(['%s' % s for s in args]) 
    246  
    247     # special cases  
    248     kforgeAdmin = KforgeAdmin() 
    249     if len(args) > 0 and args[0] in [ 'help' ]: 
    250         return kforgeAdmin.onecmd(line) 
    251     elif options.version: 
    252         return kforgeAdmin.onecmd('about') 
    253  
    254     # going to administer an environment  
    255     envPath = options.env_path 
    256     if not envPath: 
    257         if os.environ.has_key('KFORGEHOME'): 
    258             envPath = os.environ['KFORGEHOME'] 
    259         else: 
    260             print 'ERROR. Please provide a path to the KForge environment you wish to administer\n\n' 
    261             parser.print_help() 
    262             sys.exit(1) 
    263     kforgeAdmin = KforgeAdmin(envPath, options.system_path) 
    264     if options.interactive: 
    265         return kforgeAdmin.run_interactive() 
    266     elif len(args) > 0: 
    267         status = kforgeAdmin.onecmd(line) 
    268         sys.exit(status) 
    269     else: 
    270         parser.print_help() 
    271  
    272 if __name__ == '__main__': 
    273     # unittest.main() 
    274     main() 
  • trunk/src/dm/dom/base.py

    r19 r29  
    582582        "Returns register key for domain object." 
    583583        return getattr(self, self.registerKeyName) 
    584     
     584   
     585    # todo: rename to getLabel 
    585586    def getLabelValue(self): 
     587        return self.getRegisterKeyValue() 
     588 
     589    def getPersonalLabel(self): 
    586590        return self.getRegisterKeyValue() 
    587591 
  • trunk/src/dm/dom/meta.py

    r19 r29  
    165165 
    166166 
     167class Text(String): 
     168    "Models a domain object textual attribute." 
     169 
     170    def __init__(self, default='', **kwds): 
     171        super(Text, self).__init__(default=default, **kwds) 
     172 
     173 
     174class Url(String): 
     175    "Models a domain object url attribute." 
     176 
     177    def __init__(self, isRequired=False, **kwds): 
     178        super(Url, self).__init__(isRequired=isRequired, **kwds) 
     179 
     180 
    167181class Password(String): 
    168182    "Models a domain object password attribute." 
     
    196210 
    197211    def makeValueFromMultiValueDict(self, multiValueDict): 
    198         dateTimeString = multiValueDict[self.name] 
    199         return mx.DateTime.Parser.DateTimeFromString(dateTimeString) 
     212        dateTimeValue = multiValueDict[self.name] 
     213        return mx.DateTime.Parser.DateTimeFromString(str(dateTimeValue)) 
     214 
     215class Date(DateTime): 
     216    "Models a domain object date attribute." 
     217     
     218    pass 
    200219 
    201220 
  • trunk/src/dm/domtest.py

    r12 r29  
    11import unittest 
    22import dm.testunit 
     3import dm.dom.metatest 
    34import dm.dom.accesscontroltest 
    45import dm.dom.persontest 
     
    78    "Return a TestSuite of dm.db TestCases." 
    89    suites = [ 
     10        dm.dom.metatest.suite(), 
    911        dm.dom.accesscontroltest.suite(), 
    1012        dm.dom.persontest.suite(), 
  • trunk/src/dm/view/base.py

    r28 r29  
    737737        objectRegister = self.getManipulatedObjectRegister() 
    738738        objectRegisterCount = len(objectRegister) 
    739         isRegisterCountLow = objectRegisterCount < self.registerSizeThreshold, 
     739        isRegisterCountLow = objectRegisterCount < self.registerSizeThreshold 
     740        isRegisterCountSingle = objectRegisterCount == 1 
     741        isRegisterCountZero = objectRegisterCount == 0 
    740742        self.context.update({ 
    741743            'domainClassNameList' : domainClassRegister.keys(),  # todo: remove 
     
    745747            'registerCount'       : objectRegisterCount, 
    746748            'isRegisterCountLow'  : isRegisterCountLow, 
     749            'isRegisterCountSingle' : isRegisterCountSingle, 
     750            'isRegisterCountZero' : isRegisterCountZero, 
    747751        }) 
    748752 
     
    828832        super(AbstractReadView, self).setContext() 
    829833        self.context.update({ 
    830             'domainObjectNamedValues' : self.getDomainObjectAsNamedValues(), 
     834            'domainObjectPersonalLabel' : self.getDomainObjectPersonalLabel(), 
     835            'domainObjectNamedValues'  : self.getDomainObjectAsNamedValues(), 
    831836        }) 
    832837 
     
    834839        domainObject = self.getDomainObject() 
    835840        return domainObject.asNamedValues() 
     841         
     842    def getDomainObjectPersonalLabel(self): 
     843        domainObject = self.getDomainObject() 
     844        return domainObject.getPersonalLabel() 
    836845 
    837846 
  • trunk/src/dm/view/manipulator.py

    r17 r29  
    106106                ) 
    107107        elif metaAttr.isValueObject(): 
    108             field = djangoforms.TextField( 
    109                 field_name=metaAttr.name, 
    110                 is_required=metaAttr.isRequired, 
    111             ) 
     108            if metaAttr.typeName == 'Text': 
     109                field = djangoforms.LargeTextField( 
     110                    field_name=metaAttr.name, 
     111                    is_required=metaAttr.isRequired, 
     112                ) 
     113            elif metaAttr.typeName == 'Password': 
     114                field = djangoforms.PasswordField( 
     115                    field_name=metaAttr.name, 
     116                    is_required=metaAttr.isRequired, 
     117                ) 
     118            elif metaAttr.typeName == 'Integer': 
     119                field = djangoforms.IntegerField( 
     120                    field_name=metaAttr.name, 
     121                    is_required=metaAttr.isRequired, 
     122                ) 
     123            elif metaAttr.typeName == 'Url': 
     124                field = djangoforms.URLField( 
     125                    field_name=metaAttr.name, 
     126                    is_required=metaAttr.isRequired, 
     127                ) 
     128            elif metaAttr.typeName == 'DateTime': 
     129                field = djangoforms.DatetimeField( 
     130                    field_name=metaAttr.name, 
     131                    is_required=metaAttr.isRequired, 
     132                ) 
     133            elif metaAttr.typeName == 'Date': 
     134                field = djangoforms.DateField( 
     135                    field_name=metaAttr.name, 
     136                    is_required=metaAttr.isRequired, 
     137                ) 
     138            else: 
     139                field = djangoforms.TextField( 
     140                    field_name=metaAttr.name, 
     141                    is_required=metaAttr.isRequired, 
     142                ) 
    112143        if field: 
    113144            field.metaAttr = metaAttr 
  • trunk/src/dm/view/registry.py

    r28 r29  
    8585        for i in range(0, self.countRegistryPathNames(), 2): 
    8686            templatePath += self.getRegistryPathNames()[i] + '/' 
    87         templatePath += self.actionName 
     87        templatePath += self.getActionNameAsPath() 
    8888        return templatePath 
     89 
     90    def getActionNameAsPath(self): 
     91        return self.actionName[0].lower() + self.actionName[1:] 
    8992         
    9093    def isRegistryPathToRegister(self): 
     
    133136            'registryPath'  : self.registryPath, 
    134137            'registryAttribute'  : self.getRegistryPathNames()[0], 
    135             'registerName' : self.getRegistryPathNames()[-1], 
     138            'registerName' : registerName, 
     139            'registerNameTitle' : registerName[0].capitalize()+registerName[1:], 
    136140            'domainClassName' : self.getManipulatedObjectRegister().typeName, 
    137141            'actionName'    : self.actionName,