Ejemplo n.º 1
0
def get_location(domain, user, site_code):
    location = None
    if user and CommTrackUser.wrap(user.to_json()).location:
        loc = CommTrackUser.wrap(user.to_json()).location
        location = get_supply_point(domain, loc=loc)
    elif site_code:
        location = get_supply_point(domain, site_code=site_code)
    return location
Ejemplo n.º 2
0
def get_location(domain, user, site_code):
    location = None
    if user and user.location:
        loc = user.location
        location = get_supply_point(domain, loc=loc)
    elif site_code:
        location = get_supply_point(domain, site_code=site_code)
    return location
Ejemplo n.º 3
0
def validate_row(row, domain, data_cols):
    """pre-validate the information in a particular import row: valid location,
    reporting user, and data formats
    """
    # identify location
    loc_code = row.get('outlet_code') or row.get('site_code')
    row['loc'] = get_supply_point(domain, loc_code)['case']
    if row['loc'] is None:
        set_error(row, 'ERROR location code is invalid')
        return

    # identify user
    phone = row.get('phone')
    owner = None
    if phone:
        vn = VerifiedNumber.by_phone(phone)
        if not vn:
            set_error(row, 'ERROR phone number is not verified with any user')
            return
        owner = vn.owner
        row['phone'] = strip_plus(phone)

    username = row.get('reporter')
    if username:
        user = CouchUser.get_by_username('%s@%s.commcarehq.org' %
                                         (username, domain))
        if not user:
            set_error(row, 'ERROR reporter user does not exist')
            return

    if owner:
        if user and user._id != owner._id:
            set_error(row, 'ERROR phone number does not belong to user')
            return
        user = owner
    row['user'] = user

    # validate other fields

    try:
        row['timestamp'] = datetime.strptime(row['date'],
                                             '%Y-%m-%d')  # TODO: allow time?
    except ValueError:
        set_error(row, 'ERROR invalid date format')
        return

    for k in data_cols:
        val = row[k]
        if val:
            try:
                int(val)
            except ValueError:
                set_error(
                    row,
                    'ERROR invalid data value "%s" in column "%s"' % (val, k))
                return

    if all(not row[k] for k in data_cols):
        set_error(row, 'ERROR stock report is empty')
        return
Ejemplo n.º 4
0
 def get(self, site_code, domain):
     if not site_code:
         return None
     if site_code in self.cache:
         return self.cache[site_code]
     else:
         supply_point = get_supply_point(domain, site_code)['case']
         self.cache[site_code] = supply_point
         return supply_point
Ejemplo n.º 5
0
def validate_row(row, domain, data_cols):
    """pre-validate the information in a particular import row: valid location,
    reporting user, and data formats
    """
    # identify location
    loc_code = row.get('outlet_code') or row.get('site_code')
    row['loc'] = get_supply_point(domain, loc_code)['case']
    if row['loc'] is None:
        set_error(row, 'ERROR location code is invalid')
        return

    # identify user
    phone = row.get('phone')
    owner = None
    if phone:
        vn = VerifiedNumber.by_phone(phone)
        if not vn:
            set_error(row, 'ERROR phone number is not verified with any user')
            return
        owner = vn.owner
        row['phone'] = strip_plus(phone)

    username = row.get('reporter')
    if username:
        user = CouchUser.get_by_username('%s@%s.commcarehq.org' % (username, domain))
        if not user:
            set_error(row, 'ERROR reporter user does not exist')
            return

    if owner:
        if user and user._id != owner._id:
            set_error(row, 'ERROR phone number does not belong to user')
            return
        user = owner
    row['user'] = user

    # validate other fields

    try:
        row['timestamp'] = datetime.strptime(row['date'], '%Y-%m-%d') # TODO: allow time?
    except ValueError:
        set_error(row, 'ERROR invalid date format')
        return

    for k in data_cols:
        val = row[k]
        if val:
            try:
                int(val)
            except ValueError:
                set_error(row, 'ERROR invalid data value "%s" in column "%s"' % (val, k))
                return

    if all(not row[k] for k in data_cols):
        set_error(row, 'ERROR stock report is empty')
        return
