def formrowedit(request, formid, rowid): """Generate a form to edit rowid in formid""" rowid = int(rowid) + HEADERS_ROW # get the form object try: form = Form.objects.get(formid=formid) except Form.DoesNotExist: raise Http404 # get the seafform object (Seafile connexion) if settings.LOCAL: seaf = None else: seafu = form.owner.seafileuser seaf = Seafile(seafu.seafroot) seaf.authenticate(form.owner.email, token=seafu.seaftoken, validate=False) seafform = SeafForm(form.filepath, seaf, form.repoid) seafform.load() # create the django form for a specific row initials = seafform.get_values_from_data(rowid) initials['rowid'] = rowid djform = DjForm( initials, fieldlist=seafform.fields ) return render(request, 'seafform/rowedit.html', { 'seafform': seafform, 'djform': djform, 'modelform': form, 'rowid': rowid - HEADERS_ROW, })
def formrowedit(request, formid, rowid): """Generate a form to edit rowid in formid""" rowid = int(rowid) + HEADERS_ROW # get the form object try: form = Form.objects.get(formid=formid) except Form.DoesNotExist: raise Http404 # get the seafform object (Seafile connexion) if settings.LOCAL: seaf = None else: seafu = form.owner.seafileuser seaf = Seafile(seafu.seafroot, verifycerts=settings.VERIFYCERTS) seaf.authenticate(form.owner.email, token=seafu.seaftoken, validate=False) seafform = SeafForm(form.filepath, seaf, form.repoid) seafform.load() # create the django form for a specific row initials = seafform.get_values_from_data(rowid) initials['rowid'] = rowid djform = DjForm( initials, fieldlist=seafform.fields ) return render(request, 'seafform/rowedit.html', { 'seafform': seafform, 'djform': djform, 'modelform': form, 'rowid': rowid - HEADERS_ROW, })
def new(request): """Create a new form""" # if this is a POST request we need to process the form data if request.method == 'POST': path = unquote(request.POST.get('path', '')) print('new/path=' + path) if path.endswith('.ods'): if settings.LOCAL: filepath = os.path.join(settings.LOCAL_ROOT, path.lstrip('/')) seaf = None repoid = 'LOCAL' reponame = 'LOCAL' else: # Connect to Seafile seafu = request.user.seafileuser seaf = Seafile(seafu.seafroot, verifycerts=settings.VERIFYCERTS) seaf.authenticate(request.user.email, token=seafu.seaftoken, validate=False) # retreive path info parsed_path = parse(path) filepath = parsed_path['path'] repoid = repo_id_from_name(seaf, parsed_path['repo_name']) reponame = parsed_path['repo_name'] # load the form seafform = SeafForm(filepath, seaf, repoid) seafform.load() # create the slug/formid max_length = Form._meta.get_field('formid').max_length formid = orig = slugify(seafform.title)[:max_length] for x in itertools.count(1): if not Form.objects.filter(formid=formid).exists(): break # Truncate the original slug dynamically. Minus 1 for the hyphen. formid = "%s-%d" % (orig[:max_length - len(str(x)) - 1], x) # add the new form newform = Form( owner = request.user, filepath = filepath, repoid = repoid, reponame = reponame, formid = formid, title = seafform.title, creation_datetime = timezone.now(), description = seafform.description, public = seafform.public, ) newform.save() # Redirect + message return HttpResponseRedirect(reverse('private') + '?newform=' + formid) return render(request, 'seafform/new.html', { 'user': request.user, 'allow_public': settings.ALLOW_PUBLIC, })
def formview(request, formid): """Display a public form""" justaddedrow = None # get the form object try: form = Form.objects.get(formid=formid) except Form.DoesNotExist: raise Http404 # get the seafform object (Seafile connexion) if settings.LOCAL: seaf = None else: seafu = form.owner.seafileuser seaf = Seafile(seafu.seafroot) seaf.authenticate(form.owner.email, token=seafu.seaftoken, validate=False) seafform = SeafForm(form.filepath, seaf, form.repoid) try: seafform.load() except APIError: raise Http404 # build the corresponding DjForm() if request.method == 'POST': results = False # results management djform = DjForm(request.POST, fieldlist=seafform.fields) if djform.is_valid(): # check if we replace a row if seafform.edit and djform.cleaned_data['rowid'] != 'newrow': replace_row = int(djform.cleaned_data['rowid']) justaddedrow = replace_row - HEADERS_ROW else: replace_row = None justaddedrow = seafform._first_empty_row - HEADERS_ROW # save data seafform.post(djform.cleaned_data, replace_row) # if valid form and form and not edit: if seafform.view_as == 'form' and not seafform.edit: # redirect to thanks return HttpResponseRedirect(reverse('thanks',args=(formid,))) elif seafform.view_as == 'form': return HttpResponseRedirect( reverse('form',args=(formid,)) + '?results' ) #results = True # redirect to table # clean fields djform = DjForm(fieldlist=seafform.fields) else: djform = DjForm(fieldlist=seafform.fields) results = ('results' in request.GET) if seafform.view_as == 'form' and not results: # form view return render(request, 'seafform/form_as_form.html', { 'seafform': seafform, 'modelform': form, 'djform': djform, }) elif seafform.view_as == 'table' or (results and seafform.edit): # hightlight first column if static field first_is_static = (seafform.fields[0].ident == 'static') # compute some results max_chk = 0 computations = [] for colid, field in enumerate(seafform.fields): # if check boxe, number of checks if field.ident.startswith('check'): res = sum(row[colid] for row in seafform.data if row[colid]) max_chk = max(max_chk, res) computations.append(res) # if number, the sum elif field.ident == 'number': computations.append(sum( row[colid] for row in seafform.data if row[colid] )) else: computations.append(None) # columns with a star max_chk_column = [] for colid, field in enumerate(seafform.fields): if field.ident.startswith('check') and computations[colid] == max_chk: max_chk_column.append(colid) # table view return render(request, 'seafform/form_as_table.html', { 'seafform': seafform, 'modelform': form, 'djform': djform, 'justaddedrow': justaddedrow, 'first_is_static': first_is_static, 'computations': computations, 'max_chk_column': max_chk_column, }) raise Http404
def formview(request, formid): """Display a public form""" justaddedrow = None # get the form object try: form = Form.objects.get(formid=formid) except Form.DoesNotExist: raise Http404 # get the seafform object (Seafile connexion) if settings.LOCAL: seaf = None else: seafu = form.owner.seafileuser seaf = Seafile(seafu.seafroot, verifycerts=settings.VERIFYCERTS) seaf.authenticate(form.owner.email, token=seafu.seaftoken, validate=False) seafform = SeafForm(form.filepath, seaf, form.repoid) try: seafform.load() except APIError: raise Http404 _update_form_attr(form, seafform) # build the corresponding DjForm() if request.method == 'POST': results = False # results management djform = DjForm(request.POST, fieldlist=seafform.fields) if djform.is_valid(): # check if we replace a row if seafform.edit and djform.cleaned_data['rowid'] != 'newrow': replace_row = int(djform.cleaned_data['rowid']) justaddedrow = replace_row - HEADERS_ROW else: replace_row = None justaddedrow = seafform._first_empty_row - HEADERS_ROW # save data seafform.post(djform.cleaned_data, replace_row) # if valid form and form and not edit: if seafform.view_as == 'form' and not seafform.edit: # redirect to thanks return HttpResponseRedirect(reverse('thanks',args=(formid,))) elif seafform.view_as == 'form': return HttpResponseRedirect( reverse('form',args=(formid,)) + '?results' ) #results = True # redirect to table # clean fields djform = DjForm(fieldlist=seafform.fields) else: djform = DjForm(fieldlist=seafform.fields) results = ('results' in request.GET) if seafform.view_as == 'form' and not results: # form view return render(request, 'seafform/form_as_form.html', { 'seafform': seafform, 'modelform': form, 'djform': djform, }) elif seafform.view_as == 'table' or (results and seafform.edit): # hightlight first column if static field first_is_static = (seafform.fields[0].ident == 'static') # compute some results max_chk = 0 computations = [] for colid, field in enumerate(seafform.fields): # if check boxe, number of checks if field.ident.startswith('check'): res = sum(row[colid] for row in seafform.data if row[colid]) max_chk = max(max_chk, res) computations.append(res) # if number, the sum elif field.ident == 'number': computations.append(sum( row[colid] for row in seafform.data if row[colid] )) else: computations.append(None) # columns with a star max_chk_column = [] for colid, field in enumerate(seafform.fields): if field.ident.startswith('check') and computations[colid] == max_chk: max_chk_column.append(colid) # table view return render(request, 'seafform/form_as_table.html', { 'seafform': seafform, 'modelform': form, 'djform': djform, 'justaddedrow': justaddedrow, 'first_is_static': first_is_static, 'computations': computations, 'max_chk_column': max_chk_column, }) raise Http404