Esempio n. 1
0
    def get(self):
        request = self.request
        context = self.request.context

        cm_id = self._get_cmid()
        if cm_id == 'new':
            is_alt_area = not not request.params.get('altarea')

        community = context.community
        descriptions = context.descriptions
        alt_names = context.alt_names
        alt_areas = context.alt_areas

        if cm_id != 'new':
            is_alt_area = community.AlternativeArea

        prov_state = []
        with request.connmgr.get_connection() as conn:
            prov_state = list(map(tuple, conn.execute('SELECT ProvID, ProvinceStateCountry FROM dbo.vw_ProvinceStateCountry ORDER BY DisplayOrder, ProvinceStateCountry').fetchall()))

        if community:
            community.ChildCommunities = xml_to_dict_list(community.ChildCommunities)
            community.AltSearchArea = xml_to_dict_list(community.AltSearchArea)

        data = request.model_state.form.data
        data['community'] = community
        data['descriptions'] = descriptions
        data['alt_names'] = alt_names
        data['alt_areas'] = [str(x.Search_CM_ID) for x in alt_areas]

        alt_area_name_map = {str(x[0]): x[1] for x in alt_areas}

        return {'community': community, 'alt_area_name_map': alt_area_name_map, 'is_alt_area': is_alt_area, 'prov_state': prov_state}
Esempio n. 2
0
    def index(self):
        request = self.request

        rejected_requests = None
        with request.connmgr.get_connection() as conn:
            users = conn.execute('EXEC sp_Users_l').fetchall()

            show_rejected = not not request.params.get('show_rejected')

            cursor = conn.execute('EXEC sp_Users_AccountRequest_l ?',
                                  show_rejected)

            user_requests = cursor.fetchall()

            if not show_rejected:
                cursor.nextset()
                rejected_requests = cursor.fetchone()

            cursor.close()

        for user in users:
            user.ManageCommunities = [
                x['Name'] for x in xml_to_dict_list(user.ManageCommunities)
            ]
            user.ManageExternalSystems = [
                x['Name'] for x in xml_to_dict_list(user.ManageExternalSystems)
            ]

        return {
            'users': users,
            'user_requests': user_requests,
            'rejected_requests': rejected_requests
        }
Esempio n. 3
0
    def get(self):
        request = self.request
        context = self.request.context

        cm_id = self._get_cmid()
        if cm_id == 'new':
            is_alt_area = not not request.params.get('altarea')

        community = context.community
        descriptions = context.descriptions
        alt_names = context.alt_names
        alt_areas = context.alt_areas

        if cm_id != 'new':
            is_alt_area = community.AlternativeArea

        prov_state = []
        with request.connmgr.get_connection() as conn:
            prov_state = map(tuple, conn.execute('SELECT ProvID, ProvinceStateCountry FROM dbo.vw_ProvinceStateCountry ORDER BY DisplayOrder, ProvinceStateCountry').fetchall())

        if community:
            community.ChildCommunities = xml_to_dict_list(community.ChildCommunities)
            community.AltSearchArea = xml_to_dict_list(community.AltSearchArea)

        data = request.model_state.form.data
        data['community'] = community
        data['descriptions'] = descriptions
        data['alt_names'] = alt_names
        data['alt_areas'] = [str(x.Search_CM_ID) for x in alt_areas]

        alt_area_name_map = {str(x[0]): x[1] for x in alt_areas}

        return {'community': community, 'alt_area_name_map': alt_area_name_map, 'is_alt_area': is_alt_area, 'prov_state': prov_state}
Esempio n. 4
0
    def json_community(self):
        request = self.request

        validator = validators.IntID(not_empty=True)
        try:
            cm_id = validator.to_python(request.matchdict.get('cmid'))
        except validators.Invalid as e:
            return {'fail': True, 'reason': e.msg}

        community = None
        with request.connmgr.get_connection() as conn:
            community = conn.execute('EXEC sp_Community_s_MoreInfo ?',
                                     cm_id).fetchone()

        _ = request.translate
        if not community:
            return {'fail': True, 'reason': _('Community Not Found.')}

        pcn = xml_to_dict_list(community.ParentCommunityName)
        if pcn:
            pcn = pcn[0]
        community.ParentCommunityName = pcn

        community.OtherNames = xml_to_dict_list(community.OtherNames)
        community.ChildCommunities = xml_to_dict_list(
            community.ChildCommunities)
        community.SearchCommunities = xml_to_dict_list(
            community.SearchCommunities)
        community.Managers = xml_to_dict_list(community.Managers)

        community_info = render('community_more_details.mak',
                                {'community': community}, request)
        if community.ParentCommunityName:
            cm_title = _('%s (in %s)') % (
                community.Name, community.ParentCommunityName['Name'])
        else:
            cm_title = community.Name

        return {
            'fail': False,
            'community_info': community_info,
            'community_name': cm_title
        }
