Пример #1
0
class ClientViewer(BaseViewer):
    def __init__(self, request):
        BaseViewer.__init__(self, request)
        prepare_main_layout(self.request)
        self.clients = ClientManager(self.request.db)
        self.contacts = ContactManager(self.request.db)
        self._dispatch_table = dict(
            list=self.list_clients,
            add=self.add_client,
            delete=self.delete_client,
            confirmdelete=self.confirm_delete_client,
            editclient=self.edit_client,
            viewclient=self.view_client,)
        self.context = self.request.matchdict['context']
        self._view = self.context

        menu = BaseMenu()
        menu.set_header('Actions')
        url = self.url(context='add', id='somebody')
        menu.append_new_entry("Add Client", url)
        self.layout.options_menus = dict(actions=menu)
        
        self.dispatch()

    def list_clients(self):
        clients = self.clients.all()
        env = dict(clients=clients)
        template = 'trumpet:templates/consult/listclients.mako'
        self.layout.content = self.render(template, env)
        
    def _make_form(self):
        schema = AddClientSchema()
        clist = self.contacts.all()
        choices = [(c.id, '%s %s' % (c.firstname, c.lastname)) for c in  clist]
        schema['contact'].widget = make_select_widget(choices)
        form = deform.Form(schema, buttons=('submit',))
        self.layout.resources.deform_auto_need(form)
        return schema, form
    
    def add_client(self):
        schema, form = self._make_form()
        if 'submit' in self.request.POST:
            controls = self.request.POST.items()
            self.layout.subheader = 'Client Submitted'
            try:
                data = form.validate(controls)
            except deform.ValidationFailure, e:
                self.layout.content = e.render()
                return
            name = data['name']
            contact_id = int(data['contact'])
            address = data['address']
            description = data['description']
            c = self.clients.add(name, contact_id, address, description)
            content = '<p>Client %s added.</p>' % c.name
            self.layout.content = content
            return
        rendered = form.render()
        self.layout.content = rendered
        self.layout.subheader = 'Add a Client'
Пример #2
0
    def __init__(self, request):
        BaseViewer.__init__(self, request)
        prepare_main_layout(self.request)
        self.clients = ClientManager(self.request.db)
        self.contacts = ContactManager(self.request.db)
        self._dispatch_table = dict(
            list=self.list_clients,
            add=self.add_client,
            delete=self.delete_client,
            confirmdelete=self.confirm_delete_client,
            editclient=self.edit_client,
            viewclient=self.view_client,)
        self.context = self.request.matchdict['context']
        self._view = self.context

        menu = BaseMenu()
        menu.set_header('Actions')
        url = self.url(context='add', id='somebody')
        menu.append_new_entry("Add Client", url)
        self.layout.options_menus = dict(actions=menu)
        
        self.dispatch()