def bulk_new(request, template_id=None): template = None if template_id: template = get_object_or_404(TaskBulkTemplate, id=template_id) edit = template is not None error = None if request.method == 'POST': if 'step' not in request.POST: return 400 form = TaskBulkTemplateForm(request.POST, instance=template, user=request.user) if form.is_valid(): step = request.POST['step'] jsons = [x.json for x in form.task_infos] if step == 'final' and request.POST.get('action') == 'create': try: tasks = create_tasks_from_json(jsons) except Exception as e: error = e.message if error is None: template = form.save(commit=False) template.author = request.user template.save() total = len(tasks) # return ('/task/new/bulk/success/?total=' + str(total), ) if step == 'second' and jsons: json_dump = json.dumps(jsons, indent=2, sort_keys=True) return ('task_bulk_new_2nd.html', { 'form': form, 'task_infos': form.task_infos, 'json_dump': json_dump, }) else: form = TaskBulkTemplateForm(instance=template, user=request.user) history = list(TaskBulkTemplate.objects.for_user(request.user, VIEW) \ .order_by('id').distinct()) history = [{ 'title': u"{} ({})".format(x.name, x.last_edit_date), 'content': x.source_code, } for x in history] return { 'error': error, 'form': form, 'task_infos': form.task_infos, 'history': history, }
def json_new(request): message = '' if request.method == 'POST': form = TaskJSONForm(request.POST) if form.is_valid(): description = json.loads(form.cleaned_data['description']) try: tasks = create_tasks_from_json(description) message = "Created {} task(s).".format(len(tasks)) except Exception as e: message = e.message else: form = TaskJSONForm() return {'form': form, 'message': message}
def json_new(request): message = "" if request.method == "POST": form = TaskJSONForm(request.POST) if form.is_valid(): description = json.loads(form.cleaned_data["description"]) try: tasks = create_tasks_from_json(description) message = "Created {} task(s).".format(len(tasks)) except Exception as e: message = e.message else: form = TaskJSONForm() return {"form": form, "message": message}
def bulk_new(request, template_id=None): template = None if template_id: template = get_object_or_404(TaskBulkTemplate, id=template_id) edit = template is not None error = None if request.method == "POST": if "step" not in request.POST: return 400 form = TaskBulkTemplateForm(request.POST, instance=template, user=request.user) if form.is_valid(): step = request.POST["step"] jsons = [x.json for x in form.task_infos] if step == "final" and request.POST.get("action") == "create": try: tasks = create_tasks_from_json(jsons) except Exception as e: error = e.message if error is None: template = form.save(commit=False) template.author = request.user template.save() total = len(tasks) # return ('/task/new/bulk/success/?total=' + str(total), ) if step == "second" and jsons: json_dump = json.dumps(jsons, indent=2, sort_keys=True) return ("task_bulk_new_2nd.html", {"form": form, "task_infos": form.task_infos, "json_dump": json_dump}) else: form = TaskBulkTemplateForm(instance=template, user=request.user) history = list(TaskBulkTemplate.objects.for_user(request.user, VIEW).order_by("id").distinct()) history = [{"title": u"{} ({})".format(x.name, x.last_edit_date), "content": x.source_code} for x in history] return {"error": error, "form": form, "task_infos": form.task_infos, "history": history}