Skip to content

Commit f663d78

Browse files
authored
Merge pull request #817 from uw-it-aca/hotfix/MUWM-3902
Hotfix/muwm 3902
2 parents 011d941 + d5420b4 commit f663d78

File tree

4 files changed

+330
-126
lines changed

4 files changed

+330
-126
lines changed

myuw/dao/quicklinks.py

+94-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import csv
2+
import logging
3+
import os
4+
from django.db import transaction, IntegrityError
15
from myuw.models import VisitedLink, PopularLink, CustomLink, HiddenLink
26
from myuw.dao import get_netid_of_current_user, get_user_model
37
from myuw.dao.affiliation_data import get_data_for_affiliations
4-
import csv
5-
import os
68

79

810
RECENT_LINKS_DISPLAY_LIMIT = 5
911
CACHED_LABEL_DATA = {}
12+
logger = logging.getLogger(__name__)
1013

1114

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

100103
return defaults
104+
105+
106+
def add_custom_link(url, link_label=None):
107+
try:
108+
with transaction.atomic():
109+
return CustomLink.objects.create(user=get_user_model(),
110+
url=url,
111+
label=link_label)
112+
except IntegrityError:
113+
try:
114+
return get_custom_link_by_url(url)
115+
except Exception as ex:
116+
logger.error("%s add_custom_link(%s, %s) ==> %s",
117+
get_netid_of_current_user(), url, link_label, ex)
118+
return None
119+
120+
121+
def delete_custom_link(link_id):
122+
try:
123+
link = get_custom_link_by_id(link_id)
124+
return link.delete()
125+
except Exception as ex:
126+
logger.error("%s delete_custom_link(%s) ==> %s",
127+
get_netid_of_current_user(), link_id, ex)
128+
return None
129+
130+
131+
def edit_custom_link(link_id, new_url, new_label=None):
132+
try:
133+
link = get_custom_link_by_id(link_id)
134+
link.url = new_url
135+
if new_label is not None and len(new_label):
136+
link.label = new_label
137+
link.save()
138+
return link
139+
except Exception as ex:
140+
logger.error("%s edit_custom_link(%s, %s, %s) ==> %s",
141+
get_netid_of_current_user(), link_id,
142+
new_url, new_label, ex)
143+
return None
144+
145+
146+
def get_custom_link_by_id(link_id):
147+
return CustomLink.objects.get(pk=link_id, user=get_user_model())
148+
149+
150+
def get_custom_link_by_url(url):
151+
return CustomLink.objects.get(user=get_user_model(), url=url)
152+
153+
154+
def add_hidden_link(url):
155+
try:
156+
with transaction.atomic():
157+
return HiddenLink.objects.create(user=get_user_model(),
158+
url=url)
159+
except IntegrityError:
160+
try:
161+
return get_hidden_link_by_url(url)
162+
except Exception as ex:
163+
logger.error("%s add_hidden_link(%s) ==> %s",
164+
get_netid_of_current_user(), url, ex)
165+
return None
166+
167+
168+
def delete_hidden_link(link_id):
169+
try:
170+
link = get_hidden_link_by_id(link_id)
171+
return link.delete()
172+
except Exception as ex:
173+
logger.error("%s delete_hidden_link(%s) ==> %s",
174+
get_netid_of_current_user(), link_id, ex)
175+
return None
176+
177+
178+
def get_hidden_link_by_id(link_id):
179+
return HiddenLink.objects.get(pk=link_id, user=get_user_model())
180+
181+
182+
def get_hidden_link_by_url(url):
183+
return HiddenLink.objects.get(user=get_user_model(), url=url)
184+
185+
186+
def get_popular_link_by_id(link_id):
187+
return PopularLink.objects.get(pk=link_id)
188+
189+
190+
def get_recent_link_by_id(link_id):
191+
return VisitedLink.objects.get(pk=link_id,
192+
username=get_netid_of_current_user())

myuw/test/api/test_quicklinks.py

+58-41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
from myuw.test.api import MyuwApiTest
2-
from myuw.models import VisitedLink, PopularLink, CustomLink, HiddenLink
1+
import json
32
from django.core.urlresolvers import reverse
43
from myuw.test import fdao_class_website_override
5-
import json
4+
from myuw.test.api import MyuwApiTest
5+
from myuw.models import VisitedLink, PopularLink, CustomLink, HiddenLink
6+
from myuw.views.api.link import get_link_data
67

78

