Skip to content

Commit

Permalink
[#1635] Remove unnecessary @classmethods from dashboard model
Browse files Browse the repository at this point in the history
This moves some logic out of the dashboard model and into the lib, which is
probably where it belongs as that's where the rest of the relevant logic is,
and overall this makes the code shorter as well.
  • Loading branch information
Sean Hammond committed Nov 27, 2012
1 parent 2ea41a7 commit 12cea9b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 49 deletions.
9 changes: 5 additions & 4 deletions ckan/lib/email_notifications.py
Expand Up @@ -106,9 +106,10 @@ def send_notification(user, email_dict):
ckan.lib.mailer.mail_recipient(user['display_name'], user['email'],
email_dict['subject'], email_dict['body'])
# FIXME: We are accessing model from lib here but I'm not sure what
# else to do unless we add a update_activity_stream_last_viewed()
# else to do unless we add a update_email_last_sent()
# logic function which would only be needed by this lib.
model.Dashboard.update_email_last_sent(user['id'])
model.Dashboard.get(user['id']).email_last_sent = (
datetime.datetime.now())
# TODO: Do something with response?
except ckan.lib.mailer.MailerException:
raise
Expand All @@ -119,9 +120,9 @@ def get_and_send_notifications_for_user(user):
# was sent _or_ the time the user last viewed her dashboard, whichever is
# newer.
# FIXME: We are accessing model from lib here but I'm not sure what else
# to do unless we add a get_activity_stream_last_viewed() logic function
# to do unless we add a get_email_last_sent() logic function
# which would only be needed by this lib.
since = model.Dashboard.get_email_last_sent(user['id'])
since = model.Dashboard.get(user['id']).email_last_sent

notifications = get_notifications(user['id'], since)
# TODO: Handle failures from send_email_notification.
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/action/get.py
Expand Up @@ -2225,7 +2225,7 @@ def dashboard_activity_list(context, data_dict):
# Mark the new (not yet seen by user) activities.
strptime = datetime.datetime.strptime
fmt = '%Y-%m-%dT%H:%M:%S.%f'
last_viewed = model.Dashboard.get_activity_stream_last_viewed(user_id)
last_viewed = model.Dashboard.get(user_id).activity_stream_last_viewed
for activity in activity_dicts:
if activity['user_id'] == user_id:
# Never mark the user's own activities as new.
Expand Down
5 changes: 4 additions & 1 deletion ckan/logic/action/update.py
Expand Up @@ -949,4 +949,7 @@ def dashboard_mark_activities_old(context, data_dict):
data_dict)
model = context['model']
user_id = model.User.get(context['user']).id
model.Dashboard.update_activity_stream_last_viewed(user_id)
model.Dashboard.get(user_id).activity_stream_last_viewed = (
datetime.datetime.now())
if not context.get('defer_commit'):
model.repo.commit()
50 changes: 7 additions & 43 deletions ckan/model/dashboard.py
Expand Up @@ -23,57 +23,21 @@ def __init__(self, user_id):
self.email_last_sent = datetime.datetime.now()

@classmethod
def _get(cls, user_id):
'''
def get(cls, user_id):
'''Return the Dashboard object for the given user_id.
:raises: sqlalchemy.orm.exc.NoResultFound
If there's no dashboard row in the database for this user_id, a fresh
one will be created and returned.
'''
query = meta.Session.query(Dashboard)
query = query.filter(Dashboard.user_id == user_id)
row = query.one()
return row

@classmethod
def get_activity_stream_last_viewed(cls, user_id):
try:
row = cls._get(user_id)
return row.activity_stream_last_viewed
except sqlalchemy.orm.exc.NoResultFound:
# No dashboard row has been created for this user so they have no
# activity_stream_last_viewed date. Return the oldest date we can
# (i.e. all activities are new to this user).
return datetime.datetime.min

@classmethod
def update_activity_stream_last_viewed(cls, user_id):
try:
row = cls._get(user_id)
row.activity_stream_last_viewed = datetime.datetime.now()
row = query.one()
except sqlalchemy.orm.exc.NoResultFound:
row = Dashboard(user_id)
meta.Session.add(row)
meta.Session.commit()

@classmethod
def get_email_last_sent(cls, user_id):
try:
row = cls._get(user_id)
return row.email_last_sent
except sqlalchemy.orm.exc.NoResultFound:
# No dashboard row has been created for this user so they have no
# email_last_sent date. Return the oldest date we can (i.e. all
# activities are new to this user).
return datetime.datetime.min

@classmethod
def update_email_last_sent(cls, user_id):
try:
row = cls._get(user_id)
row.email_last_sent = datetime.datetime.now()
except sqlalchemy.orm.exc.NoResultFound:
row = Dashboard(user_id)
meta.Session.add(row)
meta.Session.commit()
meta.Session.commit()
return row

meta.mapper(Dashboard, dashboard_table)

0 comments on commit 12cea9b

Please sign in to comment.