示例#1
0
    def set_aggregation_path(self, name, path):
        assert self._doc is not None
        assert is_string(name) and is_not_empty(name)
        assert is_sequence(path) and is_not_empty(path)

        assert isinstance(self._doc[attributes.AGG_PATHS], dict)
        self._doc[attributes.AGG_PATHS][name] = list(path)
示例#2
0
    def __init__(self, dbm, name=None, label=None, form_code=None, fields=None, entity_type=None, type=None,
                 language="eng", state=attributes.ACTIVE_STATE):
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_not_empty(name)
        assert fields is None or is_sequence(fields)
        assert form_code is None or (is_string(form_code) and is_not_empty(form_code))
        assert type is None or is_not_empty(type)
        assert entity_type is None or is_sequence(entity_type)

        DataObject.__init__(self, dbm)

        self._form_fields = []
        self.errors = []

        # Are we being constructed from scratch or existing doc?
        if name is None:
            return

        # Not made from existing doc, so build ourselves up
        self._form_fields = fields
        self.validate_fields()

        doc = FormModelDocument()
        doc.name = name
        doc.add_label(language, label)
        doc.form_code = form_code
        doc.entity_type = entity_type
        doc.type = type
        doc.state = state
        doc.active_languages = language
        DataObject._set_document(self, doc)
示例#3
0
def create_question(post_dict, dbm):
    options = post_dict.get('options')
    datadict_type = options.get('ddtype') if options is not None else None
    if is_not_empty(datadict_type):
        #  question already has a data dict type
        datadict_slug = datadict_type.get('slug')
    else:
        datadict_slug = str(slugify(unicode(post_dict.get('title'))))
    ddtype = get_or_create_data_dict(dbm=dbm,
                                     name=post_dict.get('code'),
                                     slug=datadict_slug,
                                     primitive_type=post_dict.get('type'),
                                     description=post_dict.get('title'))

    if post_dict["type"] == "text":
        return _create_text_question(post_dict, ddtype)
    if post_dict["type"] == "integer":
        return _create_integer_question(post_dict, ddtype)
    if post_dict["type"] == "geocode":
        return _create_geo_code_question(post_dict, ddtype)
    if post_dict["type"] == "select":
        return _create_select_question(post_dict,
                                       single_select_flag=False,
                                       ddtype=ddtype)
    if post_dict["type"] == "date":
        return _create_date_question(post_dict, ddtype)
    if post_dict["type"] == "select1":
        return _create_select_question(post_dict,
                                       single_select_flag=True,
                                       ddtype=ddtype)
示例#4
0
 def complete_address(self, obj):
     complete_address = [
         obj.address, obj.addressline2, obj.city, obj.zipcode, obj.state,
         obj.country_name()
     ]
     return ", ".join(
         [element for element in complete_address if is_not_empty(element)])
示例#5
0
def entities_exists_with_value(dbm, entity_type, label, value):
    """
    Returns true if entity with the given value for the label exists
    """
    assert isinstance(dbm, DatabaseManager)
    assert is_string(label)
    rows = dbm.load_all_rows_in_view(u'entity_by_label_value', key=[entity_type, label, value])
    return is_not_empty(rows)
示例#6
0
 def _get_ngo_admin(self, organization):
     user_profiles = NGOUserProfile.objects.filter(
         org_id=organization.org_id)
     admin_users = [
         x.user for x in user_profiles
         if x.user.groups.filter(name="NGO Admins")
     ]
     #right now there is only one ngo admin
     return admin_users[0] if is_not_empty(admin_users) else NullAdmin()