Esempio n. 5
0
    def index(self):
        request = self.request

        rejected_requests = None
        with request.connmgr.get_connection() as conn:
            users = conn.execute('EXEC sp_Users_l').fetchall()

            show_rejected = not not request.params.get('show_rejected')

            cursor = conn.execute('EXEC sp_Users_AccountRequest_l ?', show_rejected)

            user_requests = cursor.fetchall()

            if not show_rejected:
                cursor.nextset()
                rejected_requests = cursor.fetchone()

            cursor.close()

        for user in users:
            user.ManageCommunities = [x['Name'] for x in xml_to_dict_list(user.ManageCommunities)]
            user.ManageExternalSystems = [x['Name'] for x in xml_to_dict_list(user.ManageExternalSystems)]

        return {'users': users, 'user_requests': user_requests, 'rejected_requests': rejected_requests}
Esempio n. 6
0
    def post(self):
        request = self.request

        is_alt_area = not not request.params.get('altarea')

        cm_id = self._get_cmid()

        if cm_id != 'new' and request.params.get('Delete'):
            reason = request.params.get('ReasonForChange')
            if reason:
                request.session.flash((cm_id, reason), 'ReasonForChange')
            return HTTPFound(location=request.route_url('community_delete', cmid=cm_id))

        model_state = request.model_state
        model_state.form.variable_decode = True
        if is_alt_area:
            log.debug('alt area')
            model_state.schema = AltAreasSchema()
        else:
            log.debug('community')
            model_state.schema = CommunitySchema()

        if model_state.validate():
            data = model_state.form.data
            cm_data = data.get('community', {})
            args = [cm_id if cm_id != 'new' else None,
                    request.user.User_ID, is_alt_area, cm_data.get('ParentCommunity'),
                    cm_data.get('ProvinceState'), data.get('ReasonForChange')]

            root = ET.Element('DESCS')

            for culture, description in model_state.form.data['descriptions'].items():

                desc = ET.SubElement(root, 'DESC')
                ET.SubElement(desc, "Culture").text = culture.replace('_', '-')
                for name, value in description.items():
                    if value:
                        ET.SubElement(desc, name).text = value

            args.append(ET.tostring(root))

            root = ET.Element('NAMES')

            for name in model_state.form.data.get('alt_names') or []:
                if name.get('Delete') or not name.get('AltName'):
                    continue

                desc = ET.SubElement(root, 'Name')
                ET.SubElement(desc, 'Culture').text = name['Culture']
                ET.SubElement(desc, 'AltName').text = name['AltName']

            args.append(ET.tostring(root))

            if is_alt_area:
                root = ET.Element('ALTAREAS')
                for area in data.get('alt_areas') or []:
                    ET.SubElement(root, 'CM_ID').text = str(area)

                args.append(ET.tostring(root))

            else:
                args.append(None)

            sql = '''
                DECLARE @RC int, @CM_ID int, @ErrMsg nvarchar(500)

                SET @CM_ID = ?

                EXEC @RC = sp_Community_u @CM_ID OUTPUT, %s, @ErrMsg OUTPUT

                SELECT @RC AS [Return], @CM_ID AS CM_ID, @ErrMsg AS ErrMsg

                ''' % (', '.join('?' * (len(args) - 1)))

            with request.connmgr.get_connection() as conn:
                result = conn.execute(sql, args).fetchone()

            if not result.Return:
                _ = request.translate
                if is_alt_area:
                    msg = _('Alternate search area saved.')
                else:
                    msg = _('Community saved.')

                request.session.flash(msg)
                kw = {}
                if cm_id == 'new':
                    kw['cmid'] = result.CM_ID
                return HTTPFound(location=request.current_route_url(**kw))

            model_state.add_error_for('*', result.ErrMsg)

            alt_areas = data.get('alt_areas') or []
        else:
            data = model_state.data
            decoded = variable_decode(request.POST)
            alt_areas = decoded.get('alt_areas') or []

            data['alt_areas'] = alt_areas
            data['alt_names'] = decoded.get('alt_names') or []

        community = request.context.community
        if cm_id != 'new':
            is_alt_area = community.AlternativeArea

        prov_state = []
        alt_area_name_map = {}
        with request.connmgr.get_connection() as conn:
            prov_state = list(map(tuple, conn.execute('SELECT ProvID, ProvinceStateCountry FROM dbo.vw_ProvinceStateCountry ORDER BY DisplayOrder, ProvinceStateCountry').fetchall()))

            if is_alt_area:
                alt_area_name_map = {str(x[0]): x[1] for x in conn.execute('EXEC sp_Community_ls_Names ?', ','.join(str(x) for x in alt_areas)).fetchall()}

        if community:
            community.ChildCommunities = xml_to_dict_list(community.ChildCommunities)
            community.AltSearchArea = xml_to_dict_list(community.AltSearchArea)

        log.debug('errors:', model_state.form.errors)

        return {'community': community, 'alt_area_name_map': alt_area_name_map,
                'is_alt_area': is_alt_area, 'prov_state': prov_state}
