Merge lp:~fo0bar/turku/turku-api-py3 into lp:turku/turku-api

Proposed by Ryan Finnie
Status: Merged
Approved by: Joel Sing
Approved revision: 61
Merged at revision: 62
Proposed branch: lp:~fo0bar/turku/turku-api-py3
Merge into: lp:turku/turku-api
Diff against target: 226 lines (+30/-25)
6 files modified
manage.py (+1/-1)
scripts/turku_health (+9/-4)
turku_api/admin.py (+1/-1)
turku_api/models.py (+8/-8)
turku_api/settings.py (+1/-1)
turku_api/views.py (+10/-10)
To merge this branch: bzr merge lp:~fo0bar/turku/turku-api-py3
Reviewer Review Type Date Requested Status
Canonical IS Reviewers Pending
Review via email: mp+381137@code.launchpad.net

Commit message

Convert to Python 3

Description of the change

Note that this just happens to be backwards compatible with Python 2.7,
even though we are treating this as a hard conversion. Future commits
might not be backwards compatible.

Welcome to the 21st century.

To post a comment you must log in.
Revision history for this message
Canonical IS Mergebot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Joel Sing (jsing) wrote :

LGTM, thanks.

Revision history for this message
Canonical IS Mergebot (canonical-is-mergebot) wrote :