示例#7
0
 def send_sms(self, from_tel, to_tel, message, message_type="Unknown"):
     message = strip_accents(message)
     if is_not_empty(from_tel):
         organization_setting = OrganizationFinder(
         ).find_organization_setting(from_tel)
         is_there_orgnization_with_to_number = OrganizationFinder(
         ).find_organization_setting(to_tel) != None
         if (is_there_orgnization_with_to_number): return False
         smsc = None
         if organization_setting is not None and organization_setting.outgoing_number is not None:
             smsc = organization_setting.outgoing_number.smsc
         if smsc is None:
             logger.error("No SMSC configured for %s" %
                          organization_setting.organization.org_id)
             raise NoSMSCException()
         socket.setdefaulttimeout(120)
         logger.debug("Posting sms to %s" % settings.VUMI_API_URL)
         if settings.USE_NEW_VUMI:
             client = VumiApiClient(
                 connection=Connection(smsc.vumi_username,
                                       smsc.vumi_username,
                                       base_url=settings.VUMI_API_URL))
             sms_response = client.send_sms(
                 to_addr=to_tel,
                 from_addr=from_tel,
                 content=message.encode('utf-8'),
                 transport_name=smsc.vumi_username)
             return sms_response[0]
         else:
             try:
                 client = VumiClient(None,
                                     None,
                                     connection=Connection(
                                         smsc.vumi_username,
                                         smsc.vumi_username,
                                         base_url=settings.VUMI_API_URL))
                 resp = client.send_sms(to_msisdn=to_tel,
                                        from_msisdn=from_tel,
                                        message=message.encode('utf-8'))
                 response_object = json.loads(resp.content)
                 if response_object:
                     log_sms(to_tel, from_tel, message,
                             organization_setting.organization,
                             response_object[0].get('id'),
                             smsc.vumi_username, message_type)
                 return True
             except (URLError,
                     VumiInvalidDestinationNumberException) as err:
                 logger.exception('Unable to send sms. %s' % err)
                 return False
             except socket.timeout:
                 logger.exception(
                     'Request timed-out. Organization: %s, From: %s, To: %s.'
                     % (organization_setting, from_tel, to_tel))
                 return False
     return False
示例#8
0
    def __init__(self,
                 dbm,
                 name=None,
                 label=None,
                 form_code=None,
                 fields=None,
                 entity_type=None,
                 type=None,
                 language="eng",
                 state=attributes.ACTIVE_STATE):
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_not_empty(name)
        assert fields is None or is_sequence(fields)
        assert form_code is None or (is_string(form_code)
                                     and is_not_empty(form_code))
        assert type is None or is_not_empty(type)
        assert entity_type is None or is_sequence(entity_type)

        DataObject.__init__(self, dbm)

        self._form_fields = []
        self.errors = []

        # Are we being constructed from scratch or existing doc?
        if name is None:
            return

        # Not made from existing doc, so build ourselves up
        self._form_fields = fields
        self.validate_fields()

        doc = FormModelDocument()
        doc.name = name
        doc.add_label(language, label)
        doc.form_code = form_code
        doc.entity_type = entity_type
        doc.type = type
        doc.state = state
        doc.active_languages = language
        DataObject._set_document(self, doc)
示例#9
0
 def _set_location_data(self, values):
     location_field_code = self._get_location_field_code()
     if location_field_code is None:
         return
     geo_field_code = self._get_geo_field_code()
     display_location, geo_code = values.get(location_field_code), values.get(geo_field_code)
     location_hierarchy = self._get_location_hierarchy_from_location_name(display_location)
     tree = self.location_tree
     if location_hierarchy is [] and is_not_empty(geo_code):
         try:
             lat_string, long_string = tuple(geo_code.split())
             location_hierarchy = tree.get_location_hierarchy_for_geocode(lat=float(lat_string),
                 long=float(long_string))
         except ValueError as e:
             raise GeoCodeFormatException(e.args)
     elif is_not_empty(location_hierarchy) and is_empty(geo_code):
         try:
             translated_geo_code = tree.get_centroid(display_location.split(',')[0], len(location_hierarchy) - 1)
             values[geo_field_code] = "%s %s" % (translated_geo_code[1], translated_geo_code[0])
         except Exception:
             pass
     values[location_field_code] = location_hierarchy
示例#10
0
    def test_should_process_registration_data_for_paid_acccount_in_english(
            self):
        with patch.object(ProSMSAccountRegistrationProcessor,
                          '_get_invoice_total') as get_invoice_total_patch:
            get_invoice_total_patch.return_value = PRO_SMS_MONTHLY_PRICING, '1 month'
            processor = self.processor.get_registration_processor(
                self.paid_organization)

            site = Site(domain='test', name='test_site')
            kwargs = dict(invoice_period='', preferred_payment='')

            processor.process(self.user1, site, 'en', kwargs)

            emails = [mail.outbox.pop() for i in range(len(mail.outbox))]

            self.assertEqual(1, len(emails))
            sent_email = emails[0]

            self.assertEqual("html", sent_email.content_subtype)
            self.assertEqual(settings.EMAIL_HOST_USER, sent_email.from_email)
            self.assertEqual(['*****@*****.**'], sent_email.to)
            self.assertEqual([settings.HNI_SUPPORT_EMAIL_ID], sent_email.bcc)

            self.assertEqual(
                render_to_string(
                    'registration/activation_email_subject_in_en.txt'),
                sent_email.subject)
            ctx_dict = {
                'activation_key':
                RegistrationProfile.objects.get(
                    user=self.user1).activation_key,
                'expiration_days':
                settings.ACCOUNT_ACTIVATION_DAYS,
                'site':
                site,
                'username':
                self.user1.first_name,
                'invoice_total':
                PRO_SMS_MONTHLY_PRICING,
                'period':
                '1 month'
            }
            self.assertEqual(
                render_to_string(
                    'registration/pro_sms_activation_email_in_en.html',
                    ctx_dict), sent_email.body)

            payment_detail = PaymentDetails.objects.filter(
                organization=self.paid_organization)
            self.assertTrue(is_not_empty(payment_detail))
            payment_detail.delete()
