Skip to content

Commit

Permalink
Merge pull request #817 from uw-it-aca/hotfix/MUWM-3902
Browse files Browse the repository at this point in the history
Hotfix/muwm 3902
  • Loading branch information
fanglinfang authored Jul 31, 2017
2 parents 011d941 + d5420b4 commit f663d78
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 126 deletions.
96 changes: 94 additions & 2 deletions myuw/dao/quicklinks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import csv
import logging
import os
from django.db import transaction, IntegrityError
from myuw.models import VisitedLink, PopularLink, CustomLink, HiddenLink
from myuw.dao import get_netid_of_current_user, get_user_model
from myuw.dao.affiliation_data import get_data_for_affiliations
import csv
import os


RECENT_LINKS_DISPLAY_LIMIT = 5
CACHED_LABEL_DATA = {}
logger = logging.getLogger(__name__)


def get_quicklink_data(affiliations):
Expand Down Expand Up @@ -98,3 +101,92 @@ def _get_default_links(affiliations):
defaults.append({'url': link['URL'], 'label': link['Label']})

return defaults


def add_custom_link(url, link_label=None):
try:
with transaction.atomic():
return CustomLink.objects.create(user=get_user_model(),
url=url,
label=link_label)
except IntegrityError:
try:
return get_custom_link_by_url(url)
except Exception as ex:
logger.error("%s add_custom_link(%s, %s) ==> %s",
get_netid_of_current_user(), url, link_label, ex)
return None


def delete_custom_link(link_id):
try:
link = get_custom_link_by_id(link_id)
return link.delete()
except Exception as ex:
logger.error("%s delete_custom_link(%s) ==> %s",
get_netid_of_current_user(), link_id, ex)
return None


def edit_custom_link(link_id, new_url, new_label=None):
try:
link = get_custom_link_by_id(link_id)
link.url = new_url
if new_label is not None and len(new_label):
link.label = new_label
link.save()
return link
except Exception as ex:
logger.error("%s edit_custom_link(%s, %s, %s) ==> %s",
get_netid_of_current_user(), link_id,
new_url, new_label, ex)
return None


def get_custom_link_by_id(link_id):
return CustomLink.objects.get(pk=link_id, user=get_user_model())


def get_custom_link_by_url(url):
return CustomLink.objects.get(user=get_user_model(), url=url)


def add_hidden_link(url):
try:
with transaction.atomic():
return HiddenLink.objects.create(user=get_user_model(),
url=url)
except IntegrityError:
try:
return get_hidden_link_by_url(url)
except Exception as ex:
logger.error("%s add_hidden_link(%s) ==> %s",
get_netid_of_current_user(), url, ex)
return None


def delete_hidden_link(link_id):
try:
link = get_hidden_link_by_id(link_id)
return link.delete()
except Exception as ex:
logger.error("%s delete_hidden_link(%s) ==> %s",
get_netid_of_current_user(), link_id, ex)
return None


def get_hidden_link_by_id(link_id):
return HiddenLink.objects.get(pk=link_id, user=get_user_model())


def get_hidden_link_by_url(url):
return HiddenLink.objects.get(user=get_user_model(), url=url)


def get_popular_link_by_id(link_id):
return PopularLink.objects.get(pk=link_id)


def get_recent_link_by_id(link_id):
return VisitedLink.objects.get(pk=link_id,
username=get_netid_of_current_user())
99 changes: 58 additions & 41 deletions myuw/test/api/test_quicklinks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
from myuw.test.api import MyuwApiTest
from myuw.models import VisitedLink, PopularLink, CustomLink, HiddenLink
import json
from django.core.urlresolvers import reverse
from myuw.test import fdao_class_website_override
import json
from myuw.test.api import MyuwApiTest
from myuw.models import VisitedLink, PopularLink, CustomLink, HiddenLink
from myuw.views.api.link import get_link_data


@fdao_class_website_override
class TestQuickLinksAPI(MyuwApiTest):

def test_get_link_data(self):
data = {'type': 'custom',
'url': 'www.washington.edu/'
}
url, label = get_link_data(data, get_id=False)
self.assertEquals(url, 'http://www.washington.edu/')
self.assertEquals(label, 'http://www.washington.edu/')

data = {'type': 'custom',
'url': 'www.washington.edu/',
'label': 'UW Homepage'
}
url, label = get_link_data(data, get_id=False)
self.assertEquals(label, 'UW Homepage')

data = {'type': 'custom',
'url': 'www.washington.edu/',
'label': 'UW Homepage',
'id': 1
}
link_id, url, label = get_link_data(data)
self.assertEquals(link_id, 1)

def test_add_popular_link(self):
PopularLink.objects.all().delete()
CustomLink.objects.all().delete()
Expand Down Expand Up @@ -109,9 +134,9 @@ def test_bad_syntax(self):
self.assertEquals(response.status_code, 404)

