<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This work was created by participants in the DataONE project, and is
# jointly copyrighted by participating institutions in DataONE. For
# more information on DataONE, see our web site at http://dataone.org.
#
#   Copyright 2009-2016 DataONE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:mod:`event_log`
================

:Synopsis: Log DataONE object accesses.
:Author: DataONE (Dahl)
"""

# D1
import d1_common.types.exceptions

# App.
import models
import auth


def _log(pid, request, event, timestamp=None):
  """Log an object access.
  :return:
  """
  ip_address = request.META['REMOTE_ADDR']
  user_agent = request.META['HTTP_USER_AGENT']
  subject = auth.get_trusted_subjects_string()

  # Support logging events that are not associated with an object.
  object_row = None
  if pid is not None:
    try:
      object_row = models.ScienceObject.objects.filter(pid__did=pid)[0]
    except IndexError:
      err_msg = u'Attempted to create event log for non-existing object. pid="{}"'\
        .format((pid))
      raise d1_common.types.exceptions.ServiceFailure(0, err_msg)

  # Create log entry.
  event_log_row = models.EventLog()
  event_log_row.sciobj = object_row
  event_log_row.event = models.event(event)
  event_log_row.ip_address = models.ip_address(ip_address)
  event_log_row.user_agent = models.user_agent(user_agent)
  event_log_row.subject = models.subject(subject)
  event_log_row.save()

  # The datetime is an optional parameter. If it is not provided, a
  # "auto_now_add=True" value in the the model defaults it to Now. The
  # disadvantage to this approach is that we have to update the timestamp in a
  # separate step if we want to set it to anything other than Now.
  if timestamp is not None:
    event_log_row.timestamp = timestamp
    event_log_row.save()


def create(pid, request, timestamp=None):
  return _log(pid, request, 'create', timestamp)


def read(pid, request, timestamp=None):
  return _log(pid, request, 'read', timestamp)


def update(pid, request, timestamp=None):
  return _log(pid, request, 'update', timestamp)


def delete(pid, request, timestamp=None):<commit_msg>Add event log create function that can be used without Request<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This work was created by participants in the DataONE project, and is
# jointly copyrighted by participating institutions in DataONE. For
# more information on DataONE, see our web site at http://dataone.org.
#
#   Copyright 2009-2016 DataONE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:mod:`event_log`
================

:Synopsis: Log DataONE object accesses.
:Author: DataONE (Dahl)
"""

# D1
import d1_common.types.exceptions

# App.
import models
import auth


def _log(pid, request, event, timestamp=None):
  """Log an object access.
  :return:
  """
  ip_address = request.META['REMOTE_ADDR']
  user_agent = request.META['HTTP_USER_AGENT']
  subject = auth.get_trusted_subjects_string()

  # Support logging events that are not associated with an object.
  object_row = None
  if pid is not None:
    try:
      object_row = models.ScienceObject.objects.filter(pid__did=pid)[0]
    except IndexError:
      err_msg = u'Attempted to create event log for non-existing object. pid="{}"'\
        .format((pid))
      raise d1_common.types.exceptions.ServiceFailure(0, err_msg)

  create_log_entry(object_row, event, ip_address, user_agent, subject)

  # The datetime is an optional parameter. If it is not provided, a
  # "auto_now_add=True" value in the the model defaults it to Now. The
  # disadvantage to this approach is that we have to update the timestamp in a
  # separate step if we want to set it to anything other than Now.
  if timestamp is not None:
    event_log_row.timestamp = timestamp
    event_log_row.save()


def create_log_entry(object_row, event, ip_address, user_agent, subject):
  event_log_row = models.EventLog()
  event_log_row.sciobj = object_row
  event_log_row.event = models.event(event)
  event_log_row.ip_address = models.ip_address(ip_address)
  event_log_row.user_agent = models.user_agent(user_agent)
  event_log_row.subject = models.subject(subject)
  event_log_row.save()


def create(pid, request, timestamp=None):
  return _log(pid, request, 'create', timestamp)


def read(pid, request, timestamp=None):
  return _log(pid, request, 'read', timestamp)


def update(pid, request, timestamp=None):
  return _log(pid, request, 'update', timestamp)


def delete(pid, request, timestamp=None):
{'fraction_non_alphanumeric': 0.09823359769286229, 'fraction_numerical': 0.006488824801730353, 'mean_word_length': 4.361352657004831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1874063', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1646, 'n_words': 595}