<commit_before>"""Copyright 2014 Google Inc. All rights reserved.

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd

GAE Model for the datastore."""

__author__ = '<EMAIL> (<NAME>)'

import json
import logging

from google.appengine.api import users
from google.appengine.ext import db

import dashboard_fields as fields


DEFAULT_DASHBOARD_TITLE = 'Untitled Dashboard'
DEFAULT_DOMAIN = 'google.com'


class Error(Exception):
  pass


class InitializeError(Error):
  pass


class SecurityError(Error):
  pass


class UserValidator(db.Model):
  user = db.UserProperty(required=True)

  @classmethod
  def GetUserFromEmail(cls, email):
    """Return a stable user_id string based on an email address.

    Args:
      email: Email address of the user.

    Returns:
      A GAE User instance, or None if the email doesn't resolve.
    """
    u = users.User(email)
    key = UserValidator(user=u).put()
    obj = UserValidator.get(key)
    user = obj.user
    obj.delete()

    if not user.user_id():
      return None

    return user

  @classmethod
  def GetUsersFromEmails(cls, emails):
    """Return a list of GAE Users based on a list of email addresses.  Also returns a list of invalid email addresses.

    Args:
      emails: An array of strings, each representing an email address of a valid GAE User.

    Returns:
      * A list of GAE Users.
      * A list of email addresses that failed to validate.
    """
    return_users = []
    return_failed_emails = []

    for email in emails:
      user = cls.GetUserFromEmail(email)

      if user:
        return_users.append(user)
      else:
        return_failed_emails.append(email)

    return return_users, return_failed_emails


class Dashboard(db.Model):
  """Models a Dashboard definition (as JSON).

  Tracks the contents of the dashboard (data, stored as JSON string), plus the
  user who created and last modified the dashboard.
  """

  created_by = db.UserProperty()
  modified_by = db.UserProperty()
  writers = db.UserProperty(repeated=True)
  title = db.StringProperty(default='')
  data = db.TextProperty(default='')
  public = db.BooleanProperty(default=False)

  @staticmethod
  def GetDashboard(dashboard_id, required=True):
    """Returns a Dashboard model for the provided dashboard_id.

    Args:
      dashboard_id: An integer key for a dashboard.
      required: Determines the behavior when the id is not found.  If false,
          None will be returned.  If true, an InitializeError will be raised.

    Returns:
      A Dashboard model instance.<commit_msg>Change dashboard entity from ext.db to ndb.<commit_after>"""Copyright 2014 Google Inc. All rights reserved.

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd

GAE Model for the datastore."""

__author__ = '<EMAIL> (<NAME>)'

import json
import logging

from google.appengine.api import users
from google.appengine.ext import ndb

import dashboard_fields as fields


DEFAULT_DASHBOARD_TITLE = 'Untitled Dashboard'
DEFAULT_DOMAIN = 'google.com'


class Error(Exception):
  pass


class InitializeError(Error):
  pass


class SecurityError(Error):
  pass


class UserValidator(ndb.Model):
  user = ndb.UserProperty(required=True)

  @classmethod
  def GetUserFromEmail(cls, email):
    """Return a stable user_id string based on an email address.

    Args:
      email: Email address of the user.

    Returns:
      A GAE User instance, or None if the email doesn't resolve.
    """
    u = users.User(email)
    key = UserValidator(user=u).put()
    obj = UserValidator.get(key)
    user = obj.user
    obj.delete()

    if not user.user_id():
      return None

    return user

  @classmethod
  def GetUsersFromEmails(cls, emails):
    """Return a list of GAE Users based on a list of email addresses.  Also returns a list of invalid email addresses.

    Args:
      emails: An array of strings, each representing an email address of a valid GAE User.

    Returns:
      * A list of GAE Users.
      * A list of email addresses that failed to validate.
    """
    return_users = []
    return_failed_emails = []

    for email in emails:
      user = cls.GetUserFromEmail(email)

      if user:
        return_users.append(user)
      else:
        return_failed_emails.append(email)

    return return_users, return_failed_emails


class Dashboard(ndb.Model):
  """Models a Dashboard definition (as JSON).

  Tracks the contents of the dashboard (data, stored as JSON string), plus the
  user who created and last modified the dashboard.
  """

  created_by = ndb.UserProperty()
  modified_by = ndb.UserProperty()
  writers = ndb.UserProperty(repeated=True)
  title = ndb.StringProperty(default='')
  data = ndb.TextProperty(default='')
  public = ndb.BooleanProperty(default=False)

  @staticmethod
  def GetDashboard(dashboard_id, required=True):
    """Returns a Dashboard model for the provided dashboard_id.

    Args:
      dashboard_id: An integer key for a dashboard.
      required: Determines the behavior when the id is not found.  If false,
          None will be returned.  If true, an InitializeError will be raised.

    Returns:
      A Dashboard model instance.
{'fraction_non_alphanumeric': 0.08118219229330341, 'fraction_numerical': 0.0014964459408903852, 'mean_word_length': 3.2947791164658633, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '6949035', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1538, 'n_words': 567}