def test_add_pure_custom(self):
CustomLink.objects.all().delete()
self.set_user('javerage')
url = reverse('myuw_manage_links')
CustomLink.objects.all().delete()

data = json.dumps({'type': 'custom',
'url': 'www.washington.edu/classroom/SMI+401'
Expand All @@ -127,7 +152,7 @@ def test_add_pure_custom(self):
'http://www.washington.edu/classroom/SMI+401')
self.assertEqual(all[0].label, 'Room Information')

# Same w/ protocol
# Add the same link but w/ protocol
data = json.dumps({'type': 'custom',
'url': 'http://www.washington.edu/classroom/SMI+401'
})
Expand All @@ -138,7 +163,7 @@ def test_add_pure_custom(self):
all = CustomLink.objects.all()
self.assertEqual(len(all), 1)

# https is different though
# https is different
http_url = 'https://www.washington.edu/classroom/SMI+401'
data = json.dumps({'type': 'custom',
'url': http_url
Expand All @@ -149,49 +174,44 @@ def test_add_pure_custom(self):

all = CustomLink.objects.all()
self.assertEqual(len(all), 2)
self.assertEqual(all[0].url,
'http://www.washington.edu/classroom/SMI+401')
self.assertEqual(all[1].url,
'https://www.washington.edu/classroom/SMI+401')

# Make sure we do a reasonable job w/ urls we can't resolve
data = json.dumps({'type': 'custom',
'url': 'http://www.washington.edu/classroom/404'
})
# not http/https url
data = json.dumps({
'type': 'custom',
'url': 'webcal://www.trumba.com/calendars/sea_acad-cal.ics'
})

response = self.client.post(url, data, content_type='application_json')
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, 200)
all = CustomLink.objects.all()
self.assertEqual(len(all), 3)

def test_edit_custom_link(self):
CustomLink.objects.all().delete()
self.set_user('javerage')
url = reverse('myuw_manage_links')
CustomLink.objects.all().delete()

# add link
data = json.dumps({'type': 'custom',
'url': 'www.washington.edu/classroom/SMI+401'
})

response = self.client.post(url, data, content_type='application_json')
self.assertEqual(response.status_code, 200)

link_id = CustomLink.objects.all()[0].pk

# Try to edit the link as someone else
self.set_user('jpce')
all = CustomLink.objects.all()
self.assertEqual(len(all), 1)
# edit
link_id = all[0].pk
data = json.dumps({'type': 'custom-edit',
'url': 'http://example.com',
'label': 'Just example',
'id': link_id,
})
response = self.client.post(url, data, content_type='application_json')
self.assertEqual(response.status_code, 404)

all = CustomLink.objects.all()
self.assertEquals(len(all), 1)
link = all[0]
self.assertEquals(link.url,
'http://www.washington.edu/classroom/SMI+401')

self.set_user('javerage')
response = self.client.post(url, data, content_type='application_json')
self.assertEqual(response.status_code, 200)

all = CustomLink.objects.all()
self.assertEquals(len(all), 1)
link = all[0]
Expand All @@ -200,7 +220,7 @@ def test_edit_custom_link(self):

# Make sure links actually have a label...
data = json.dumps({'type': 'custom-edit',
'url': 'http://example.com',
'url': 'www.washington.edu/classroom/SMI+401',
'label': ' ',
'id': link_id,
})
Expand All @@ -215,11 +235,11 @@ def test_edit_custom_link(self):
self.assertEquals(link.label, 'Just example')

def test_remove_link(self):
CustomLink.objects.all().delete()

# Add a link as 2 users, make sure we can remove ours, but not theirs
self.set_user('javerage')
url = reverse('myuw_manage_links')
CustomLink.objects.all().delete()

data = json.dumps({'type': 'custom',
'url': 'www.washington.edu/classroom/SMI+401'
})
Expand Down Expand Up @@ -258,26 +278,23 @@ def test_remove_default_by_url(self):
self.set_user('javerage')
url = reverse('myuw_manage_links')

# add HiddenLink
data = json.dumps({'type': 'hide',
'id': 'http://example.com'})

response = self.client.post(url, data, content_type='application_json')
self.assertEquals(response.status_code, 200)
all = HiddenLink.objects.all()

self.assertEqual(len(all), 1)
self.assertEqual(all[0].url, 'http://example.com')

# same link second time
response = self.client.post(url, data, content_type='application_json')
self.assertEquals(response.status_code, 200)
all = HiddenLink.objects.all()

self.assertEqual(len(all), 1)

# Hide a non-default
data = json.dumps({'type': 'hide',
'id': 'http://uw.edu'})

'url': 'http://uw.edu'})
response = self.client.post(url, data, content_type='application_json')
self.assertEquals(response.status_code, 200)
self.assertEquals(response.status_code, 404)
all = HiddenLink.objects.all()
self.assertEqual(len(all), 2)
self.assertEqual(len(all), 1)
Loading

0 comments on commit f663d78

Please sign in to comment.