Merge lp:~daker/ubuntu-api-website/fix.1275721 into lp:ubuntu-api-website

Proposed by Adnane Belmadiaf on 2014-02-15
Status: Merged
Merged at revision: 56
Proposed branch: lp:~daker/ubuntu-api-website/fix.1275721
Merge into: lp:ubuntu-api-website
Diff against target: 478 lines (+151/-124)
3 files modified
developer_network/apidocs/models.py (+34/-35)
developer_network/web/forms.py (+85/-0)
developer_network/web/views.py (+32/-89)
To merge this branch: bzr merge lp:~daker/ubuntu-api-website/fix.1275721
Reviewer Review Type Date Requested Status
Ubuntu API Website Developers 2014-02-15 Pending
Review via email: mp+206594@code.launchpad.net

Commit message

Code adjustments

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'developer_network/apidocs/models.py'
2--- developer_network/apidocs/models.py 2013-12-19 21:14:27 +0000
3+++ developer_network/apidocs/models.py 2014-02-15 20:59:13 +0000
4@@ -1,64 +1,63 @@
5+# -*- coding: utf-8 -*-
6+
7 from django.db import models
8-from common.models import Topic, Version, Section
9-# Create your models here.
10+from common.models import Section
11
12 class Namespace(models.Model):
13-
14- class Meta:
15- ordering = ('name',)
16-
17+
18 platform_section = models.ForeignKey(Section)
19 name = models.CharField(max_length=64)
20 display_name = models.CharField(max_length=64, blank=True, default='')
21-
22+
23+ class Meta:
24+ ordering = ('name',)
25+
26 def __unicode__(self):
27- return self.name
28+ return u'%s' % self.name
29
30
31 class Element(models.Model):
32 'Displayable API Element'
33-
34+
35+ name = models.CharField(max_length=64)
36+ description= models.CharField(max_length=256, blank=True, default='')
37+ namespace = models.ForeignKey(Namespace, blank=True, null=True)
38+
39+ section = models.ForeignKey(Section)
40+ fullname = models.CharField(max_length=128)
41+ keywords = models.CharField(max_length=256, blank=True, default='')
42+ data = models.TextField(blank=True, default='')
43+
44 class Meta:
45 verbose_name = "Rendered Element"
46 verbose_name_plural = "Rendered Elements"
47 ordering = ('name',)
48-
49- name = models.CharField(max_length=64)
50- description= models.CharField(max_length=256, blank=True, default='')
51- namespace = models.ForeignKey(Namespace, blank=True, null=True)
52-
53- section = models.ForeignKey(Section)
54- fullname = models.CharField(max_length=128)
55- keywords = models.CharField(max_length=256, blank=True, default='')
56- data = models.TextField(blank=True, default='')
57+
58+ def __unicode__(self):
59+ return u'%s' % self.fullname
60
61 @property
62 def platform_section(self):
63 return self.section
64-
65- def __unicode__(self):
66- return self.fullname
67
68
69 class Page(models.Model):
70 'Displayable Page of non-Element content'
71-
72+
73+ slug = models.CharField(max_length=64)
74+ title = models.CharField(max_length=64)
75+ description= models.CharField(max_length=256, blank=True, default='')
76+ namespace = models.ForeignKey(Namespace, blank=True, null=True)
77+
78+ section = models.ForeignKey(Section)
79+ fullname = models.CharField(max_length=128)
80+ keywords = models.CharField(max_length=256, blank=True, default='')
81+ data = models.TextField(blank=True, default='')
82+
83 class Meta:
84 verbose_name = "Rendered Page"
85 verbose_name_plural = "Rendered Pages"
86 ordering = ('slug',)
87-
88- slug = models.CharField(max_length=64)
89- title = models.CharField(max_length=64)
90- description= models.CharField(max_length=256, blank=True, default='')
91- namespace = models.ForeignKey(Namespace, blank=True, null=True)
92-
93- section = models.ForeignKey(Section)
94- fullname = models.CharField(max_length=128)
95- keywords = models.CharField(max_length=256, blank=True, default='')
96- data = models.TextField(blank=True, default='')
97
98 def __unicode__(self):
99- return self.fullname
100-
101-
102+ return u'%s' % self.fullname
103
104=== added file 'developer_network/web/forms.py'
105--- developer_network/web/forms.py 1970-01-01 00:00:00 +0000
106+++ developer_network/web/forms.py 2014-02-15 20:59:13 +0000
107@@ -0,0 +1,85 @@
108+# -*- coding: utf-8 -*-
109+
110+from django.forms import ModelForm
111+from django.contrib.auth.models import User
112+
113+from common.models import Topic, Version, Section
114+from apidocs.models import Namespace, Element, Page
115+from rest_framework.authtoken.models import Token
116+
117+
118+class TopicForm(ModelForm):
119+ class Meta:
120+ model = Topic
121+
122+
123+class VersionForm(ModelForm):
124+ class Meta:
125+ model = Version
126+
127+
128+class SectionForm(ModelForm):
129+ class Meta:
130+ model = Section
131+
132+
133+class NamespaceForm(ModelForm):
134+ class Meta:
135+ model = Namespace
136+
137+ def __init__(self, *args, **kargs):
138+ if 'version' in kargs:
139+ version = kargs['version']
140+ del kargs['version']
141+ elif hasattr(self, 'instance') and self.instance.section:
142+ version = self.instance.section.topic_version
143+ else:
144+ version=None
145+ super(NamespaceForm, self).__init__(*args, **kargs)
146+ self.fields['platform_section'].queryset = Section.objects.filter(topic_version=version)
147+
148+
149+class PageForm(ModelForm):
150+ class Meta:
151+ model = Page
152+
153+ def __init__(self, *args, **kargs):
154+ if 'version' in kargs:
155+ version = kargs['version']
156+ del kargs['version']
157+ elif hasattr(self, 'instance') and self.instance.section:
158+ version = self.instance.section.topic_version
159+ else:
160+ version=None
161+ super(PageForm, self).__init__(*args, **kargs)
162+ self.fields['section'].queryset = Section.objects.filter(topic_version=version)
163+ self.fields['namespace'].queryset = Namespace.objects.filter(platform_section__topic_version=version)
164+
165+
166+class ElementForm(ModelForm):
167+ class Meta:
168+ model = Element
169+
170+ def __init__(self, *args, **kargs):
171+ if 'version' in kargs:
172+ version = kargs['version']
173+ del kargs['version']
174+ elif hasattr(self, 'instance') and self.instance.section:
175+ version = self.instance.section.topic_version
176+ else:
177+ version=None
178+ super(ElementForm, self).__init__(*args, **kargs)
179+ self.fields['section'].queryset = Section.objects.filter(topic_version=version)
180+ self.fields['namespace'].queryset = Namespace.objects.filter(platform_section__topic_version=version)
181+
182+
183+class TokenForm(ModelForm):
184+ class Meta:
185+ model = Token
186+ fields = ('user',)
187+
188+
189+class UserForm(ModelForm):
190+ class Meta:
191+ model = User
192+ fields = ('username',)
193
194=== modified file 'developer_network/web/views.py'
195--- developer_network/web/views.py 2014-02-07 18:39:18 +0000
196+++ developer_network/web/views.py 2014-02-15 20:59:13 +0000
197@@ -1,8 +1,9 @@
198+# -*- coding: utf-8 -*-
199+
200 from django.shortcuts import render_to_response, get_object_or_404
201-from django.http import HttpResponse, HttpResponseRedirect
202+from django.http import HttpResponseRedirect
203 from django.core.urlresolvers import reverse
204-from django.template import loader, RequestContext
205-from django.forms import ModelForm
206+from django.template import RequestContext
207 from django.contrib.auth.models import User, Group
208
209 from common.models import Topic, Version, Section
210@@ -10,26 +11,25 @@
211 from related.models import Snippet, Link, LINK_QUESTION, LINK_TUTORIAL
212 from rest_framework.authtoken.models import Token
213
214-# Create your views here.
215+from forms import *
216+
217 def overview(request):
218 topics = Topic.objects.all()
219-
220+
221 context = {
222 'topics': topics,
223 }
224 return render_to_response('web/overview.html', context, RequestContext(request))
225-
226+
227+
228 def topic_view(request, topic_name):
229 topic = get_object_or_404(Topic, slug=topic_name)
230-
231+
232 context = {
233 'topic': topic,
234 }
235 return render_to_response('web/distro.html', context, RequestContext(request))
236
237-class TopicForm(ModelForm):
238- class Meta:
239- model = Topic
240
241 def topic_edit(request, topic_id=0):
242 if topic_id:
243@@ -53,6 +53,7 @@
244 }
245 return render_to_response('web/topic_edit.html', context, RequestContext(request))
246
247+
248 def version_view(request, topic_name, release_version):
249 version = get_object_or_404(Version, topic__slug=topic_name, slug=release_version)
250
251@@ -74,7 +75,7 @@
252 else:
253 break
254 sorted_sections.insert(i, section)
255-
256+
257 first_column_size = 0
258 for section in sorted_sections:
259 section_size = section.namespace_set.count() + section.free_element_set().count() + 2 # Extra 2 for the section header
260@@ -93,9 +94,6 @@
261 }
262 return render_to_response('web/release.html', context, RequestContext(request))
263
264-class VersionForm(ModelForm):
265- class Meta:
266- model = Version
267
268 def version_edit(request, topic_name, version_id=0):
269 if version_id:
270@@ -122,9 +120,6 @@
271 }
272 return render_to_response('web/version_edit.html', context, RequestContext(request))
273
274-class SectionForm(ModelForm):
275- class Meta:
276- model = Section
277
278 def section_edit(request, topic_name, version_name, section_id=0):
279 if section_id:
280@@ -153,14 +148,14 @@
281 'name': section.name,
282 }
283 return render_to_response('web/delete_confirm.html', context, RequestContext(request))
284-
285+
286 elif request.GET.get('action', None) == 'delete_cancel':
287 return HttpResponseRedirect(reverse(version_view, args=[topic_name, version_name]))
288-
289+
290 elif request.GET.get('action', None) == 'delete_confirm':
291 section.delete()
292 return HttpResponseRedirect(reverse(version_view, args=[topic_name, version_name]))
293-
294+
295 else:
296 form = SectionForm(instance=section)
297 context = {
298@@ -182,7 +177,7 @@
299 links = Link.objects.filter(rendered_element=namespace)
300 questions = [link for link in links if link.link_type is LINK_QUESTION]
301 tutorials = [link for link in links if link.link_type is LINK_TUTORIAL]
302-
303+
304 context = {
305 'sidenav': topic_name,
306 'topic': namespace.platform_section.topic_version.topic,
307@@ -193,21 +188,7 @@
308 }
309 return render_to_response('web/namespace.html', context, RequestContext(request))
310
311-class NamespaceForm(ModelForm):
312- class Meta:
313- model = Namespace
314-
315- def __init__(self, *args, **kargs):
316- if 'version' in kargs:
317- version = kargs['version']
318- del kargs['version']
319- elif hasattr(self, 'instance') and self.instance.section:
320- version = self.instance.section.topic_version
321- else:
322- version=None
323- super(NamespaceForm, self).__init__(*args, **kargs)
324- self.fields['platform_section'].queryset = Section.objects.filter(topic_version=version)
325-
326+
327 def namespace_edit(request, topic_name, version_name, namespace_id=0):
328 if namespace_id:
329 namespace = get_object_or_404(Namespace, platform_section__topic_version__slug=version_name, platform_section__topic_version__topic__slug=topic_name, id=namespace_id)
330@@ -235,14 +216,14 @@
331 'type': 'namespace',
332 }
333 return render_to_response('web/delete_confirm.html', context, RequestContext(request))
334-
335+
336 elif request.GET.get('action', None) == 'delete_cancel':
337 return HttpResponseRedirect(reverse(element_view, args=[topic_name, version_name, namespace.name]))
338-
339+
340 elif request.GET.get('action', None) == 'delete_confirm':
341 namespace.delete()
342 return HttpResponseRedirect(reverse(version_view, args=[topic_name, version_name]))
343-
344+
345 else:
346 form = NamespaceForm(instance=namespace, version=version)
347 context = {
348@@ -260,7 +241,7 @@
349 links = Link.objects.filter(rendered_element=page)
350 questions = [link for link in links if link.link_type is LINK_QUESTION]
351 tutorials = [link for link in links if link.link_type is LINK_TUTORIAL]
352-
353+
354 context = {
355 'sidenav': topic_name,
356 'topic': page.section.topic_version.topic,
357@@ -271,22 +252,7 @@
358 }
359 return render_to_response('web/page.html', context, RequestContext(request))
360
361-class PageForm(ModelForm):
362- class Meta:
363- model = Page
364-
365- def __init__(self, *args, **kargs):
366- if 'version' in kargs:
367- version = kargs['version']
368- del kargs['version']
369- elif hasattr(self, 'instance') and self.instance.section:
370- version = self.instance.section.topic_version
371- else:
372- version=None
373- super(PageForm, self).__init__(*args, **kargs)
374- self.fields['section'].queryset = Section.objects.filter(topic_version=version)
375- self.fields['namespace'].queryset = Namespace.objects.filter(platform_section__topic_version=version)
376-
377+
378 def page_edit(request, topic_name, release_version, page_id=0):
379 if page_id:
380 page = get_object_or_404(Page, section__topic_version__slug=release_version, section__topic_version__topic__slug=topic_name, id=page_id)
381@@ -315,7 +281,8 @@
382 'page': page,
383 }
384 return render_to_response('web/page_edit.html', context, RequestContext(request))
385-
386+
387+
388 def element_view(request, topic_name, release_version, element_fullname):
389 try:
390 element = Element.objects.get(section__topic_version__slug=release_version, section__topic_version__topic__slug=topic_name, fullname=element_fullname)
391@@ -328,7 +295,7 @@
392
393 questions = [link for link in links if link.link_type is LINK_QUESTION]
394 tutorials = [link for link in links if link.link_type is LINK_TUTORIAL]
395-
396+
397 context = {
398 'sidenav': topic_name,
399 'topic': element.section.topic_version.topic,
400@@ -340,22 +307,7 @@
401 }
402 return render_to_response('web/element.html', context, RequestContext(request))
403
404-class ElementForm(ModelForm):
405- class Meta:
406- model = Element
407-
408- def __init__(self, *args, **kargs):
409- if 'version' in kargs:
410- version = kargs['version']
411- del kargs['version']
412- elif hasattr(self, 'instance') and self.instance.section:
413- version = self.instance.section.topic_version
414- else:
415- version=None
416- super(ElementForm, self).__init__(*args, **kargs)
417- self.fields['section'].queryset = Section.objects.filter(topic_version=version)
418- self.fields['namespace'].queryset = Namespace.objects.filter(platform_section__topic_version=version)
419-
420+
421 def element_edit(request, topic_name, release_version, element_id=0):
422 if element_id:
423 element = get_object_or_404(Element, section__topic_version__slug=release_version, section__topic_version__topic__slug=topic_name, id=element_id)
424@@ -382,14 +334,14 @@
425 'element': element,
426 }
427 return render_to_response('web/delete_confirm.html', context, RequestContext(request))
428-
429+
430 elif request.GET.get('action', None) == 'delete_cancel':
431 return HttpResponseRedirect(reverse(element_view, args=[topic_name, release_version, element.fullname]))
432-
433+
434 elif request.GET.get('action', None) == 'delete_confirm':
435 element.delete()
436 return HttpResponseRedirect(reverse(version_view, args=[topic_name, release_version]))
437-
438+
439 else:
440 form = ElementForm(instance=element, version=version)
441 context = {
442@@ -400,12 +352,13 @@
443 'element': element,
444 }
445 return render_to_response('web/element_edit.html', context, RequestContext(request))
446-
447+
448+
449 def search(request, topic_name, release_version):
450 version = get_object_or_404(Version, topic__slug=topic_name, slug=release_version)
451 query = request.GET.get('query', '')
452 results = Element.objects.filter(section__topic_version=version, name__icontains=query).order_by('section', 'fullname')
453-
454+
455 context = {
456 'sidenav': topic_name,
457 'topic': version.topic,
458@@ -415,15 +368,6 @@
459 }
460 return render_to_response('web/search.html', context, RequestContext(request))
461
462-class TokenForm(ModelForm):
463- class Meta:
464- model = Token
465- fields = ('user',)
466-
467-class UserForm(ModelForm):
468- class Meta:
469- model = User
470- fields = ('username',)
471
472 def token_edit(request, token_key=None):
473 if token_key:
474@@ -468,4 +412,3 @@
475 'tokens': tokens,
476 }
477 return render_to_response('web/token_edit.html', context, RequestContext(request))
478-

Subscribers

People subscribed via source and target branches