-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds_reset.py
executable file
·134 lines (106 loc) · 4.2 KB
/
ds_reset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
#
# Copyright 2011 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.
"""Datastore reset page for the application."""
from datetime import datetime
import re
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import model
class DatastoreResetPage(webapp.RequestHandler):
"""Handler for datastore reset requests."""
def post(self):
"""Resets the datastore."""
# Ignore if not local.
if not re.search('(appspot)', self.request.host):
for incident in model.Incident.all():
incident.delete()
for message in model.Message.all():
message.delete()
user = users.get_current_user()
# Create an incident for the user.
self.CreateIncident('Incident for ' + user.nickname(), user.nickname())
# Creates an unassigned incident.
self.CreateIncident('Unassigned incident')
# Creates an incident assigned to 'some_user' if one doesn't exist.
if user.nickname() is not 'some_user':
self.CreateIncident('Incident for some_user',
owner='some_user')
# Creates an incident with the accepted tag of 'API-Test'.
self.CreateIncident('API-Test', accepted_tags=['API-Test'])
# Creates an incident with the accepted tag of 'Special-ToAssignTag'.
self.CreateIncident('To assign tag',
accepted_tags=['Special-ToAssignTag'])
# Creates an incident to be resolved.
self.CreateIncident('To resolve', accepted_tags=['Special-ToResolve'])
# Creates a resolved incident.
self.CreateIncident('Resolved', status='resolved')
def CreateIncident(self, title, owner='none', accepted_tags=None,
suggested_tags=None, status='new'):
"""Creates an incident with limited customization.
Args:
title: Title of the incident
owner: Optionally specifies the owner of the incident.
accepted_tags: Optional list of accepted_tags applied to the incident.
suggested_tags: Optional list of suggested_tags applied to the incident.
status: Optional string status for the new incident.
"""
# Set empty tags outside of the default constructor, in case we ever need
# to modify these later.
if not accepted_tags:
accepted_tags = []
if not suggested_tags:
suggested_tags = []
incident = model.Incident()
incident.title = title
incident.created = datetime.now()
incident.status = status
incident.owner = owner
incident.author = 'test@example.com'
incident.mailing_list = 'support@example.com'
incident.canonical_link = 'http://google.com'
incident.suggested_tags = suggested_tags
incident.accepted_tags = accepted_tags
incident.put()
self.CreateMessages(incident)
def CreateMessages(self, incident):
"""Creates messages associated with the supplied incident.
Args:
incident: Incident to which messages should be appended.
"""
in_reply_to = None
for j in range(2):
message = model.Message()
message.title = 'Message #' + str(j)
message.incident = incident
message.in_reply_to = in_reply_to
message.message_id = 'message-%s-%s' % (incident.key, str(j))
message.author = 'text@example.com'
message.body = 'Text'
message.sent = datetime.now()
message.mailing_list = 'support@example.com'
message.canonical_link = 'http://google.com'
message.put()
in_reply_to = message.message_id
application = webapp.WSGIApplication(
[
('/ds_reset', DatastoreResetPage)
],
debug=True)
def main():
"""Runs the application."""
run_wsgi_app(application)
if __name__ == '__main__':
main()