示例#1
0
 def log_edit_set(key, added, removed):
     if added:
         system_messages.append(
             u"Added to {0}: {1}"
             .format(JSON.describe(key), ", ".join(added))
         )
     if removed:
         system_messages.append(
             u"Removed from {0}: {1}"
             .format(JSON.describe(key), ", ".join(removed))
         )
示例#2
0
        def log_edit_value(key, old, new):
            if key is JSON.number:
                return

            if old == new:
                #print "Client submitted unchaged value for {0}: {1}".format(JSON.describe(key), new)
                return

            if key in JSON.states():
                state_changes.append((key, new))
                return

            system_messages.append(u"Changed {0} to: {1}".format(JSON.describe(key), new if new else u"<no value>"))
示例#3
0
 def test_JSON_states(self):
     """
     L{JSON.states} returns incident state names.
     """
     self.assertEquals(
         set(JSON.states()),
         set((
             JSON.created,
             JSON.dispatched,
             JSON.on_scene,
             JSON.closed,
         )),
     )
示例#4
0
    def edit_incident(self, request, number):
        number = int(number)
        incident = self.storage.read_incident_with_number(number)

        #
        # Handle the changes requested by the client
        #
        edits_json = from_json_io(request.content)
        edits = Incident.from_json(edits_json, number=number, validate=False)

        user_entries = []

        system_messages = []
        state_changes = []

        def log_edit_value(key, old, new):
            if key is JSON.number:
                return

            if old == new:
                #print "Client submitted unchaged value for {0}: {1}".format(JSON.describe(key), new)
                return

            if key in JSON.states():
                state_changes.append((key, new))
                return

            system_messages.append(u"Changed {0} to: {1}".format(JSON.describe(key), new if new else u"<no value>"))

        def diff_set(key, old, new):
            old = frozenset(old if old else ())
            new = frozenset(new if new else ())
            unchanged = old & new
            removed = old ^ unchanged
            added = new ^ unchanged
            return added, removed

        def log_edit_set(key, added, removed):
            if added:
                system_messages.append(u"Added to {0}: {1}".format(JSON.describe(key), ", ".join(added)))
            if removed:
                system_messages.append(u"Removed from {0}: {1}".format(JSON.describe(key), ", ".join(removed)))

        for key in edits_json.keys():
            key = JSON.lookupByValue(key)

            if key is JSON.report_entries:
                if edits.report_entries is not None:
                    for entry in edits.report_entries:
                        # Edit report entries to add author
                        entry.author = self.avatarId.decode("utf-8")
                        user_entries.append(entry)
            elif key is JSON.location_name:
                if edits.location.name is not None:
                    log_edit_value(key, incident.location.name, edits.location.name)
                    incident.location.name = edits.location.name
            elif key is JSON.location_address:
                if edits.location.address is not None:
                    log_edit_value(key, incident.location.address, edits.location.address)
                    incident.location.address = edits.location.address
            elif key is JSON.ranger_handles:
                if edits.rangers is not None:
                    added, removed = diff_set(key, incident.rangers, edits.rangers)
                    log_edit_set(key, [r.handle for r in added], [r.handle for r in removed])
                    incident.rangers = edits.rangers
            elif key is JSON.incident_types:
                if edits.incident_types is not None:
                    log_edit_set(key, *diff_set(key, incident.incident_types, edits.incident_types))
                    incident.incident_types = edits.incident_types
            else:
                attr_name = key.name
                attr_value = getattr(edits, attr_name)

                if key in (JSON.created, JSON.dispatched, JSON.on_scene, JSON.closed):
                    if edits.created is None:
                        # If created is None, then we aren't editing state.
                        # (It would be weird if others were not None here.)
                        continue
                elif attr_value is None:
                    # None values should not cause edits.
                    continue

                log_edit_value(key, getattr(incident, attr_name), attr_value)

                setattr(incident, attr_name, attr_value)

        #
        # Figure out what to report about state changes
        #
        highest_change = None
        lowest_change = None
        for state_changed, state_time in state_changes:
            if state_time is None:
                if lowest_change is None or JSON.cmpStates(lowest_change, state_changed) > 0:
                    lowest_change = state_changed
            else:
                if highest_change is None or JSON.cmpStates(highest_change[0], state_changed) < 0:
                    highest_change = (state_changed, state_time)

        if highest_change is not None:
            system_messages.append(u"State changed to: {0}".format(JSON.describe(highest_change[0])))
        elif lowest_change is not None:
            # We need one state less than lowest_change
            last = None
            for state in JSON.states():
                if state == lowest_change:
                    break
                last = state
            system_messages.append(u"State changed to: {0}".format(JSON.describe(last)))

        #
        # Add system report entries, then user entries
        #
        if system_messages:
            incident.report_entries.append(
                ReportEntry(
                    author = self.avatarId.decode("utf-8"),
                    text = u"\n".join(system_messages),
                    system_entry = True,
                )
            )
        incident.report_entries.extend(user_entries)

        #
        # Write to disk
        #
        self.storage.write_incident(incident)

        #
        # Respond
        #
        set_response_header(request, HeaderName.contentType, ContentType.JSON)
        request.setResponseCode(http.OK)

        return "";
示例#5
0
    def test_JSON_cmpStates(self):
        states = tuple(JSON.states())

        self.assertEquals(states, sorted(states))