def update_field(request, pk, field): """ This is function and not a CreateView/Update as it points to a template with less fields :param request: :param pk: primary key of Canvas :param field: which field to update :return: """ widgets = {} if field=='logo': widgets[field] = ClearableFileInput() else: widgets[field] = Textarea(attrs={'rows':4, 'cols':150}) CanvasForm = modelform_factory(Canvas, fields=(field,), widgets=widgets) if request.method == 'POST': form = CanvasForm(request.POST) if form.is_valid(): obj = Canvas.objects.get(id=pk) if obj.originalauthor == request.user: # more maintainable than dfunckt/django-rules if 'logo' in request.FILES: data = ContentFile(request.FILES['logo'].file.read()) filename = request.FILES['logo'].name form.cleaned_data['logo'] = filename default_storage.save(filename, data) setattr(obj, field, form.cleaned_data[field]) obj.save() return HttpResponseRedirect('/canvas/'+pk) else: return HttpResponseRedirect('/login/') else: form = CanvasForm(instance = Canvas.objects.get(id=pk)) return render(request, 'app/canvas_form.html', {'formfield': form})
def create_canvas(request): """ This is function and not a CreateView/Update as it points to a template with less fields :param request: :param pk: primary key of Canvas :param field: which field to update :return: """ if request.method == 'POST': form = CanvasForm(request.POST) if form.is_valid(): form.cleaned_data['originalauthor'] = request.user if 'logo' in request.FILES: data = ContentFile(request.FILES['logo'].file.read()) filename = request.FILES['logo'].name form.cleaned_data['logo'] = filename default_storage.save(filename, data) obj = Canvas.objects.create(**form.cleaned_data) return HttpResponseRedirect("/canvas/%d" % obj.id) else: form = CanvasForm() # blank form return render(request, 'app/canvas_new.html', {'company': form['company'],'description': form['description'],'logo': form['logo']})
def canvas(): form = CanvasForm() form2 = NoiseForm() # If post request with form1, tracks the star selected if form.validate_on_submit() and form.submit.data: x = form.data['x'] y = form.data['y'] # outFolder = form.data['outputFolder'] '''This accounts for cases where x and y are close to edges Note: Done, but can probably be made more pythonic. I have left it as is because stars are rarely, if ever, near edges of images TODO: Unhardcode the image size parameters here if I decide to improve this section TODO: Account for edges without reducing the template size (to avoid reducing accuracy)''' # This part makes the template bounds so that the star is in the center template_rect = {'x': x - 100, 'y': y - 100, 'h': 200, 'w': 200} if x < 100 or x > 1948: # I hard coded a width bound here, change in the future template_rect['x'] = x template_rect['w'] = 2 * x if x > 1948: template_rect['x'] = 2048 - x if y < 100 or y > 1948: # I hard coded a height bound here, change in the future template_rect['y'] = y template_rect['h'] = 2 * y if y > 1948: template_rect['y'] = 2048 - y '''TODO: I'm leaving previous code, which makes a dictionary, as is for now. Potentially hange this in the future''' x = template_rect['x'] y = template_rect['y'] height = template_rect['h'] width = template_rect['w'] img = cv2.imread('app/static/images/Initial_Image.jpg', cv2.IMREAD_GRAYSCALE) template = img[y:y+height, x:x+width] # print('Warning: The tracking parameters are currently in testing mode (no rotation, 200 pixel translation steps)') # Warns user # track_matrix_fast('Testing', 'Data/Testing/Denoised', template, img.shape, 360, 200) # This is just for testing, very large steps=fast but inaccurate track_matrix_fast('Testing', 'Data/Testing/Denoised', template, img.shape) # This is for real tracking aperture_photometry(np.load('Data/Testing/positions.npy')) # track_matrix('Testing', 'Data/Testing/Denoised', template_rect) # track('Testing', 'Data/Testing/Denoised', template_rect) return redirect('/lightcurve') # If post request with form2, adjusts the denoising parameter elif form2.validate_on_submit() and form2.submit.data: response = make_response(render_template('canvas.html', form=form, form2=form2)) if form2.data['adjust'] == 'reset': response.set_cookie('threshold', '.35') # If user chooses reset, resets threshold to .35 denoise(.35) # Denoises with .35 as threshold else: # Otherwise, increases/decreases threshold by amount specified by user threshold = round(float(request.cookies.get('threshold')) + float(form2.data['adjust']), 2) # Rounds to 2 decimals print(str(threshold)) # Just for testing denoise(threshold) # Denoises with new threshold response.set_cookie('threshold', str(threshold)) # Sets new threshold return response response = make_response(render_template('canvas.html', form=form, form2=form2)) if 'threshold' not in request.cookies: # Sets cookie holding threshold if it hasn't been set response.set_cookie('threshold', '.35') # Note: Threshold has to be a string, can't use a float return response