示例#11
0
 def _get_ddtype(self, post_dict):
     options = post_dict.get('options')
     datadict_type = options.get('ddtype') if options is not None else None
     if is_not_empty(datadict_type):
         #  question already has a data dict type
         datadict_slug = datadict_type.get('slug')
     else:
         datadict_slug = str(slugify(unicode(post_dict.get('title'))))
     ddtype = self._get_or_create_data_dict(
         name=post_dict.get('code'),
         slug=datadict_slug,
         primitive_type=post_dict.get('type'),
         description=post_dict.get('title'))
     return ddtype
示例#12
0
    def __init__(self,
                 dbm,
                 name=None,
                 label=None,
                 form_code=None,
                 fields=None,
                 language="en",
                 is_registration_model=False,
                 validators=None,
                 enforce_unique_labels=True):
        if not validators: validators = [MandatoryValidator()]
        assert isinstance(dbm, DatabaseManager)
        assert name is None or is_not_empty(name)
        assert fields is None or is_sequence(fields)
        assert form_code is None or (is_string(form_code)
                                     and is_not_empty(form_code))
        # assert type is None or is_not_empty(type)

        DataObject.__init__(self, dbm)
        self.xform_model = None
        self._old_doc = None

        self._snapshots = {}
        self._form_fields = []
        self.errors = []
        self.validators = validators
        self._enforce_unique_labels = enforce_unique_labels
        self._validation_exception = []
        # Are we being constructed from scratch or existing doc?
        if name is None:
            return

        # Not made from existing doc, so build ourselves up
        self._validate_fields(fields)
        self._form_fields = fields

        self._set_doc(form_code, is_registration_model, label, language, name)
示例#13
0
def define_type(dbm, entity_type):
    """
    Add this entity type to the tree of all entity types and save it
    to the database. entity_type may be a string or a list of
    strings.
    """
    assert is_not_empty(entity_type)
    assert is_sequence(entity_type)
    type_path = ([entity_type] if is_string(entity_type) else entity_type)
    type_path = [item.strip() for item in type_path]
    if entity_type_already_defined(dbm, type_path):
        raise EntityTypeAlreadyDefined(u"Type: %s is already defined" % u'.'.join(entity_type))
        # now make the new one
    entity_tree = _get_entity_type_tree(dbm)
    entity_tree.add_path([atree.AggregationTree.root_id] + entity_type)
    entity_tree.save()
示例#14
0
    def get_paths(self):
        """""
        Returns a list of lists, each one a path from root to every node, with root removed.

        So a simple tree of ROOT->A->B->C returns [[A],[A,B], [A,B,C]]

        Use this method if every node has meaning, e.g. HEALTH FAC->HOSPITAL->REGIONAL

        """ ""
        # TODO: hold this as a cache that is blown by Add Path
        return [
            p[1:] for p in nx.shortest_path(
                self.graph,
                AggregationTree.root_id,
            ).values() if is_not_empty(p[1:])
        ]
示例#15
0
    def __init__(self, dbm, id=None):
        """""
        Note: _document is for 'protected' factory methods. If it is passed in the other
        arguments are ignored.

        """ ""
        assert (id is None or is_not_empty(id))

        DataObject.__init__(self, dbm)
        self.graph = None

        # being constructed from DB? If so, no more work here
        if id is None:
            return

        # ok, newly constructed, so set up a new Document
        self._set_document(AggregationTreeDocument(id=id))
示例#16
0
def define_type(dbm, entity_type):
    """
    Add this entity type to the tree of all entity types and save it
    to the database. entity_type may be a string or a list of
    strings.
    """
    assert is_not_empty(entity_type)
    assert is_sequence(entity_type)
    assert isinstance(dbm, DatabaseManager)
    if entity_type_already_defined(dbm, entity_type):
        raise EntityTypeAlreadyDefined(u"Type: %s is already defined" %
                                       u'.'.join(entity_type))
    entity_tree = AggregationTree.get(dbm,
                                      ENTITY_TYPE_TREE_ID,
                                      get_or_create=True)
    entity_tree.add_path([AggregationTree.root_id] + entity_type)
    entity_tree.save()
