text: import os
  import uuid
  from ...tools import row2dict, xls_reader
  from datetime import datetime
  from sqlalchemy import not_, func
  from pyramid.view import (
      view_config,
      )
  from pyramid.httpexceptions import (
      HTTPFound,
      )
  import colander
  from deform import (
      Form,
      widget,
      ValidationFailure,
      )
  from ...models import (
      DBSession,
      Group
      )
      
  from datatables import ColumnDT, DataTables
  from ...views.base_view import BaseViews
      
  
  SESS_ADD_FAILED = 'Tambah group gagal'
  SESS_EDIT_FAILED = 'Edit group gagal'
  
  
                  
  class AddSchema(colander.Schema):
      group_name = colander.SchemaNode(
                      colander.String(),
                      validator=colander.Length(max=18))
                      
      description = colander.SchemaNode(
                      colander.String())
  class EditSchema(AddSchema):
      id = colander.SchemaNode(colander.String(),
              missing=colander.drop,
              widget=widget.HiddenWidget(readonly=True))
              
  class view_group(BaseViews):
      ########                    
      # List #
      ########    
      @view_config(route_name='group', renderer='templates/group/list.pt',
                   permission='read')
      def view_list(self):
          return dict(a={})
          
      ##########                    
      # Action #
      ##########    
      @view_config(route_name='group-act', renderer='json',
                   permission='read')
      def gaji_group_act(self):
          ses = self.request.session
          req = self.request
          params = req.params
          url_dict = req.matchdict
          
          if url_dict['act']=='grid':
              columns = []
              columns.append(ColumnDT('id'))
              columns.append(ColumnDT('group_name'))
              columns.append(ColumnDT('description'))
              columns.append(ColumnDT('member_count'))
              
              query = DBSession.query(Group)
              rowTable = DataTables(req, Group, query, columns)
              return rowTable.output_result()
          elif url_dict['act']=='headofnama':
              term = 'term' in params and params['term'] or '' 
              rows = DBSession.query(Group.id, Group.group_name
                        ).filter(
                        Group.group_name.ilike('%%%s%%' % term) ).all()
              r = []
              for k in rows:
                  d={}
                  d['id']          = k[0]
                  d['value']       = k[1]
                  r.append(d)
              return r                  
          
      #######    
      # Add #
      #######
      def form_validator(self, form, value):
          if 'id' in form.request.matchdict:
              uid = form.request.matchdict['id']
              q = DBSession.query(Group).filter_by(id=uid)
              group = q.first()
          else:
              group = None
                  
      def get_form(self, class_form, row=None):
          schema = class_form(validator=self.form_validator)
          schema = schema.bind()
          schema.request = self.request
          if row:
            schema.deserialize(row)
          return Form(schema, buttons=('simpan','batal'))
          
      def save(self, values, user, row=None):
          if not row:
              row = Group()
              row.created = datetime.now()
              row.create_uid = user.id
          row.from_dict(values)
          row.updated = datetime.now()
          row.update_uid = user.id
          row.disabled = 'disabled' in values and values['disabled'] and 1 or 0
          DBSession.add(row)
          DBSession.flush()
          return row
          
      def save_request(self, values, row=None):
          if 'id' in self.request.matchdict:
              values['id'] = self.request.matchdict['id']
          row = self.save(values, self.request.user, row)
          self.request.session.flash('group sudah disimpan.')
              
      def route_list(self):
          return HTTPFound(location=self.request.route_url('group'))
          
      def session_failed(self, session_name):
          r = dict(form=self.session[session_name])
          del self.session[session_name]
          return r
          
      @view_config(route_name='group-add', renderer='templates/group/add.pt',
                   permission='add')
      def view_group_add(self):
          req = self.request
          ses = self.session
          form = self.get_form(AddSchema)
          if req.POST:
              if 'simpan' in req.POST:
                  controls = req.POST.items()
                  try:
                      c = form.validate(controls)
                  except ValidationFailure, e:
                      req.session[SESS_ADD_FAILED] = e.render()               
                      return HTTPFound(location=req.route_url('group-add'))
                  self.save_request(dict(controls))
              return self.route_list()
          elif SESS_ADD_FAILED in req.session:
              return self.session_failed(SESS_ADD_FAILED)
          return dict(form=form.render())
  
          
      ########
      # Edit #
      ########
      def query_id(self):
          return DBSession.query(Group).filter_by(id=self.request.matchdict['id'])
          
      def id_not_found(self):    
          msg = 'group ID %s Tidak Ditemukan.' % self.request.matchdict['id']
          request.session.flash(msg, 'error')
          return route_list()
  
      @view_config(route_name='group-edit', renderer='templates/group/add.pt',
                   permission='edit')
      def view_group_edit(self):
          request = self.request
          row = self.query_id().first()
          if not row:
              return id_not_found(request)
          form = self.get_form(EditSchema)
          if request.POST:
              if 'simpan' in request.POST:
                  controls = request.POST.items()
                  print controls
                  try:
                      c = form.validate(controls)
                  except ValidationFailure, e:
                      request.session[SESS_EDIT_FAILED] = e.render()               
                      return HTTPFound(location=request.route_url('group-edit',
                                        id=row.id))
                  self.save_request(dict(controls), row)
              return self.route_list()
          elif SESS_EDIT_FAILED in request.session:
              return self.session_failed(SESS_EDIT_FAILED)
          values = row.to_dict()
          return dict(form=form.render(appstruct=values))
  
      ##########
      # Delete #
      ##########    
      @view_config(route_name='group-delete', renderer='templates/group/delete.pt',
                   permission='delete')
      def view_group_delete(self):
          request = self.request
          q = self.query_id()
          row = q.first()
          
          if not row:
              return self.id_not_found(request)
          form = Form(colander.Schema(), buttons=('hapus','batal'))
          if request.POST:
              if 'hapus' in request.POST:
                  msg = 'group ID %d %s sudah dihapus.' % (row.id, row.description)
                  try:
                    q.delete()
                    DBSession.flush()
                  except:
                    msg = 'group ID %d %s tidak dapat dihapus.' % (row.id, row.description)
                  request.session.flash(msg)
              return self.route_list()
          return dict(row=row,
                       form=form.render())
  
      ##########                    
      # CSV #
      ##########    
          
      @view_config(route_name='group-csv', renderer='csv',
                   permission='read')
      def export_csv(self):
          request = self.request
          
          query = DBSession.query(Group.group_name.label('kode'), Group.description, Group.member_count.label('anggota'))
                                            
          r = query.first()
          header = r.keys()
          query = query.all()
          rows = []
          for item in query:
              rows.append(list(item))
  
          # override attributes of response
          filename = 'group%s.csv' % datetime.now().strftime('%Y%m%d%H%M%S')
  
          self.request.response.content_disposition = 'attachment;filename=' + filename
  
          return {
            'header': header,
            'rows': rows,
          }
  