def add_idea(request, banner_id=None): if 'Mobile' in request.META['HTTP_USER_AGENT']: mobile = True else: mobile = False if request.method == 'POST': matching_ideas = Idea.objects.filter( creator=request.user, title=request.POST.get('title', '')) if matching_ideas.count() > 0: # user already submitted this idea return HttpResponseRedirect(reverse('idea:idea_detail', args=(matching_ideas[0].id,))) idea = Idea(creator=request.user, state=state_helper.get_first_state()) if idea.state.name == 'Pending': form = IdeaForm(request.POST, instance=idea) if form.is_valid(): new_idea = form.save() vote_up(new_idea, request.user) send_mail('New Idea Pending', 'New idea pending! http://ec2-54-88-16-5.compute-1.amazonaws.com/idea/detail/' + str(new_idea.id), '*****@*****.**', User.objects.filter(groups__name='Approvers').values_list('email',flat=True), fail_silently=True) devices = GCMDevice.objects.filter(user__groups__name='Approvers') devices.send_message("New idea pending!", extra={"id": new_idea.id}) devices = APNSDevice.objects.filter(user__groups__name='Approvers') devices.send_message(None, sound="", content_available=True, extra={"id": new_idea.id, "msg": "New idea pending!"}) if 'Mobile' in request.META['HTTP_USER_AGENT']: mobile = True else: mobile = False return _render(request, 'idea/add_success.html', {'idea': new_idea, 'mobile':mobile, }) else: if 'banner' in request.POST: form.fields["banner"].queryset = get_current_banners() else: form.fields.pop('banner') form.fields.pop('challenge-checkbox') form.set_error_css() return _render(request, 'idea/add.html', {'form': form, 'mobile':mobile, }) else: return HttpResponse('Idea is archived', status=403) else: idea_title = request.GET.get('idea_title', '') current_banners = get_current_banners() if current_banners.count() == 0: form = IdeaForm(initial={'title': idea_title}) form.fields.pop('banner') form.fields.pop('challenge-checkbox') else: if banner_id and Banner.objects.get(id=banner_id) in get_current_banners(): banner = Banner.objects.get(id=banner_id) else: banner = None form = IdeaForm(initial={'title': idea_title, 'banner': banner}) form.fields["banner"].queryset = current_banners return _render(request, 'idea/add.html', { 'form': form, 'mobile':mobile, #, #'similar': [r.object for r in more_like_text(idea_title, Idea)] })
def test_idea_all_fields(self): f = IdeaForm({ 'title':'Create an automatic birthday greeting emailer.', 'summary': 'test summary', 'text': 'test text', 'tags': 'test, tags', }) self.assertEqual(f.is_valid(), True)
def test_idea_tags_required(self): """ The field 'text' is option when adding an idea. """ f = IdeaForm({ 'title':'Create an automatic birthday greeting emailer.', 'summary': 'test summary', 'text': 'test text', }) self.assertEqual(f.is_valid(), False)
def test_idea_title_required(self): """ The field 'title' is required when adding an idea. """ f = IdeaForm({ 'summary': 'test summary', 'text': 'test text', 'tags': 'test, tags', }) self.assertEqual(f.is_valid(), False)
def add_idea(request, banner_id=None): if request.method == 'POST': matching_ideas = Idea.objects.filter( creator=request.user, title=request.POST.get('title', '')) if matching_ideas.count() > 0: # user already submitted this idea return HttpResponseRedirect(reverse('idea:idea_detail', args=(matching_ideas[0].id,))) idea = Idea(creator=request.user, state=state_helper.get_first_state()) if idea.state.name == 'Active': form = IdeaForm(request.POST, instance=idea) if form.is_valid(): new_idea = form.save() vote_up(new_idea, request.user) return _render(request, 'idea/add_success.html', {'idea': new_idea, }) else: if 'banner' in request.POST: form.fields["banner"].queryset = get_current_banners() else: form.fields.pop('banner') form.fields.pop('challenge-checkbox') form.set_error_css() return _render(request, 'idea/add.html', {'form': form, }) else: return HttpResponse('Idea is archived', status=403) else: idea_title = request.GET.get('idea_title', '') current_banners = get_current_banners() if current_banners.count() == 0: form = IdeaForm(initial={'title': idea_title}) form.fields.pop('banner') form.fields.pop('challenge-checkbox') else: if banner_id and Banner.objects.get(id=banner_id) in get_current_banners(): banner = Banner.objects.get(id=banner_id) else: banner = None form = IdeaForm(initial={'title': idea_title, 'banner': banner}) form.fields["banner"].queryset = current_banners return _render(request, 'idea/add.html', { 'form': form, 'similar': [r.object for r in more_like_text(idea_title, Idea)] })
def edit_idea(request, idea_id): idea = get_object_or_404(Idea, pk=int(idea_id)) original_banner = idea.banner if idea.creator != request.user: return HttpResponseRedirect(reverse('idea:idea_detail', args=(idea_id,))) if request.method == 'POST': form = IdeaForm(request.POST, instance=idea) form.fields.pop('tags') if form.is_valid(): form.save() return HttpResponseRedirect(reverse('idea:idea_detail', args=(idea_id,))) else: if 'banner' in request.POST: if original_banner: current_banners = get_current_banners([original_banner.id]) else: current_banners = get_current_banners() form.fields["banner"].queryset = current_banners else: form.fields.pop('banner') form.fields.pop('challenge-checkbox') form.set_error_css() return _render(request, 'idea/edit.html', {'form': form, 'idea': idea }) else: form_initial = {} if original_banner: current_banners = get_current_banners([original_banner.id]) form_initial["challenge-checkbox"] = "on" else: current_banners = get_current_banners() form = IdeaForm(instance=idea, initial=form_initial) form.fields.pop('tags') if len(current_banners) == 0: form.fields.pop('banner') form.fields.pop('challenge-checkbox') else: form.fields["banner"].queryset = current_banners return _render(request, 'idea/edit.html', {'form': form, 'idea': idea })
def post(self, request, idea_id=None): form = IdeaForm(request.POST) if not form.is_valid(): return render(request, 'idea/form.html', {'form': form}) if idea_id: # Update the existing idea with data from the form idea = get_object_or_404(Idea, pk=idea_id) idea.updated_date = timezone.now() else: # Create a new idea with data from the form and default values idea = Idea( created_date=timezone.now(), user_id=self.request.user.id, votes=0 ) idea.title = form.cleaned_data['title'] idea.description = form.cleaned_data['description'] idea.category_id = form.cleaned_data['categories'].id # Setting the excerpt excerpt = form.cleaned_data['excerpt'] if not excerpt: # Max is 200, we remove 3 char for '...' MAX = 197 stripped_description = strip_tags(idea.description) stripped_description = stripped_description.replace(' ', ' ') length = len(stripped_description) if length > MAX: excerpt = stripped_description[:MAX] + '...' else: excerpt = stripped_description idea.excerpt = excerpt idea.save() return HttpResponseRedirect('/ideas/idea/' + str(idea.id))
def post(self, request, idea_id=None): form = IdeaForm(request.POST) if not form.is_valid(): return render(request, 'idea/form.html', {'form': form}) if idea_id: # Update the existing idea with data from the form idea = get_object_or_404(Idea, pk=idea_id) idea.updated_date = timezone.now() else: # Create a new idea with data from the form and default values idea = Idea(created_date=timezone.now(), user_id=self.request.user.id, votes=0) idea.title = form.cleaned_data['title'] idea.description = form.cleaned_data['description'] idea.category_id = form.cleaned_data['categories'].id # Setting the excerpt excerpt = form.cleaned_data['excerpt'] if not excerpt: # Max is 200, we remove 3 char for '...' MAX = 197 stripped_description = strip_tags(idea.description) stripped_description = stripped_description.replace(' ', ' ') length = len(stripped_description) if length > MAX: excerpt = stripped_description[:MAX] + '...' else: excerpt = stripped_description idea.excerpt = excerpt idea.save() return HttpResponseRedirect('/ideas/idea/' + str(idea.id))
def add_idea(request): banner = get_banner() if request.method == 'POST': idea = Idea(creator=request.user, state=state_helper.get_first_state()) if idea.state.name == 'Active': form = IdeaForm(request.POST, instance=idea) if form.is_valid(): new_idea = form.save() vote_up(new_idea, request.user) # Make sure the search index included the tags site.get_index(Idea).update_object(new_idea) return HttpResponseRedirect(reverse('idea_detail', args=(idea.id,))) else: return HttpResponse('Idea is archived', status=403) else: idea_title = request.GET.get('idea_title', '') form = IdeaForm(initial={'title':idea_title}) return _render(request, 'idea/add.html', { 'form':form, 'banner':banner, 'similar': [r.object for r in more_like_text(idea_title, Idea)] })
def test_idea_title_required(self): """ The field 'title' is required when adding an idea. """ f = IdeaForm({'text':'Every employee should get 4 weeks of vacation.'}) self.assertEqual(f.is_valid(), False)
def test_idea_text_optional(self): """ The field 'text' is option when adding an idea. """ f = IdeaForm({'title':'Create an automatic birthday greeting emailer.'}) self.assertEqual(f.is_valid(), True)