<commit_before># Copyright 2008 Google Inc.
#
# 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.

"""Project related views and functions.

This requires Django 0.97.pre.
"""


# Python imports
import os
import cgi
import random
import re
import logging
import binascii
import datetime
import urllib
import md5
from xml.etree import ElementTree
from cStringIO import StringIO

# AppEngine imports
from google.appengine.api import mail
from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.api import urlfetch
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

# Django imports
# TODO(guido): Don't import classes/functions directly.
from django import http
from django import forms
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseForbidden, HttpResponseNotFound
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.forms import formsets

# Local imports
import models
import engine
import library
import patching
import fields
import views
from view_util import *

## Project CRUD ##

def _assert_project_and_owner(request, project):
  "If the current user is not an owner of this project or an admin, give a 404."
  if not project:
    raise http.Http404()
  if not request.user or not ((project.key() in [
          p.key() for p in request.projects_owned_by_user])
      or request.user_is_admin):
    raise http.Http404()

@project_owner_or_admin_required
def project_list(request):
  """/admin/projects - list of all projects"""
  if request.user_is_admin:
    projects = models.Project.get_all_projects()
  else:
    projects = models.Project.projects_owned_by_user(request.user)
    logging.info("project_list projects=" + str(projects))
    if not projects:
      raise http.Http404()
  return respond(request, 'admin_projects.html', {'projects': projects})

def _keys_for(values):
  return [x.key() for x in values]

def _users_to_accounts(users):
  return map(models.Account.get_account_for_user, users)

def _combine_users_and_groups(users, groupKeys):
  return models.AccountGroup.get(groupKeys) + _users_to_accounts(users)

def _field_to_approval_right(cleaned):
  result = models.ApprovalRight()
  result.files = cleaned["files"]
  (result.approvers_users,result.approvers_groups
      ) = fields.UserGroupField.get_user_and_group_keys(cleaned["approvers"])
  (result.verifiers_users,result.verifiers_groups
      ) = fields.UserGroupField.get_user_and_group_keys(cleaned["verifiers"])
  result.put()
  return result

def _approval_right_to_field(key):
  val = models.ApprovalRight.get(key)
  if not val:
    raise KeyError("no ApprovalRight for key: " + str(key))
  return {
      'files': val.files,
      'approvers': _combine_users_and_groups(val.approvers_users,
                      val.approvers_groups),
      'verifiers': _combine_users_and_groups(val.verifiers_users,
                      val.verifiers_groups),
    }

class AdminProjectForm(BaseForm):
  _template = 'admin_project.html'

  name = forms.CharField()
  comment = forms.CharField(required=False,
                            max_length=10000,
                            widget=forms.Textarea(attrs={'cols': 60}))
  owners = fields.UserGroupField()
  code_reviews = fields.ApproversField()

  @classmethod
  def _init(cls, project):
     owners = fields.UserGroupField.field_value_for_keys(
                project.owners_users,
                project.owners_groups),
     return {'initial': {'name': project.name,
             'comment': project.comment,
             'owners': owners,<commit_msg>Rename project owners to project leads in the web UI<commit_after># Copyright 2008 Google Inc.
#
# 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.

"""Project related views and functions.

This requires Django 0.97.pre.
"""


# Python imports
import os
import cgi
import random
import re
import logging
import binascii
import datetime
import urllib
import md5
from xml.etree import ElementTree
from cStringIO import StringIO

# AppEngine imports
from google.appengine.api import mail
from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.api import urlfetch
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

# Django imports
# TODO(guido): Don't import classes/functions directly.
from django import http
from django import forms
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseForbidden, HttpResponseNotFound
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.forms import formsets

# Local imports
import models
import engine
import library
import patching
import fields
import views
from view_util import *

## Project CRUD ##

def _assert_project_and_owner(request, project):
  "If the current user is not an owner of this project or an admin, give a 404."
  if not project:
    raise http.Http404()
  if not request.user or not ((project.key() in [
          p.key() for p in request.projects_owned_by_user])
      or request.user_is_admin):
    raise http.Http404()

@project_owner_or_admin_required
def project_list(request):
  """/admin/projects - list of all projects"""
  if request.user_is_admin:
    projects = models.Project.get_all_projects()
  else:
    projects = models.Project.projects_owned_by_user(request.user)
    logging.info("project_list projects=" + str(projects))
    if not projects:
      raise http.Http404()
  return respond(request, 'admin_projects.html', {'projects': projects})

def _keys_for(values):
  return [x.key() for x in values]

def _users_to_accounts(users):
  return map(models.Account.get_account_for_user, users)

def _combine_users_and_groups(users, groupKeys):
  return models.AccountGroup.get(groupKeys) + _users_to_accounts(users)

def _field_to_approval_right(cleaned):
  result = models.ApprovalRight()
  result.files = cleaned["files"]
  (result.approvers_users,result.approvers_groups
      ) = fields.UserGroupField.get_user_and_group_keys(cleaned["approvers"])
  (result.verifiers_users,result.verifiers_groups
      ) = fields.UserGroupField.get_user_and_group_keys(cleaned["verifiers"])
  result.put()
  return result

def _approval_right_to_field(key):
  val = models.ApprovalRight.get(key)
  if not val:
    raise KeyError("no ApprovalRight for key: " + str(key))
  return {
      'files': val.files,
      'approvers': _combine_users_and_groups(val.approvers_users,
                      val.approvers_groups),
      'verifiers': _combine_users_and_groups(val.verifiers_users,
                      val.verifiers_groups),
    }

class AdminProjectForm(BaseForm):
  _template = 'admin_project.html'

  name = forms.CharField()
  comment = forms.CharField(required=False,
                            max_length=10000,
                            widget=forms.Textarea(attrs={'cols': 60}))
  owners = fields.UserGroupField(label='Project Lead(s)')
  code_reviews = fields.ApproversField()

  @classmethod
  def _init(cls, project):
     owners = fields.UserGroupField.field_value_for_keys(
                project.owners_users,
                project.owners_groups),
     return {'initial': {'name': project.name,
             'comment': project.comment,
             'owners': owners,
{'fraction_non_alphanumeric': 0.0852055449330784, 'fraction_numerical': 0.0074091778202676865, 'mean_word_length': 4.081360048573163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '7031738', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2386, 'n_words': 721}