Merge lp:~michael.nelson/ubuntu-webcatalog/962140-display-video-demos into lp:ubuntu-webcatalog

Proposed by Michael Nelson
Status: Merged
Approved by: Anthony Lenton
Approved revision: 124
Merged at revision: 115
Proposed branch: lp:~michael.nelson/ubuntu-webcatalog/962140-display-video-demos
Merge into: lp:ubuntu-webcatalog
Diff against target: 195 lines (+80/-21)
3 files modified
src/webcatalog/forms.py (+16/-13)
src/webcatalog/management/commands/import_for_purchase_apps.py (+3/-6)
src/webcatalog/tests/test_forms.py (+61/-2)
To merge this branch: bzr merge lp:~michael.nelson/ubuntu-webcatalog/962140-display-video-demos
Reviewer Review Type Date Requested Status
Anthony Lenton (community) Approve
Review via email: mp+104402@code.launchpad.net

Commit message

Import video urls during import_for_purchase_apps.

Description of the change

Overview
========

Just a prep-branch for bug 962140 that:

* ensures we import the video urls when importing for-purchase apps
* removes unnecessary list serialise/unserialise and simplifies importer code by putting responsibility for saving media in the form.save() itself (matching the distro import command)

`fab test`

Tomorrow's first branch will simply use the imported data to display the iframe.

To post a comment you must log in.
124. By Michael Nelson

REFACTOR: Added commit kwargs back to form.save() methods.

Revision history for this message
Anthony Lenton (elachuni) wrote :