Esempio n. 7
0
    def post(self):
        request = self.request

        is_alt_area = not not request.params.get('altarea')

        cm_id = self._get_cmid()

        if cm_id != 'new' and request.params.get('Delete'):
            reason = request.params.get('ReasonForChange')
            if reason:
                request.session.flash((cm_id, reason), 'ReasonForChange')
            return HTTPFound(location=request.route_url('community_delete', cmid=cm_id))

        model_state = request.model_state
        model_state.form.variable_decode = True
        if is_alt_area:
            log.debug('alt area')
            model_state.schema = AltAreasSchema()
        else:
            log.debug('community')
            model_state.schema = CommunitySchema()

        if model_state.validate():
            data = model_state.form.data
            cm_data = data.get('community', {})
            args = [cm_id if cm_id != 'new' else None,
                    request.user.User_ID, is_alt_area, cm_data.get('ParentCommunity'),
                    cm_data.get('ProvinceState'), data.get('ReasonForChange')]

            root = ET.Element('DESCS')

            for culture, description in model_state.form.data['descriptions'].iteritems():

                desc = ET.SubElement(root, 'DESC')
                ET.SubElement(desc, "Culture").text = culture.replace('_', '-')
                for name, value in description.iteritems():
                    if value:
                        ET.SubElement(desc, name).text = value

            args.append(ET.tostring(root))

            root = ET.Element('NAMES')

            for name in model_state.form.data.get('alt_names') or []:
                if name.get('Delete') or not name.get('AltName'):
                    continue

                desc = ET.SubElement(root, 'Name')
                ET.SubElement(desc, 'Culture').text = name['Culture']
                ET.SubElement(desc, 'AltName').text = name['AltName']

            args.append(ET.tostring(root))

            if is_alt_area:
                root = ET.Element('ALTAREAS')
                for area in data.get('alt_areas') or []:
                    ET.SubElement(root, 'CM_ID').text = unicode(area)

                args.append(ET.tostring(root))

            else:
                args.append(None)

            sql = '''
                DECLARE @RC int, @CM_ID int, @ErrMsg nvarchar(500)

                SET @CM_ID = ?

                EXEC @RC = sp_Community_u @CM_ID OUTPUT, %s, @ErrMsg OUTPUT

                SELECT @RC AS [Return], @CM_ID AS CM_ID, @ErrMsg AS ErrMsg

                ''' % (', '.join('?' * (len(args) - 1)))

            with request.connmgr.get_connection() as conn:
                result = conn.execute(sql, args).fetchone()

            if not result.Return:
                _ = request.translate
                if is_alt_area:
                    msg = _('Alternate search area saved.')
                else:
                    msg = _('Community saved.')

                request.session.flash(msg)
                kw = {}
                if cm_id == 'new':
                    kw['cmid'] = result.CM_ID
                return HTTPFound(location=request.current_route_url(**kw))

            model_state.add_error_for('*', result.ErrMsg)

            alt_areas = data.get('alt_areas') or []
        else:
            data = model_state.data
            decoded = variable_decode(request.POST)
            alt_areas = decoded.get('alt_areas') or []

            data['alt_areas'] = alt_areas
            data['alt_names'] = decoded.get('alt_names') or []

        community = request.context.community
        if cm_id != 'new':
            is_alt_area = community.AlternativeArea

        prov_state = []
        alt_area_name_map = {}
        with request.connmgr.get_connection() as conn:
            prov_state = map(tuple, conn.execute('SELECT ProvID, ProvinceStateCountry FROM dbo.vw_ProvinceStateCountry ORDER BY DisplayOrder, ProvinceStateCountry').fetchall())

            if is_alt_area:
                alt_area_name_map = {str(x[0]): x[1] for x in conn.execute('EXEC sp_Community_ls_Names ?', ','.join(str(x) for x in alt_areas)).fetchall()}

        if community:
            community.ChildCommunities = xml_to_dict_list(community.ChildCommunities)
            community.AltSearchArea = xml_to_dict_list(community.AltSearchArea)

        log.debug('errors:', model_state.form.errors)

        return {'community': community, 'alt_area_name_map': alt_area_name_map,
                'is_alt_area': is_alt_area, 'prov_state': prov_state}