示例#17
0
def define_type(dbm, entity_type):
    """
    Add this entity type to the tree of all entity types and save it
    to the database. entity_type may be a string or a list of
    strings.
    """
    assert is_not_empty(entity_type)
    assert is_sequence(entity_type)
    type_path = ([entity_type] if is_string(entity_type) else entity_type)
    type_path = [item.strip() for item in type_path]
    if entity_type_already_defined(dbm, type_path):
        raise EntityTypeAlreadyDefined(u"Type: %s is already defined" %
                                       u'.'.join(entity_type))
        # now make the new one
    entity_tree = _get_entity_type_tree(dbm)
    entity_tree.add_path([atree.AggregationTree.root_id] + entity_type)
    entity_tree.save()
示例#18
0
    def __init__(self, dbm, id=None):
        """""
        Note: _document is for 'protected' factory methods. If it is passed in the other
        arguments are ignored.

        """""
        assert (id is None or is_not_empty(id))

        DataObject.__init__(self, dbm)
        self.graph = None

        # being constructed from DB? If so, no more work here
        if id is None:
            return

        # ok, newly constructed, so set up a new Document
        self._set_document(AggregationTreeDocument(id=id))
示例#19
0
        def build_dicts(dicts, parent, node_dict):
            for n in node_dict:
                # if we haven't seen it yet, build a dict for it
                if n not in dicts:
                    d = dict(self.graph.node[n])
                    dicts[n] = d  # hang onto them until they are connected up so don't get garbage collected!
                else:
                    d = dicts[n]

                # add to parent
                if parent is not None:
                    parent['_children'][n] = d

                # recurse children
                children = node_dict[n]
                if is_not_empty(children):
                    if '_children' not in d:
                        d['_children'] = dict()
                    build_dicts(dicts, d, node_dict[n])
示例#20
0
        def build_dicts(dicts, parent, node_dict):
            for n in node_dict:
                # if we haven't seen it yet, build a dict for it
                if n not in dicts:
                    d = dict(self.graph.node[n])
                    dicts[
                        n] = d  # hang onto them until they are connected up so don't get garbage collected!
                else:
                    d = dicts[n]

                # add to parent
                if parent is not None:
                    parent['_children'][n] = d

                # recurse children
                children = node_dict[n]
                if is_not_empty(children):
                    if '_children' not in d:
                        d['_children'] = dict()
                    build_dicts(dicts, d, node_dict[n])
示例#21
0
    def test_should_process_registration_data_for_paid_acccount_in_french(
            self):
        processor = get_registration_processor(self.paid_organization)

        site = Site(domain='test', name='test_site')
        kwargs = dict(invoice_period='', preferred_payment='')

        processor.process(self.user1, site, 'fr', kwargs)

        emails = [mail.outbox.pop() for i in range(len(mail.outbox))]

        self.assertEqual(1, len(emails))
        sent_email = emails[0]

        self.assertEqual("html", sent_email.content_subtype)
        self.assertEqual(settings.EMAIL_HOST_USER, sent_email.from_email)
        self.assertEqual(['*****@*****.**'], sent_email.to)
        self.assertEqual([settings.HNI_SUPPORT_EMAIL_ID], sent_email.bcc)

        self.assertEqual(
            render_to_string(
                'registration/activation_email_subject_in_fr.txt'),
            sent_email.subject)
        ctx_dict = {
            'activation_key':
            RegistrationProfile.objects.get(user=self.user1).activation_key,
            'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
            'site': site,
            'name': self.user1.first_name + ' ' + self.user1.last_name
        }
        self.assertEqual(
            render_to_string('registration/activation_email_in_fr.html',
                             ctx_dict), sent_email.body)

        payment_detail = PaymentDetails.objects.filter(
            organization=self.paid_organization)
        self.assertTrue(is_not_empty(payment_detail))
        payment_detail.delete()
示例#22
0
        def build_graph(graph, parent_id, current_id, current_dict):
            # shallow copy current dict for attributes
            attrs = dict(current_dict)
            children = None

            # make sure not already there...
            if current_id in graph:
                raise ValueError('Attempting to add multiple nodes to tree with same _id: %s' % current_id)

            try:
                children = attrs.pop('_children')
            except KeyError:
                # no problem, it's a terminal node with no children
                pass

            graph.add_node(current_id, attrs)
            if parent_id is not None:
                graph.add_edge(parent_id, current_id)

            # now recurse
            if is_not_empty(children):
                for c in children:
                    build_graph(graph, current_id, c, children[c])