Looks fine!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/webcatalog/forms.py'
2--- src/webcatalog/forms.py 2012-04-20 06:41:56 +0000
3+++ src/webcatalog/forms.py 2012-05-03 12:42:19 +0000
4@@ -96,12 +96,10 @@
5
6 return cleaned_data
7
8- def save(self):
9- # We don't provide a commit=False option as we have
10- # to commit to be able to add the screenshots.
11- app = super(ApplicationForm, self).save()
12+ def save(self, commit=True):
13+ app = super(ApplicationForm, self).save(commit=commit)
14 screenshot_url = self.cleaned_data['screenshot_url']
15- if screenshot_url:
16+ if commit and screenshot_url:
17 if screenshot_url not in app.screenshots:
18 app.applicationmedia_set.create(
19 media_type='screenshot', url=screenshot_url)
20@@ -114,7 +112,7 @@
21 # Return an empty list if no input was given.
22 if not value:
23 return []
24- return value.split(',')
25+ return value
26
27 def validate(self, value):
28 "Check if value consists only of valid urls."
29@@ -128,20 +126,21 @@
30
31 class ForPurchaseApplicationForm(forms.ModelForm):
32 screenshot_urls = MultiURLField(required=False)
33+ video_embedded_html_urls = MultiURLField(required=False)
34
35 class Meta:
36 model = Application
37- exclude = ('distroseries', 'section', 'popcon')
38+ exclude = ('section', 'popcon')
39
40 @classmethod
41- def from_json(cls, app_data, distroseries):
42+ def from_api_data(cls, app_data, distroseries):
43 app_data = app_data.copy()
44+ app_data['distroseries'] = distroseries.id
45+ app_data['for_purchase'] = True
46 if 'description' in app_data:
47 tagline, _, description = app_data['description'].partition('\n')
48 app_data['comment'] = tagline
49 app_data['description'] = description
50- if 'screenshot_urls' in app_data:
51- app_data['screenshot_urls'] = ",".join(app_data['screenshot_urls'])
52 if 'debtags' in app_data and app_data['debtags']:
53 app_data['debtags'] = json.dumps([get_hw_short_description(x)
54 for x in app_data['debtags']])
55@@ -159,16 +158,20 @@
56 return apt.apt_pkg.upstream_version(value)
57
58 def save(self, commit=True):
59- app = super(ForPurchaseApplicationForm, self).save(commit)
60+ app = super(ForPurchaseApplicationForm, self).save(commit=commit)
61 if commit:
62- self.save_screenshot_urls()
63+ self.save_media_urls()
64 return app
65
66- def save_screenshot_urls(self):
67+ def save_media_urls(self):
68 for screenshot_url in self.cleaned_data['screenshot_urls']:
69 self.instance.applicationmedia_set.get_or_create(
70 url=screenshot_url, media_type='screenshot')
71
72+ for video_url in self.cleaned_data['video_embedded_html_urls']:
73+ self.instance.applicationmedia_set.get_or_create(
74+ url=video_url, media_type='video')
75+
76
77 class EmailDownloadLinkForm(forms.Form):
78 email = forms.EmailField()
79
80=== modified file 'src/webcatalog/management/commands/import_for_purchase_apps.py'
81--- src/webcatalog/management/commands/import_for_purchase_apps.py 2012-03-19 14:38:02 +0000
82+++ src/webcatalog/management/commands/import_for_purchase_apps.py 2012-05-03 12:42:19 +0000
83@@ -70,13 +70,10 @@
84 Application.objects.check_latest(app_data['package_name'])
85
86 def import_app_from_data(self, app_data, icon_data, distroseries):
87- form = ForPurchaseApplicationForm.from_json(app_data, distroseries)
88+ form = ForPurchaseApplicationForm.from_api_data(
89+ app_data, distroseries)
90 if form.is_valid():
91- app = form.save(commit=False)
92- app.distroseries = distroseries
93- app.for_purchase = True
94- app.save()
95- form.save_screenshot_urls()
96+ app = form.save()
97 app.update_departments()
98 self.add_icon_to_app(app, data=icon_data)
99 self.output(u"{0} created.\n".format(app.name).encode('utf-8'), 1)
100
101=== modified file 'src/webcatalog/tests/test_forms.py'
102--- src/webcatalog/tests/test_forms.py 2012-04-20 06:41:56 +0000
103+++ src/webcatalog/tests/test_forms.py 2012-05-03 12:42:19 +0000
104@@ -22,12 +22,16 @@
105 with_statement,
106 )
107
108+from django.forms import ValidationError
109+from django.test import TestCase
110+
111 from textwrap import dedent
112
113 from webcatalog.forms import (
114 ApplicationForm,
115 desktop_field_mappings,
116 ForPurchaseApplicationForm,
117+ MultiURLField,
118 )
119 from webcatalog.models import Application, ApplicationMedia
120 from webcatalog.tests.factory import TestCaseWithFactory
121@@ -36,6 +40,7 @@
122 __all__ = [
123 'ApplicationFormTestCase',
124 'ForPurchaseApplicationFormTestCase',
125+ 'MultiURLFieldTestCase',
126 ]
127
128
129@@ -169,10 +174,64 @@
130
131 class ForPurchaseApplicationFormTestCase(TestCaseWithFactory):
132
133- def test_from_json_shortens_debtag(self):
134+ def test_from_api_data_shortens_debtag(self):
135 app = self.factory.make_application()
136 data = {'debtags': ['hardware::storage:cd-writer']}
137
138- form = ForPurchaseApplicationForm.from_json(data, app.distroseries)
139+ form = ForPurchaseApplicationForm.from_api_data(data, app.distroseries)
140
141 self.assertEqual(form.data['debtags'], '["CD burner"]')
142+
143+ def test_save_media_urls(self):
144+ app = self.factory.make_application(
145+ screenshot_url="http://example.com/screenshot1.png")
146+ self.assertEqual(1, app.applicationmedia_set.count())
147+ data = {
148+ 'screenshot_urls': [
149+ 'http://example.com/screenshot1.png',
150+ 'http://example.com/screenshot2.png',
151+ ],
152+ 'video_embedded_html_urls': [
153+ 'http://example.com/video1.mp4',
154+ ],
155+ 'archive_id': app.archive_id,
156+ 'package_name': app.package_name,
157+ 'name': app.name,
158+ }
159+
160+ form = ForPurchaseApplicationForm.from_api_data(data,
161+ app.distroseries)
162+ self.assertTrue(form.is_valid())
163+ form.save_media_urls()
164+ self.assertEqual(3, app.applicationmedia_set.count())
165+ actual_urls = [media.url for media in app.applicationmedia_set.all()]
166+ self.assertEqual([
167+ 'http://example.com/screenshot1.png',
168+ 'http://example.com/screenshot2.png',
169+ 'http://example.com/video1.mp4',
170+ ], actual_urls)
171+
172+
173+class MultiURLFieldTestCase(TestCase):
174+
175+ def test_normalize_empty_value(self):
176+ self.assertEqual(
177+ [], MultiURLField().to_python(''))
178+ self.assertEqual(
179+ [], MultiURLField().to_python(None))
180+ self.assertEqual(
181+ [], MultiURLField().to_python([]))
182+
183+ def test_normalize_expects_list_strings(self):
184+ self.assertEqual(
185+ ['foo', 'bar'],
186+ MultiURLField().to_python(['foo', 'bar']))
187+
188+ def test_validates_multiple_URLs(self):
189+ MultiURLField().validate(
190+ ['http://example.com', 'https://example.com/two'])
191+
192+ self.assertRaises(
193+ ValidationError,
194+ MultiURLField().validate,
195+ ['httpd://example.com', 'https://example.com/two'])

Subscribers

People subscribed via source and target branches