Change successfully merged at revision 62

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'manage.py'
2--- manage.py 2015-07-30 22:41:42 +0000
3+++ manage.py 2020-03-24 23:11:14 +0000
4@@ -1,4 +1,4 @@
5-#!/usr/bin/env python
6+#!/usr/bin/env python3
7
8 # Turku backups - API server
9 # Copyright 2015 Canonical Ltd.
10
11=== modified file 'scripts/turku_health'
12--- scripts/turku_health 2015-03-27 07:22:30 +0000
13+++ scripts/turku_health 2020-03-24 23:11:14 +0000
14@@ -1,4 +1,4 @@
15-#!/usr/bin/env python
16+#!/usr/bin/env python3
17
18 # Turku backups
19 # Copyright 2015 Canonical Ltd.
20@@ -18,9 +18,14 @@
21 import os
22 import sys
23
24+import django
25+
26+
27 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
28 sys.path.append(BASE_DIR)
29 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "turku_api.settings")
30+django.setup()
31+
32
33 from turku_api.models import Machine, Source, Storage
34
35@@ -51,10 +56,10 @@
36 sick_objects.append(repr(source))
37
38 if (storages_sick > 0) or (machines_sick > 0) or (sources_sick > 0):
39- print 'CRITICAL %d/%d storages, %d/%d machines, %d/%d sources' % (storages_sick, storages_total, machines_sick, machines_total, sources_sick, sources_total)
40+ print('CRITICAL %d/%d storages, %d/%d machines, %d/%d sources' % (storages_sick, storages_total, machines_sick, machines_total, sources_sick, sources_total))
41 for object in sick_objects:
42- print object
43+ print(object)
44 sys.exit(2)
45 else:
46- print 'OK %d storages, %d machines, %d sources' % (storages_total, machines_total, sources_total)
47+ print('OK %d storages, %d machines, %d sources' % (storages_total, machines_total, sources_total))
48 sys.exit(0)
49
50=== modified file 'turku_api/admin.py'
51--- turku_api/admin.py 2020-03-24 21:50:17 +0000
52+++ turku_api/admin.py 2020-03-24 23:11:14 +0000
53@@ -32,7 +32,7 @@
54 if not name:
55 name = obj
56 return format_html(
57- '<a href="%s">%s</a>' % (url, unicode(name))
58+ '<a href="{}">{}</a>'.format(url, name)
59 )
60
61
62
63=== modified file 'turku_api/models.py'
64--- turku_api/models.py 2020-03-24 21:50:17 +0000
65+++ turku_api/models.py 2020-03-24 23:11:14 +0000
66@@ -45,10 +45,10 @@
67 decoded_json = json.loads(value)
68 except ValueError:
69 raise ValidationError('Must be a valid JSON string list')
70- if not type(decoded_json) == list:
71+ if not isinstance(decoded_json, (list, tuple, set)):
72 raise ValidationError('Must be a valid JSON string list')
73 for i in decoded_json:
74- if not type(i) in (str, unicode):
75+ if not isinstance(i, str):
76 raise ValidationError('Must be a valid JSON string list')
77
78
79@@ -113,7 +113,7 @@
80 help_text='Date/time this auth was added.',
81 )
82
83- def __unicode__(self):
84+ def __str__(self):
85 return self.name
86
87
88@@ -198,7 +198,7 @@
89 help_text='Date/time this storage unit last checked in.',
90 )
91
92- def __unicode__(self):
93+ def __str__(self):
94 return self.name
95
96
97@@ -277,7 +277,7 @@
98 help_text='Date/time this machine last checked in.',
99 )
100
101- def __unicode__(self):
102+ def __str__(self):
103 return '%s (%s)' % (self.unit_name, self.uuid[0:8])
104
105
106@@ -393,7 +393,7 @@
107 class Meta:
108 unique_together = (('machine', 'name'),)
109
110- def __unicode__(self):
111+ def __str__(self):
112 return '%s %s' % (self.machine.unit_name, self.name)
113
114
115@@ -432,7 +432,7 @@
116 help_text='Summary of the backup\'s events.',
117 )
118
119- def __unicode__(self):
120+ def __str__(self):
121 return '%s %s' % (str(self.source), self.date.strftime('%Y-%m-%d %H:%M:%S'))
122
123
124@@ -459,5 +459,5 @@
125 help_text='Date/time this filter set was added.',
126 )
127
128- def __unicode__(self):
129+ def __str__(self):
130 return self.name
131
132=== modified file 'turku_api/settings.py'
133--- turku_api/settings.py 2020-03-24 21:50:17 +0000
134+++ turku_api/settings.py 2020-03-24 23:11:14 +0000
135@@ -71,6 +71,6 @@
136 SECRET_KEY = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(30))
137
138 try:
139- from local_settings import *
140+ from turku_api.local_settings import *
141 except ImportError:
142 pass
143
144=== modified file 'turku_api/views.py'
145--- turku_api/views.py 2019-10-29 10:07:18 +0000
146+++ turku_api/views.py 2020-03-24 23:11:14 +0000
147@@ -99,9 +99,9 @@
148
149 def random_weighted(m):
150 """Return a weighted random key."""
151- total = sum([v for v in m.values()])
152+ total = sum(m.values())
153 if total <= 0:
154- return random.choice(m.keys())
155+ return random.choice(list(m.keys()))
156 weighted = []
157 tp = 0
158 for (k, v) in m.items():
159@@ -186,7 +186,7 @@
160 # Check for global auth
161 if 'auth' not in self.req:
162 raise HttpResponseException(HttpResponseForbidden('Bad auth'))
163- if type(self.req['auth']) == dict:
164+ if isinstance(self.req['auth'], dict):
165 if not (('name' in self.req['auth']) and ('secret' in self.req['auth'])):
166 raise HttpResponseException(HttpResponseForbidden('Bad auth'))
167 try:
168@@ -203,7 +203,7 @@
169 raise HttpResponseException(HttpResponseForbidden('Bad auth'))
170
171 def update_config(self):
172- if not (('machine' in self.req) and (type(self.req['machine']) == dict)):
173+ if not (('machine' in self.req) and (isinstance(self.req['machine'], dict))):
174 raise HttpResponseException(HttpResponseBadRequest('"machine" dict required'))
175 req_machine = self.req['machine']
176
177@@ -271,12 +271,12 @@
178
179 if 'sources' in req_machine:
180 req_sources = req_machine['sources']
181- if not type(req_sources) == dict:
182+ if not isinstance(req_sources, dict):
183 raise HttpResponseException(HttpResponseBadRequest('Invalid type for "sources"'))
184 elif 'sources' in self.req:
185 # XXX legacy
186 req_sources = self.req['sources']
187- if not type(req_sources) == dict:
188+ if not isinstance(req_sources, dict):
189 raise HttpResponseException(HttpResponseBadRequest('Invalid type for "sources"'))
190 else:
191 req_sources = {}
192@@ -318,7 +318,7 @@
193 raise HttpResponseException(HttpResponseBadRequest('Validation error: %s' % str(e)))
194 s.save()
195
196- for name in req_sources.keys():
197+ for name in req_sources:
198 if name in sources_in_db:
199 continue
200 s = Source()
201@@ -413,7 +413,7 @@
202 return scheduled_sources
203
204 def agent_ping_checkin(self):
205- if not (('machine' in self.req) and (type(self.req['machine']) == dict)):
206+ if not (('machine' in self.req) and (isinstance(self.req['machine'], dict))):
207 raise HttpResponseException(HttpResponseBadRequest('"machine" dict required'))
208 req_machine = self.req['machine']
209
210@@ -447,7 +447,7 @@
211 return HttpResponse(json.dumps(out), content_type='application/json')
212
213 def agent_ping_restore(self):
214- if not (('machine' in self.req) and (type(self.req['machine']) == dict)):
215+ if not (('machine' in self.req) and (isinstance(self.req['machine'], dict))):
216 raise HttpResponseException(HttpResponseBadRequest('"machine" dict required'))
217 req_machine = self.req['machine']
218
219@@ -550,7 +550,7 @@
220 return HttpResponse(json.dumps({}), content_type='application/json')
221
222 def storage_update_config(self):
223- if not (('storage' in self.req) and (type(self.req['storage']) == dict)):
224+ if not (('storage' in self.req) and (isinstance(self.req['storage'], dict))):
225 raise HttpResponseException(HttpResponseBadRequest('"storage" dict required'))
226 req_storage = self.req['storage']
227

Subscribers

People subscribed via source and target branches

to all changes: