def image(request, message=None): global current_image # Parse message if (message): message = message.replace('_', ' ') # If an image is captured create new image object and update related fields if (message == "image served"): imagelist = Image.objects.all().order_by('name') c = { 'message': message, 'form': UploadFileForm(), 'imagelist': imagelist, 'username': request.user.username } if (not (current_image.imagex)): return render(request, "image.html", c) dir = os.getcwd() im_path = dir + "/media/" + current_image.imagex.name[:-4] im_p = current_image.imagex.name[:-4] im_ext = current_image.imagex.name[-4:] Image(name="ruled_image").imagex.save( current_image.imagex.name[15:], File(open(im_path + "_ruled" + im_ext, 'rb'))) ruled_image = Image.objects.get(name="ruled_image") Image.objects.get(name="ruled_image").delete() if (ruled_image and ruled_image.imagex): c['current_image'] = ruled_image return render(request, "image.html", c) # If requesting user is admin serve raw image elif (message == 'raw image served to admin'): imagelist = Image.objects.all().order_by('name') c = { 'message': message, 'form': UploadFileForm(), 'imagelist': imagelist, 'username': request.user.username } if (current_image and current_image.imagex): c['current_image'] = current_image return render(request, "image.html", c) # Else keep current image else: imagelist = Image.objects.all().order_by('name') c = { 'message': message, 'form': UploadFileForm(), 'imagelist': imagelist, 'username': request.user.username } if (current_image and current_image.imagex): c['current_image'] = current_image return render(request, "image.html", c)
def loadImage(request, owner): global current_image try: if request.POST['submit'] == "LoadImage": form = UploadFileForm(request.POST, request.FILES) # If form exists parse form data if form.is_valid(): m = Image(name='currentLoadedImage') m.imagex = form.cleaned_data['file'] m.save() current_image = Image.objects.get(name="currentLoadedImage") current_image.owner = request.user.username Image.objects.get(name="currentLoadedImage").delete() return redirect('/client/image/msg/Image_loaded_from_file') else: return redirect('client/image/msg/Invalid_request/' ) #TODO add invalid request message except KeyError: # form not submitted yet, show it return redirect('/client/image/msg/Key_error/') return redirect('/client/image')
def loadImage(request, owner): global current_image try: if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) # If form exists parse form data if form.is_valid(): m = Image(name='currentLoadedImage') m.imagex = form.cleaned_data['file'] m.save() current_image = Image.objects.get(name="currentLoadedImage") current_image.owner = request.user.username Image.objects.get(name="currentLoadedImage").delete() print(current_image.imagex) return JsonResponse(str(current_image.imagex), safe=False) else: return redirect('client/image/msg/Invalid_request/') except KeyError: # form not submitted yet, show it return redirect('/client/image/msg/Key_error/') return redirect('/client/image')
def save(request): global current_image try: if request.method == 'POST': name = request.POST['name'] Image(name=name, rules=current_image.rules, owner=current_image.owner, defaultAction=current_image.defaultAction, imagex=current_image.imagex).save() return HttpResponse("") else: return redirect( '/client/image/msg/Invalid_request/') # redirect to home page except KeyError: # form not submitted yet, show it return redirect( '/client/image/msg/Key_error/') # redirect to home page
def save(request): global current_image try: if request.POST['submit'] == 'Save': # form submitted, process it name = request.POST['name'] Image(name=name, rules=current_image.rules, owner=current_image.owner, defaultAction=current_image.defaultAction, imagex=current_image.imagex).save() return redirect( '/client/image/msg/Image_saved/') # redirect to home page else: return redirect( '/client/image/msg/Invalid_request/') # redirect to home page except KeyError: # form not submitted yet, show it return redirect( '/client/image/msg/Key_error/') # redirect to home page
import os import re import numpy as np from django.core.files import File # Create your views here. class SignUp(generic.CreateView): form_class = UserCreationForm success_url = reverse_lazy('login') template_name = 'signup.html' # Initialize current image current_image = Image() # Function for index page rendering @login_required def index(request, message=None): global current_image current_image = None if (message): message = message.replace('_', ' ') ulist = list(User.objects.all()) glist = list(Group.objects.all()) imagelist = Image.objects.all().order_by('name') context = { 'ulist': ulist, 'glist': glist,
def getImage(request): global current_image try: if request.method == 'POST': if (current_image and current_image.imagex): dir = os.getcwd() image = cv2.imread(dir + "/media/" + current_image.imagex.name) # If admin is request user return raw image if (request.user.username == 'admin'): return JsonResponse(str(current_image.imagex), safe=False) global mask # If setdefault is blur create a blurred image if (current_image.defaultAction == 'BLUR'): image = cv2.blur(image, (7, 7)) # Initialize mask as nparray with all zeros mask = np.zeros(image.shape, dtype="uint8") # Create mask depending on default action if (current_image.defaultAction == 'ALLOW' or current_image.defaultAction == 'BLUR'): h = image.shape[0] w = image.shape[1] for y in range(0, h): for x in range(0, w): mask[y, x] = 255 # Draw the rules on the mask rules = current_image.getRules() for rule in (rules): if (rule[0][0] == 'u'): if (rule[0][2:] == request.user.username): applyRule(rule) elif (rule[0][0] == 'g'): gname = rule[0][2:] if (memberCheck(request, request.user.username, gname)): applyRule(rule) elif (rule[0][0] == 'r'): rexp = rule[0][2:] revalidator = re.compile(rexp) if (revalidator.match(user[0])): applyRule(rule) else: return redirect('/client/image/msg/?/') # Apply mask to the image maskedImg = cv2.bitwise_and(image, mask) # Handle file operations im_path = dir + "/media/" + current_image.imagex.name[:-4] im_ext = current_image.imagex.name[-4:] cv2.imwrite(im_path + "_ruled" + im_ext, maskedImg) imagelist = Image.objects.all().order_by('name') if (not (current_image.imagex)): return HttpResponse("") # Create new image object dir = os.getcwd() im_path = dir + "/media/" + current_image.imagex.name[:-4] im_p = current_image.imagex.name[:-4] im_ext = current_image.imagex.name[-4:] Image(name="ruled_image").imagex.save( current_image.imagex.name[15:], File(open(im_path + "_ruled" + im_ext, 'rb'))) # Handle file operations ruled_image = Image.objects.get(name="ruled_image") Image.objects.get(name="ruled_image").delete() if (ruled_image and ruled_image.imagex): return JsonResponse(str(ruled_image.imagex), safe=False) else: return JsonResponse(str(current_image.imagex), safe=False) else: return JsonResponse("Image Not Loaded!", safe=False) else: return redirect( '/client/image/msg/Invalid_request/') # redirect to home page except KeyError: # form not submitted yet, show it return redirect( '/client/image/msg/Key_error/') # redirect to home page return redirect('/client')