Ejemplo n.º 6
0
    def handle(self):
        if not self.args:
            self.respond(ARRIVED_HELP)
            return

        msdcode = self.args[0]
        location = get_supply_point(self.domain, site_code=msdcode)

        if location:
            self.respond(ARRIVED_KNOWN, facility=location['case'].name)
        else:
            self.respond(ARRIVED_DEFAULT)
Ejemplo n.º 7
0
    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner
        if isinstance(u, CommCareUser):
            linked_loc_id = u.dynamic_properties().get('commtrack_location')
            if linked_loc_id:
                self.location = get_supply_point(self.domain.name, loc=Location.get(linked_loc_id))['case']

        self.C = domain.commtrack_settings
Ejemplo n.º 8
0
 def get(self, site_code, domain):
     if not site_code:
         return None
     if site_code in self.cache:
         return self.cache[site_code]
     else:
         supply_point = get_supply_point(
             domain,
             site_code
         )['case']
         self.cache[site_code] = supply_point
         return supply_point
Ejemplo n.º 9
0
    def handle(self):
        if not self.args:
            self.respond(ARRIVED_HELP)
            return

        msdcode = self.args[0]
        location = get_supply_point(self.domain, site_code=msdcode)

        if location:
            self.respond(ARRIVED_KNOWN, facility=location['case'].name)
        else:
            self.respond(ARRIVED_DEFAULT)
Ejemplo n.º 10
0
    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner
        if isinstance(u, CommCareUser):
            linked_loc_id = u.dynamic_properties().get('commtrack_location')
            if linked_loc_id:
                self.location = get_supply_point(
                    self.domain.name, loc=Location.get(linked_loc_id))['case']

        self.C = domain.commtrack_settings
Ejemplo n.º 11
0
    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner
        if domain.commtrack_enabled:
            # currently only support one location on the UI
            linked_loc = CommTrackUser.wrap(u.to_json()).location
            if linked_loc:
                self.location = get_supply_point(self.domain.name, loc=linked_loc)['case']

        self.C = domain.commtrack_settings
Ejemplo n.º 12
0
    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner

        if domain.commtrack_enabled:
            # if user is not actually a user, we let someone else process
            if not isinstance(u, CouchUser):
                raise NotAUserClassError

            # currently only support one location on the UI
            linked_loc = u.location
            if linked_loc:
                self.location = get_supply_point(self.domain.name, loc=linked_loc)

        self.C = domain.commtrack_settings
Ejemplo n.º 13
0
    def __init__(self, domain, v):
        self.domain = domain
        self.v = v

        self.location = None
        u = v.owner

        if domain.commtrack_enabled:
            # if user is not actually a user, we let someone else process
            if not isinstance(u, CouchUser):
                raise NotAUserClassError

            # currently only support one location on the UI
            linked_loc = u.location
            if linked_loc:
                self.location = get_supply_point(self.domain.name,
                                                 loc=linked_loc)

        self.C = domain.commtrack_settings
Ejemplo n.º 14
0
 def location_from_code(self, loc_code):
     """return the supply point case referenced by loc_code"""
     result = get_supply_point(self.domain.name, loc_code)['case']
     if not result:
         raise SMSError('invalid location code "%s"' % loc_code)
     return result
Ejemplo n.º 15
0
 def location_from_code(self, loc_code):
     """return the supply point case referenced by loc_code"""
     result = get_supply_point(self.domain.name, loc_code)
     if not result:
         raise SMSError('invalid location code "%s"' % loc_code)
     return result
Ejemplo n.º 16
0
 def lookup(self, site_code):
     supply_point = get_supply_point(
         self.domain,
         site_code
     )['case']
     return supply_point
Ejemplo n.º 17
0
 def lookup(self, site_code):
     supply_point = get_supply_point(
         self.domain,
         site_code
     )['case']
     return supply_point