|
4 | 4 | from django.db.models import Q |
5 | 5 | from django.shortcuts import render |
6 | 6 | from django.views.generic import TemplateView |
| 7 | + |
7 | 8 | from taggie.parser import generate_tags |
8 | 9 | from .models import Tag, Tutorial |
9 | 10 |
|
@@ -119,29 +120,41 @@ def get(self, request): |
119 | 120 |
|
120 | 121 | def post(self, request): |
121 | 122 | """POST a tutorial""" |
122 | | - link_count = Tutorial.objects.filter( |
123 | | - link=request.POST['tlink']).count() |
124 | | - if link_count == 0: |
125 | | - tags, title = generate_tags(request.POST['tlink']) |
| 123 | + link = request.POST['tlink'] |
| 124 | + category = request.POST['tcategory'] |
| 125 | + result = handle_tutorial_post_request(link, category) |
| 126 | + if result is not None: |
126 | 127 | self.context['error'] = 'Not a Tutorial Link, Try Again' |
127 | | - if 'other' in tags: |
| 128 | + if result: |
128 | 129 | return render( |
129 | 130 | request, |
130 | 131 | 'contribute.html', |
131 | 132 | self.context |
132 | 133 | ) |
133 | 134 | else: |
134 | | - tutorial_object = Tutorial.objects.create( |
135 | | - title=title, |
136 | | - link=request.POST['tlink'], |
137 | | - category=request.POST['tcategory'] |
138 | | - ) |
139 | | - for tag in tags: |
140 | | - obj, created = Tag.objects.get_or_create(name=tag) |
141 | | - |
142 | | - tag_obj_list = Tag.objects.filter(name__in=tags) |
143 | | - tutorial_object.tags.set(tag_obj_list) |
144 | | - # thankyou.html shouldn't be accessible unless someone successfully posts |
145 | | - # a tutorial |
| 135 | + # thankyou.html shouldn't be accessible unless someone successfully posts |
| 136 | + # a tutorial |
146 | 137 | return render(request, 'thankyou.html', {'title': 'Thanks!'}) |
147 | 138 | return render(request, 'thankyou.html', {'title': 'Thanks!'}) |
| 139 | + |
| 140 | + |
| 141 | +def handle_tutorial_post_request(link, category): |
| 142 | + if Tutorial.objects.filter( |
| 143 | + link=link |
| 144 | + ).exists(): |
| 145 | + tags, title = generate_tags(link) |
| 146 | + if 'other' in tags: |
| 147 | + return True |
| 148 | + else: |
| 149 | + tutorial_object = Tutorial.objects.create( |
| 150 | + title=title, |
| 151 | + link=link, |
| 152 | + category=category |
| 153 | + ) |
| 154 | + tag_obj_list = [] |
| 155 | + for tag in tags: |
| 156 | + obj, created = Tag.objects.get_or_create(name=tag) |
| 157 | + tag_obj_list.append(obj) |
| 158 | + |
| 159 | + tutorial_object.tags.set(tag_obj_list) |
| 160 | + return False |
0 commit comments