Esempio n. 8
0
class Communities(ViewBase):
    @view_config(route_name="communities",
                 renderer='communities.mak',
                 permission='view')
    def index(self):
        request = self.request

        communities = []

        external_system_code = request.params.get('ExternalSystem')
        with request.connmgr.get_connection() as conn:
            sql = 'EXEC sp_Community_l ?, ? ; EXEC sp_External_System_l'
            cursor = conn.execute(sql, (request.user and request.user.User_ID),
                                  external_system_code)

            communities = cursor.fetchall()

            cursor.nextset()

            external_systems = map(tuple, cursor.fetchall())

            cursor.close()

        communities = {
            k: list(g)
            for k, g in groupby(communities, attrgetter('ParentCommunity'))
        }

        request.model_state.form.data['ExternalSystem'] = external_system_code

        return {
            'communities': communities,
            'external_systems': external_systems
        }

    @view_config(route_name="search",
                 renderer='results.mak',
                 permission='view')
    def search(self):
        request = self.request

        model_state = request.model_state
        model_state.validators = {
            'terms': validators.UnicodeString(not_empty=True)
        }
        model_state.method = None

        communities = []
        if model_state.validate():
            with request.connmgr.get_connection() as conn:
                communities = conn.execute(
                    'EXEC sp_Community_ls ?,?',
                    (request.user and request.user.User_ID),
                    model_state.value('terms'))

        return {'communities': communities}

    @view_config(route_name="json_community",
                 renderer='json',
                 permission='view')
    def json_community(self):
        request = self.request

        validator = validators.IntID(not_empty=True)
        try:
            cm_id = validator.to_python(request.matchdict.get('cmid'))
        except validators.Invalid, e:
            return {'fail': True, 'reason': e.message}

        community = None
        with request.connmgr.get_connection() as conn:
            community = conn.execute('EXEC sp_Community_s_MoreInfo ?',
                                     cm_id).fetchone()

        _ = request.translate
        if not community:
            return {'fail': True, 'reason': _('Community Not Found.')}

        pcn = xml_to_dict_list(community.ParentCommunityName)
        if pcn:
            pcn = pcn[0]
        community.ParentCommunityName = pcn

        community.OtherNames = xml_to_dict_list(community.OtherNames)
        community.ChildCommunities = xml_to_dict_list(
            community.ChildCommunities)
        community.SearchCommunities = xml_to_dict_list(
            community.SearchCommunities)
        community.Managers = xml_to_dict_list(community.Managers)

        community_info = render('community_more_details.mak',
                                {'community': community}, request)
        if community.ParentCommunityName:
            cm_title = _('%s (in %s)') % (
                community.Name, community.ParentCommunityName['Name'])
        else:
            cm_title = community.Name

        return {
            'fail': False,
            'community_info': community_info,
            'community_name': cm_title
        }