Exemplo n.º 1
0
 def process_view(self, request):
     """
     Create a set of statements in bulk, based on CSV upload or URL lookup.
     """
     if request.POST.get('count', None):
         count = request.POST.get('count')
         if count.isdigit():
             count = int(count)
         else:
             return HttpResponseBadRequest("Count is not a number.")
         for i in range(1, (count + 1)):
             if request.POST.get("p%d" % i, None):
                 data = {
                     'text': request.POST.get("s%d" % i),
                     'tag': request.POST.get("t%d" % i)
                 }
                 preexisting_statements = []
                 statements_added_count = 0
                 form = StatementForm(data)
                 if form.is_valid():
                     form.save()
                     statements_added_count += 1
                 else:
                     if form.errors['text'] == \
                         [u'Statement with this Statement already exists.']:
                         # Statement already exists.
                         preexisting_statements.append(data['text'])
                     else:
                         # Some other form validation error (unlikely...)
                         err_string = ""
                         for field, errs in form.errors.items():
                             errs = ", ".join(errs)
                             err_string += "%s: %s ," % (field, errs)
                         return HttpResponseBadRequest(
                             "Validation error: %s" % err_string
                         )
         async_export_csv()
         if preexisting_statements:
             return simple.direct_to_template(request,
                 template = 'admin/statements/statement/import_errors.html',
                 extra_context = {
                     'title': 'Import Errors',
                     'errors': preexisting_statements,
                     'statements_added_count': statements_added_count
                 }
             )                
         url = reverse('admin:statements_statement_changelist')
         return simple.redirect_to(request, url)                      
Exemplo n.º 2
0
def add_statement(request):
    """A view that allows users add a statement."""    
    if request.method == 'POST':
        form = StatementFormWithCaptcha(request.POST)
        if form.is_valid():
            if form.cleaned_data['honeypot'] == '':
                statement = form.save()
                async_export_csv()
            return simple.redirect_to(request,
                url=reverse("statements:index"), 
                permanent=False
            )
    else:
        form = StatementFormWithCaptcha()
    return simple.direct_to_template(request,
        extra_context = {'form': form},
        template = 'statements/add_statement.html'
    )