OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """Servlet to export a project's config in JSON format. |
| 7 """ |
| 8 |
| 9 import logging |
| 10 import time |
| 11 |
| 12 from third_party import ezt |
| 13 |
| 14 from framework import permissions |
| 15 from framework import jsonfeed |
| 16 from framework import servlet |
| 17 from project import project_helpers |
| 18 from tracker import tracker_bizobj |
| 19 |
| 20 |
| 21 class ProjectExport(servlet.Servlet): |
| 22 """Only site admins can export a project""" |
| 23 |
| 24 _PAGE_TEMPLATE = 'project/project-export-page.ezt' |
| 25 _MAIN_TAB_MODE = servlet.Servlet.MAIN_TAB_ADMIN |
| 26 |
| 27 def AssertBasePermission(self, mr): |
| 28 """Make sure that the logged in user has permission to view this page.""" |
| 29 super(ProjectExport, self).AssertBasePermission(mr) |
| 30 if not mr.auth.user_pb.is_site_admin: |
| 31 raise permissions.PermissionException( |
| 32 'Only site admins may export project configuration') |
| 33 |
| 34 def GatherPageData(self, mr): |
| 35 """Build up a dictionary of data values to use when rendering the page.""" |
| 36 |
| 37 return { |
| 38 'admin_tab_mode': None, |
| 39 'page_perms': self.MakePagePerms(mr, None, permissions.CREATE_ISSUE), |
| 40 } |
| 41 |
| 42 |
| 43 class ProjectExportJSON(jsonfeed.JsonFeed): |
| 44 """ProjectExportJSON shows all configuration for a Project in JSON form.""" |
| 45 |
| 46 # Pretty-print the JSON output. |
| 47 JSON_INDENT = 4 |
| 48 |
| 49 def AssertBasePermission(self, mr): |
| 50 """Make sure that the logged in user has permission to view this page.""" |
| 51 super(ProjectExportJSON, self).AssertBasePermission(mr) |
| 52 if not mr.auth.user_pb.is_site_admin: |
| 53 raise permissions.PermissionException( |
| 54 'Only site admins may export project configuration') |
| 55 |
| 56 def HandleRequest(self, mr): |
| 57 """Build up a dictionary of data values to use when rendering the page. |
| 58 |
| 59 Args: |
| 60 mr: commonly used info parsed from the request. |
| 61 |
| 62 Returns: |
| 63 Dict of values used by EZT for rendering the page. |
| 64 """ |
| 65 project = self.services.project.GetProject(mr.cnxn, mr.project.project_id) |
| 66 user_id_set = project_helpers.UsersInvolvedInProject(project) |
| 67 |
| 68 config = self.services.config.GetProjectConfig( |
| 69 mr.cnxn, mr.project.project_id) |
| 70 user_id_set.update(tracker_bizobj.UsersInvolvedInConfig(config)) |
| 71 |
| 72 # The value 0 indicates "no user", e.g., that an issue has no owner. |
| 73 # We don't need to create a User row to represent that. |
| 74 user_id_set.discard(0) |
| 75 email_dict = self.services.user.LookupUserEmails(mr.cnxn, user_id_set) |
| 76 |
| 77 project_json = self._MakeProjectJSON(project, email_dict) |
| 78 config_json = self._MakeConfigJSON(config, email_dict) |
| 79 |
| 80 json_data = { |
| 81 'metadata': { |
| 82 'version': 1, |
| 83 'when': int(time.time()), |
| 84 'who': mr.auth.email, |
| 85 }, |
| 86 'project': project_json, |
| 87 'config': config_json, |
| 88 # This list could be derived from the others, but we provide it for |
| 89 # ease of processing. |
| 90 'emails': email_dict.values(), |
| 91 } |
| 92 return json_data |
| 93 |
| 94 def _MakeProjectJSON(self, project, email_dict): |
| 95 project_json = { |
| 96 'name': project.project_name, |
| 97 'summary': project.summary, |
| 98 'description': project.description, |
| 99 'state': project.state.name, |
| 100 'access': project.access.name, |
| 101 'owners': [email_dict.get(user) for user in project.owner_ids], |
| 102 'committers': [email_dict.get(user) for user in project.committer_ids], |
| 103 'contributors': [ |
| 104 email_dict.get(user) for user in project.contributor_ids], |
| 105 'perms': [self._MakePermJSON(perm, email_dict) |
| 106 for perm in project.extra_perms], |
| 107 'issue_notify_address': project.issue_notify_address, |
| 108 'attachment_bytes': project.attachment_bytes_used, |
| 109 'attachment_quota': project.attachment_quota, |
| 110 'recent_activity': project.recent_activity, |
| 111 'process_inbound_email': project.process_inbound_email, |
| 112 'only_owners_remove_restrictions': |
| 113 project.only_owners_remove_restrictions, |
| 114 'only_owners_see_contributors': project.only_owners_see_contributors, |
| 115 'revision_url_format': project.revision_url_format, |
| 116 'read_only_reason': project.read_only_reason, |
| 117 } |
| 118 return project_json |
| 119 |
| 120 def _MakePermJSON(self, perm, email_dict): |
| 121 perm_json = { |
| 122 'member': email_dict.get(perm.member_id), |
| 123 'perms': [p for p in perm.perms], |
| 124 } |
| 125 return perm_json |
| 126 |
| 127 def _MakeConfigJSON(self, config, email_dict): |
| 128 config_json = { |
| 129 'statuses': |
| 130 [self._MakeStatusJSON(status) |
| 131 for status in config.well_known_statuses], |
| 132 'statuses_offer_merge': |
| 133 [status for status in config.statuses_offer_merge], |
| 134 'labels': |
| 135 [self._MakeLabelJSON(label) for label in config.well_known_labels], |
| 136 'exclusive_label_prefixes': |
| 137 [label for label in config.exclusive_label_prefixes], |
| 138 # TODO(agable): Export the projects FieldDefs (not yet used). |
| 139 'components': |
| 140 [self._MakeComponentJSON(component, email_dict) |
| 141 for component in config.component_defs], |
| 142 'templates': |
| 143 [self._MakeTemplateJSON(template, email_dict) |
| 144 for template in config.templates], |
| 145 'developer_template': config.default_template_for_developers, |
| 146 'user_template': config.default_template_for_users, |
| 147 'list_cols': config.default_col_spec, |
| 148 'list_spec': config.default_sort_spec, |
| 149 'grid_x': config.default_x_attr, |
| 150 'grid_y': config.default_y_attr, |
| 151 'only_known_values': config.restrict_to_known, |
| 152 } |
| 153 if config.custom_issue_entry_url: |
| 154 config_json.update({'issue_entry_url': config.custom_issue_entry_url}) |
| 155 return config_json |
| 156 |
| 157 def _MakeTemplateJSON(self, template, email_dict): |
| 158 template_json = { |
| 159 'name': template.name, |
| 160 'summary': template.summary, |
| 161 'content': template.content, |
| 162 'summary_must_be_edited': template.summary_must_be_edited, |
| 163 'owner': email_dict.get(template.owner_id), |
| 164 'status': template.status, |
| 165 'labels': [label for label in template.labels], |
| 166 # TODO(agable): Export the template's default Fields (not yet used). |
| 167 'members_only': template.members_only, |
| 168 'owner_defaults_to_member': template.owner_defaults_to_member, |
| 169 'component_required': template.component_required, |
| 170 'admins': [email_dict(user) for user in template.admin_ids], |
| 171 } |
| 172 return template_json |
| 173 |
| 174 def _MakeStatusJSON(self, status): |
| 175 status_json = { |
| 176 'status': status.status, |
| 177 'open': status.means_open, |
| 178 'docstring': status.status_docstring, |
| 179 } |
| 180 return status_json |
| 181 |
| 182 def _MakeLabelJSON(self, label): |
| 183 label_json = { |
| 184 'label': label.label, |
| 185 'docstring': label.label_docstring, |
| 186 } |
| 187 return label_json |
| 188 |
| 189 def _MakeComponentJSON(self, component, email_dict): |
| 190 component_json = { |
| 191 'path': component.path, |
| 192 'docstring': component.docstring, |
| 193 'admins': [email_dict.get(user) for user in component.admin_ids], |
| 194 'ccs': [email_dict.get(user) for user in component.cc_ids], |
| 195 } |
| 196 return component_json |
OLD | NEW |