def message_as_json_dict(message, include_body = True): """ Serializes message metadata and (optionnaly) message body into JSON. Modified date is returned as milliseconds """ json = { 'guid' : message.pk, 'subject' : message.subject, 'sender_address' : message.sender_address.email, 'date' : message.date.ctime(), # modified date in milliseconds since epoch in UTC 'modified_date' : '%.0f' % (time.mktime(message.modified_date.timetuple())*1000000 + message.modified_date.microsecond) } if include_body: body_type = models.MimeType.HTML message_asset = message.get_asset(models.AssetClass.MESSAGE_PART, body_type) if not message_asset: body_type = models.MimeType.TEXT message_asset = message.get_asset(models.AssetClass.MESSAGE_PART, body_type) if message_asset: message_file = instantiate_asset(message_asset)['Local-Path'] message_text = open(message_file,'r').read() #TODO: figure out why a message has some non-ascii chars # i need to call decode here message_text = message_text.decode('utf8','ignore') #TODO: extract the message from a thread. # This will be much more complex than looking for -----Original Message----- if body_type == models.MimeType.TEXT: json.update(body = message_text.split('-----Original Message-----')[0]) else: json.update(body = message_text) json.update(body_type = body_type) return json
def jsonify_model(model, only=None, exclude=None, include=None): """ Jsonifies given model object See :func:`as_json` for more info :param model: object to be converted into json :param only: list of attributes to be included in json, if this parameter is not set attributes() method of the model will be used for obtaining the list of attributes names :param exclude: list of attributes to be excluded from the json format :param include: list of attribute names to be included in json, attribute names can be any properties of `model` (even method names) """ json = {} if only: json.update(jsonify_iterable(model, only)) else: json.update(jsonify_iterable(model, model.attributes(), exclude)) if include: json.update(jsonify_iterable(model, include)) return cleanup(json)
def aggregate_as_json_dict(aggregate, message_body = True): messages = aggregate.messages.all().order_by('-date') latest_message_date = messages[len(messages)-1].date latest_message_date = latest_message_date and latest_message_date.ctime() or None latest_message = aggregate.latest_message if aggregate.status == models.MessageAggregate.STATUS_READY: json = { 'status' : str(aggregate.status), 'guid' : '%s.aggregate' % aggregate.pk, 'subject' : aggregate.name, 'summary' : aggregate.summary, 'date' : str(latest_message_date), 'humanized_age' : humanize_date(latest_message_date), 'tags' : [ '%s.tag' % tag.pk for tag in aggregate.tags.all() ], 'latest_sender' : aggregate.latest_sender.email } if message_body: json.update(messages = [ message_as_json_dict(message, message_body) for message in messages]) else: json.update(messages = [ '%s' % message.pk for message in messages]) elif aggregate.status == models.MessageAggregate.STATUS_DELETED: json = { 'status' : str(aggregate.status), 'guid' : '%s.aggregate' % aggregate.pk } json.update(messages = [ '%s' % message.pk for message in messages]) return json