text: '''
  Created on Jun 10, 2014
  
  @author: Dan, alexandru-m-g
  '''
  
  import logging as logging
  
  import ckan.lib.helpers as h
  import ckan.logic as logic
  import ckan.lib.base as base
  import ckan.plugins.toolkit as tk
  from ckan.common import c, request, _
  import ckan.model as model
  
  import ckanext.hdx_org_group.helpers.static_lists as static_lists
  import ckanext.hdx_theme.helpers.helpers as hdx_h
  import ckanext.hdx_theme.util.mail as hdx_mail
  
  log = logging.getLogger(__name__)
  
  
  class HDXReqsOrgController(base.BaseController):
  
      # def request_membership(self, org_id):
      #     '''
      #         user_email, name of user, username, organization name,  list with org-admins emails,
      #     '''
      #     try:
      #         msg = request.params.get('message', '')
      #         user = hdx_h.hdx_get_user_info(c.user)
      #         context = {'model': model, 'session': model.Session,
      #                    'user': c.user or c.author}
      #         org_admins = tk.get_action('member_list')(context,{'id':org_id,'capacity':'admin','object_type':'user'})
      #         admins=[]
      #         for admin_tuple in org_admins:
      #             admin_id = admin_tuple[0]
      #             admins.append(hdx_h.hdx_get_user_info(admin_id))
      #         admins_with_email = [admin for admin in admins if admin['email']]
      #
      #         data_dict = {'display_name': user['display_name'], 'name': user['name'],
      #                      'email': user['email'], 'organization': org_id,
      #                      'message': msg, 'admins': admins_with_email}
      #         tk.get_action('hdx_send_request_membership')(context, data_dict)
      #
      #         h.flash_success(_('Message sent'))
      #     except hdx_mail.NoRecipientException, e:
      #         h.flash_error(_(str(e)))
      #     except exceptions.Exception, e:
      #         log.error(str(e))
      #         h.flash_error(_('Request can not be sent. Contact an administrator.'))
      #     h.redirect_to(controller='organization', action='read', id=org_id)
  
      def request_editor_for_org(self, org_id):
          '''
              user_email, name of user, username, organization name,  list with org-admins emails,
          '''
          try:
              msg = _('Please allow me to submit data to this organization ')
              user = hdx_h.hdx_get_user_info(c.user)
              context = {'model': model, 'session': model.Session,
                         'user': c.user or c.author}
              org_admins = tk.get_action('member_list')(context,{'id':org_id,'capacity':'admin','object_type':'user'})
              admins=[]
              for admin_tuple in org_admins:
                  admin_id = admin_tuple[0]
                  admins.append(hdx_h.hdx_get_user_info(admin_id))
              admins_with_email = [admin for admin in admins if admin['email']]
  
              data_dict = {'display_name': user['display_name'], 'name': user['name'],
                           'email': user['email'], 'organization': org_id,
                           'message': msg, 'admins': admins_with_email}
              tk.get_action('hdx_send_editor_request_for_org')(context, data_dict)
              h.flash_success(_('Message sent'))
          except hdx_mail.NoRecipientException, e:
              h.flash_error(_(str(e)))
          except:
              h.flash_error(_('Request can not be sent. Contact an administrator'))
          if 'from' in request.params:
              h.redirect_to(request.params.get('from'))
          h.redirect_to(controller='organization', action='read', id=org_id)
  
      def request_new_organization(self):
          context = {'model': model, 'session': model.Session, 'user': c.user or c.author}
          try:
              tk.check_access('hdx_send_new_org_request',context)
          except logic.NotAuthorized:
              base.abort(403, _('Unauthorized to send a new org request'))
  
          errors = {}
          error_summary = {}
          data = {'from': request.params.get('from','')}
  
          sent_successfully = False
          if 'save' in request.params:
              try:
                  data = self._process_new_org_request()
                  self._validate_new_org_request_field(data)
  
                  tk.get_action('hdx_send_new_org_request')(context, data)
  
                  data.clear()
                  h.flash_success(_('Request sent successfully'))
                  sent_successfully = True
              except hdx_mail.NoRecipientException, e:
                  h.flash_error(_(str(e)))
              except logic.ValidationError, e:
                  errors = e.error_dict
                  error_summary = e.error_summary
              except Exception, e:
                  log.error(str(e))
                  h.flash_error(_('Request can not be sent. Contact an administrator'))
              if sent_successfully:
                  h.redirect_to('user_dashboard_organizations')
  
          vars = {'data': data, 'errors': errors,
                  'error_summary': error_summary, 'action': 'new'}
  
          c.hdx_org_type_list = [{'value': '-1', 'text': _('-- Please select --')}] + \
                                [{'value': t[1], 'text': _(t[0])} for t in static_lists.ORGANIZATION_TYPE_LIST]
          c.form = base.render('organization/request_organization_form.html', extra_vars=vars)
          return base.render('organization/request_new.html')
  
      def _process_new_org_request(self):
          hdx_org_type = None
          hdx_org_type_code = request.params.get('hdx_org_type', '')
  
          if hdx_org_type_code:
              hdx_org_type = next(
                  (type[0] for type in static_lists.ORGANIZATION_TYPE_LIST if type[1] == hdx_org_type_code), '-1')
  
          data = {'name': request.params.get('name', ''), \
                  'title': request.params.get('title', ''), \
                  'org_url': request.params.get('org_url', ''), \
                  'description': request.params.get('description', ''), \
                  'your_email': request.params.get('your_email', ''), \
                  'your_name': request.params.get('your_name', ''), \
                  'org_acronym': request.params.get('org_acronym', ''), \
                  'hdx_org_type': hdx_org_type, \
  
                  }
          return data
  
      def _validate_new_org_request_field(self, data):
          errors = {}
          for field in ['title', 'description', 'your_email', 'your_name', 'hdx_org_type']:
              if data[field].strip() in ['', '-1']:
                  errors[field] = [_('should not be empty')]
  
          if len(errors) > 0:
              raise logic.ValidationError(errors)
  