Beispiel #1
0
    def handle_register(self, message, msg_text):
        connection, unused = getConnectionAndReporter(message,
                                                      self.ALLOWED_ROLE_CODES)

        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'register'])
            return

        try:
            location_code, role_code, full_name = grammar(text).register()
        except parsley.ParseError:
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        role = Role.get_by_code(role_code)
        if role is None or role.code.lower() not in self.ALLOWED_ROLE_CODES:
            message.respond(self.ERROR_MESSAGES[u'invalid_role'] % {
                u'role_code': role_code,
                u'text': message.text
            })
            return

        kwargs = {u'location': location, u'role': role}
        kwargs[u'alias'], kwargs[u'first_name'], kwargs[
            u'last_name'] = Reporter.parse_name(full_name)
        rep = Reporter(**kwargs)

        if Reporter.exists(rep, connection):
            message.respond(
                self.RESPONSE_MESSAGES[u'already_registered'] % {
                    u'name': rep.first_name,
                    u'role': rep.role.name,
                    u'location': rep.location.name,
                    u'location_type': rep.location.type.name
                })
            return

        rep.save()
        connection.reporters.add(rep)

        message.respond(
            self.RESPONSE_MESSAGES[u'register'] % {
                u'name': rep.first_name,
                u'role': rep.role.code,
                u'location': rep.location.name,
                u'location_type': rep.location.type.name
            })
Beispiel #2
0
def run():
    print "starting"
    from hq.models import ExtUser, ReporterProfile
    from reporters.models import Reporter, PersistantBackend, PersistantConnection

    all_users = ExtUser.objects.all()
    for user in all_users:
        print "processing user %s" % user
        rep = user.reporter
        if rep:
            print "%s already has attached reporter object!  %s" % (user,
                                                                    reporter)
        else:
            rep = Reporter()
            # if they have a first and last name set, use those,
            # otherwise just use the login
            if user.first_name and user.last_name:

                alias, fn, ln = Reporter.parse_name(
                    "%s %s" % (user.first_name, user.last_name))
            else:
                alias, fn, ln = Reporter.parse_name(user.username)
            print "Chose alias: %s first last: %s %s" % (alias, fn, ln)
            rep.first_name = fn
            rep.last_name = ln
            rep.alias = alias
            rep.save()

        profile = ReporterProfile()
        profile.reporter = rep
        profile.chw_id = user.chw_id
        profile.chw_username = user.chw_username
        profile.domain = user.domain
        profile.save()
        print "Saved profile %s for %s" % (profile, user)
        if user.primary_phone:
            # create a backend / connection for them.  This is
            # still a little hazy as it's not clear how the
            # backend is properly chosen

            # this will create an arbitrary backend if none is
            # found
            if len(PersistantBackend.objects.all()) == 0:
                PersistantBackend.objects.create(
                    slug="data_migration", title="Data Migration Backend")
            backend = PersistantBackend.objects.all()[0]
            try:
                conn = PersistantConnection.objects.create(
                    backend=backend, identity=user.primary_phone, reporter=rep)
                print "created connection %s for %s" % (conn, user)
            except Exception, e:
                print "Error creating connection for %s for number %s.  Is it possible you have duplicate phone numbers?" % (
                    user, user.primary_phone)
Beispiel #3
0
def __combined_message_log_row(row):
    reporter = None
    connection = None

    # order of fields output by combined_message_log:
    #   [0] direction           [1] message_id      [2] message_date
    #   [3] message_text        [4] reporter_id     [5] reporter_first_name
    #   [6] reporter_last_name  [7] backend_id      [8] backend_title
    #   [9] backend_slug       [10] connection_id  [11] connection_identity

    # if this message is linked to a reporter, create a Reporter object
    # (so we can call the  methods like full_name) without hitting the
    # database each time. note that not all fields were fetched, so the
    # object won't work fully, but enough to display it
    if row[4] is not None:
        reporter = Reporter(first_name=row[5], last_name=row[6], pk=row[4])

    # likewise for a backend+connection, if this message isn't
    # linked to a reporter. combined_message_log can't filter
    # by connections (yet), but they must be displayed
    if row[7] is not None:
        backend = PersistantBackend(title=row[8], slug=row[9], id=row[7])

        connection = PersistantConnection(backend=backend,
                                          identity=row[11],
                                          id=row[10])

    # If the date object is already a datetime, don't bother
    # casting it.  Otherwise do.
    casted_date = row[2]
    if not isinstance(casted_date, datetime):
        casted_date = typecast_timestamp(row[2])
    return {
        "direction": row[0],
        "pk": row[1],
        "date": casted_date,
        "text": row[3],
        "reporter": reporter,
        "connection": connection
    }
Beispiel #4
0
    def register(self, message, location_code, role, name=''):
        conn, unused = getConnectionAndReporter(message, ALLOWED_ROLE_CODES)

        data = {}
        try:
            data['location'] = Location.objects.get(code=location_code,
                                                    type__name=u'RC',
                                                    active=True)
            data['role'] = Role.objects.get(code__iexact=role)
            data['alias'], data['first_name'], data[
                'last_name'] = Reporter.parse_name(name.strip())
            rep = Reporter(**data)

            if Reporter.exists(rep, conn):
                message.respond(self.response_messages['already_registered'] %
                                dict(name=rep.first_name,
                                     role=rep.role,
                                     location_name=rep.location))
                return True

            rep.save()
            conn.reporters.add(rep)

            message.respond(self.response_messages['registered'] %
                            dict(name=rep.first_name,
                                 role=rep.role,
                                 location_name=rep.location,
                                 location_type=rep.location.type))
        except Role.DoesNotExist:
            message.respond(self.error_messages['invalid_role'] %
                            dict(role_code=role, text=message.text))
        except Location.DoesNotExist:
            message.respond(
                self.error_messages['invalid_location'] %
                dict(location_code=location_code, text=message.text))

        return True
Beispiel #5
0
 def _create_reporter(self, alias, phone_number):
     reporter = Reporter(alias=alias)
     reporter.save()
     pconnection = self._create_connection(phone_number, reporter)
     reporter.connections.add(pconnection)
     return reporter