89
@fdao_class_website_override
910
class TestQuickLinksAPI(MyuwApiTest):
11+
12+
def test_get_link_data(self):
13+
data = {'type': 'custom',
14+
'url': 'www.washington.edu/'
15+
}
16+
url, label = get_link_data(data, get_id=False)
17+
self.assertEquals(url, 'http://www.washington.edu/')
18+
self.assertEquals(label, 'http://www.washington.edu/')
19+
20+
data = {'type': 'custom',
21+
'url': 'www.washington.edu/',
22+
'label': 'UW Homepage'
23+
}
24+
url, label = get_link_data(data, get_id=False)
25+
self.assertEquals(label, 'UW Homepage')
26+
27+
data = {'type': 'custom',
28+
'url': 'www.washington.edu/',
29+
'label': 'UW Homepage',
30+
'id': 1
31+
}
32+
link_id, url, label = get_link_data(data)
33+
self.assertEquals(link_id, 1)
34+
1035
def test_add_popular_link(self):
1136
PopularLink.objects.all().delete()
1237
CustomLink.objects.all().delete()
@@ -109,9 +134,9 @@ def test_bad_syntax(self):
109134
self.assertEquals(response.status_code, 404)
110135

111136
def test_add_pure_custom(self):
137+
CustomLink.objects.all().delete()
112138
self.set_user('javerage')
113139
url = reverse('myuw_manage_links')
114-
CustomLink.objects.all().delete()
115140

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

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

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

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

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

158188
response = self.client.post(url, data, content_type='application_json')
159-
self.assertEqual(response.status_code, 404)
189+
self.assertEqual(response.status_code, 200)
190+
all = CustomLink.objects.all()
191+
self.assertEqual(len(all), 3)
160192

161193
def test_edit_custom_link(self):
194+
CustomLink.objects.all().delete()
162195
self.set_user('javerage')
163196
url = reverse('myuw_manage_links')
164-
CustomLink.objects.all().delete()
165-
197+
# add link
166198
data = json.dumps({'type': 'custom',
167199
'url': 'www.washington.edu/classroom/SMI+401'
168200
})
169201

170202
response = self.client.post(url, data, content_type='application_json')
171203
self.assertEqual(response.status_code, 200)
172-
173-
link_id = CustomLink.objects.all()[0].pk
174-
175-
# Try to edit the link as someone else
176-
self.set_user('jpce')
204+
all = CustomLink.objects.all()
205+
self.assertEqual(len(all), 1)
206+
# edit
207+
link_id = all[0].pk
177208
data = json.dumps({'type': 'custom-edit',
178209
'url': 'http://example.com',
179210
'label': 'Just example',
180211
'id': link_id,
181212
})
182213
response = self.client.post(url, data, content_type='application_json')
183-
self.assertEqual(response.status_code, 404)
184-
185-
all = CustomLink.objects.all()
186-
self.assertEquals(len(all), 1)
187-
link = all[0]
188-
self.assertEquals(link.url,
189-
'http://www.washington.edu/classroom/SMI+401')
190-
191-
self.set_user('javerage')
192-
response = self.client.post(url, data, content_type='application_json')
193214
self.assertEqual(response.status_code, 200)
194-
195215
all = CustomLink.objects.all()
196216
self.assertEquals(len(all), 1)
197217
link = all[0]
@@ -200,7 +220,7 @@ def test_edit_custom_link(self):
200220

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

217237
def test_remove_link(self):
238+
CustomLink.objects.all().delete()
239+
218240
# Add a link as 2 users, make sure we can remove ours, but not theirs
219241
self.set_user('javerage')
220242
url = reverse('myuw_manage_links')
221-
CustomLink.objects.all().delete()
222-
223243
data = json.dumps({'type': 'custom',
224244
'url': 'www.washington.edu/classroom/SMI+401'
225245
})
@@ -258,26 +278,23 @@ def test_remove_default_by_url(self):
258278
self.set_user('javerage')
259279
url = reverse('myuw_manage_links')
260280

281+
# add HiddenLink
261282
data = json.dumps({'type': 'hide',
262283
'id': 'http://example.com'})
263-
264284
response = self.client.post(url, data, content_type='application_json')
265285
self.assertEquals(response.status_code, 200)
266286
all = HiddenLink.objects.all()
267-
268287
self.assertEqual(len(all), 1)
269288
self.assertEqual(all[0].url, 'http://example.com')
270-
289+
# same link second time
271290
response = self.client.post(url, data, content_type='application_json')
272291
self.assertEquals(response.status_code, 200)
273292
all = HiddenLink.objects.all()
274-
275293
self.assertEqual(len(all), 1)
276-
294+
# Hide a non-default
277295
data = json.dumps({'type': 'hide',
278-
'id': 'http://uw.edu'})
279-
296+
'url': 'http://uw.edu'})
280297
response = self.client.post(url, data, content_type='application_json')
281-
self.assertEquals(response.status_code, 200)
298+
self.assertEquals(response.status_code, 404)
282299
all = HiddenLink.objects.all()
283-
self.assertEqual(len(all), 2)
300+
self.assertEqual(len(all), 1)

0 commit comments

Comments
 (0)