Skip to content

Commit c7e1392

Browse files
committed
Address review points
Re-capitalise SQL keywords. Improve `Subscription.select()` to handle `unicode` values. Add `Susbcription.__str__()` to provide a string represention of a subscription. Refactor `Subscription._from_dict()` method to use `select()` as a single source for select queries and `__str__()` to improve logging.
1 parent e962ba0 commit c7e1392

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

code_comments/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def create_tables(env, db):
5050
for stmt in to_sql(env, schema[table_name]):
5151
cursor.execute(stmt)
5252
cursor.execute(
53-
"insert into system values ('code_comments_schema_version', %s)",
53+
"INSERT INTO system VALUES ('code_comments_schema_version', %s)",
5454
str(db_version))
5555

5656

code_comments/subscription.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,25 @@ def __init__(self, env, data=None):
2626
self.__dict__ = data
2727
self.env = env
2828

29+
def __str__(self):
30+
"""
31+
Returns a user friendly string representation.
32+
"""
33+
template = "{0} as {1} for {2} {3}"
34+
if self.type == "changeset":
35+
_identifier = self.rev
36+
elif self.type == "browser":
37+
_identifier = "{0} @ {1}".format(self.path, self.rev)
38+
else:
39+
_identifier = self.path
40+
41+
return template.format(self.user, self.role, self.type, _identifier)
42+
2943
@classmethod
3044
def select(cls, env, args={}, notify=None):
45+
"""
46+
Retrieve existing subscription(s).
47+
"""
3148
select = 'SELECT * FROM code_comments_subscriptions'
3249
if notify:
3350
args['notify'] = bool(notify)
@@ -36,7 +53,7 @@ def select(cls, env, args={}, notify=None):
3653
criteria = []
3754
for key, value in args.iteritems():
3855
template = '{0}={1}'
39-
if isinstance(value, str):
56+
if isinstance(value, str) or isinstance(value, unicode):
4057
template = '{0}=\'{1}\''
4158
if (isinstance(value, tuple) or isinstance(value, list)):
4259
template = '{0} IN (\'{1}\')'
@@ -130,25 +147,37 @@ def _from_dict(cls, env, dict_):
130147
"""
131148
Creates a subscription from a dict.
132149
"""
133-
cursor = env.get_read_db().cursor()
134-
select = ("SELECT * FROM code_comments_subscriptions WHERE "
135-
"user=%s AND type=%s AND path=%s AND "
136-
"repos=%s AND rev=%s AND notify=%s"
137-
)
138-
values = (dict_['user'], dict_['type'], dict_['path'], dict_['repos'],
139-
dict_['rev'], dict_['notify'])
140-
cursor.execute(select, values)
141-
row = cursor.fetchone()
142-
if row:
143-
env.log.debug(
144-
'Subscription for {type} already exists'.format(**dict_))
145-
return cls._from_row(env, row)
146-
else:
147-
env.log.debug(
148-
'Subscription for {type} created'.format(**dict_))
150+
subscription = None
151+
152+
# Look for existing subscriptions
153+
args = {
154+
'user': dict_['user'],
155+
'type': dict_['type'],
156+
'path': dict_['path'],
157+
'repos': dict_['repos'],
158+
'rev': dict_['rev'],
159+
}
160+
subscriptions = cls.select(env, args)
161+
162+
# Only return the first one
163+
for _subscription in subscriptions:
164+
if subscription is None:
165+
subscription = _subscription
166+
env.log.debug(
167+
'Subscription already exists: %s', str(subscription))
168+
else:
169+
# The unique constraint on the table should prevent this ever
170+
# occurring
171+
env.log.debug('Multiple subscriptions found!')
172+
173+
# Create a new subscription if we didn't find one
174+
if subscription is None:
149175
subscription = cls(env, dict_)
150176
subscription.insert()
151-
return subscription
177+
env.log.debug(
178+
'Subscription created: %s', str(subscription))
179+
180+
return subscription
152181

153182
@classmethod
154183
def from_attachment(cls, env, attachment, user=None, role='author',

0 commit comments

Comments
 (0)