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 })
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