示例#23
0
    def add_path(self, nodes):
        """""Adds a path to the tree.

        If the first item in the path is AggregationTree.root_id, this will be added at root.

        Otherwise, the method attempts to find the first (depth first search) occurrence of the first element
        in the sequence, and adds the path there.

        The 'nodes' list can contain tuples of the form '(node_name, dict)' to associate data
        with the nodes.
        e.g. add_path('a', ('b', {size: 10}), 'c')

        Raises a 'ValueError' if the path does NOT start with root and the first item cannot be found.
        """""

        assert is_sequence(nodes) and is_not_empty(nodes)

        first = (nodes[0][0] if is_sequence(nodes[0]) else nodes[0])
        if not first in self.graph:
            raise ValueError('First item in path: %s not in Tree.' % first)

        # iterate, add nodes, and pull out path
        path = []
        for n in nodes:
            data = None
            name = None
            if is_sequence(n):
                name = n[0]
                data = n[1]
            else:
                name = n

            self._add_node(name, data)
            path.append(name)

        # add the path
        self.graph.add_path(path)
示例#24
0
    def add_path(self, nodes):
        """""Adds a path to the tree.

        If the first item in the path is AggregationTree.root_id, this will be added at root.

        Otherwise, the method attempts to find the first (depth first search) occurrence of the first element
        in the sequence, and adds the path there.

        The 'nodes' list can contain tuples of the form '(node_name, dict)' to associate data
        with the nodes.
        e.g. add_path('a', ('b', {size: 10}), 'c')

        Raises a 'ValueError' if the path does NOT start with root and the first item cannot be found.
        """ ""

        assert is_sequence(nodes) and is_not_empty(nodes)

        first = (nodes[0][0] if is_sequence(nodes[0]) else nodes[0])
        if not first in self.graph:
            raise ValueError('First item in path: %s not in Tree.' % first)

        # iterate, add nodes, and pull out path
        path = []
        for n in nodes:
            data = None
            name = None
            if is_sequence(n):
                name = n[0]
                data = n[1]
            else:
                name = n

            self._add_node(name, data)
            path.append(name)

        # add the path
        self.graph.add_path(path)
示例#25
0
def delete_datasender_users_if_any(all_ids, organization):
    for id in all_ids:
        profiles = NGOUserProfile.objects.filter(org_id=organization.org_id,
                                                 reporter_id=id)
        if is_not_empty(profiles):
            profiles[0].user.delete()
 def has_error_in_submission(self, form_submission):
     return is_not_empty(form_submission.errors)
示例#27
0
 def get_entity_type(self, form_model):
     entity_type = self.form_model.entity_type
     return [e_type.lower() for e_type in entity_type
             ] if is_not_empty(entity_type) else None
示例#28
0
 def get_entity_type(self, form_model):
     entity_type = self.get_answer_for(ENTITY_TYPE_FIELD_CODE)
     return [e_type.lower() for e_type in entity_type
             ] if is_not_empty(entity_type) else None
示例#29
0
 def get_entity_type(self, values):
     entity_type = self._case_insensitive_lookup(values,
                                                 ENTITY_TYPE_FIELD_CODE)
     return entity_type.lower() if is_not_empty(entity_type) else None
示例#30
0
    def get_paths(self):
        """""
        Returns a list of lists, each one a path from root to every node, with root removed.

        So a simple tree of ROOT->A->B->C returns [[A],[A,B], [A,B,C]]

        Use this method if every node has meaning, e.g. HEALTH FAC->HOSPITAL->REGIONAL

        """""
        # TODO: hold this as a cache that is blown by Add Path
        return [p[1:] for p in nx.shortest_path(self.graph, AggregationTree.root_id, ).values() if is_not_empty(p[1:])]
        print "User void status:%s" % entity_to_be_deleted.is_void()
        if entity_to_be_deleted.is_void():
            print "Deleting user."
            user_profile.user.delete()
            if organization.in_trial_mode:
                delete_datasender_for_trial_mode(dbm,
                                                 [profile_reporter_id.lower()],
                                                 'reporter')
        else:
            print "Not deleting user since it is not soft deleted."


def _is_only_datasender(user):
    return user.groups.filter(
        name__in=["NGO Admins", "Project Managers"]).count() <= 0


for email_id in email_addresses:
    try:
        print "Processing User with email_id: '%s'" % email_id
        user = User.objects.get(email=email_id)
        if _is_only_datasender(user):
            profiles = NGOUserProfile.objects.filter(user=user)
            if is_not_empty(profiles):
                _delete_user_entry(profiles)
            else:
                print "User with email_id: '%s' has no profile" % user.email
        else:
            print "User with email_id: '%s' is not a simple DS and will not be deleted." % user.email
    except ObjectDoesNotExist:
        print "User with email_id: '%s' does not exist" % user.email