def stitch_chunks(self): f = open(os.path.join(settings.MEDIA_ROOT, cloud_path(self, self.filename)), "wb") for chunk in self.chunks.all().order_by("pk"): f.write(chunk.chunk.read()) f.close() f = UploadedFile(open(f.name, "rb")) self.upload.save(self.filename, f) self.state = Upload.STATE_COMPLETE self.save() f.close()
def handle_noargs(self, users=None, review_requests=None, diffs=None, reviews=None, diff_comments=None, password=None, verbosity=NORMAL, **options): num_of_requests = None num_of_diffs = None num_of_reviews = None num_of_diff_comments = None random.seed() if review_requests: num_of_requests = self.parseCommand("review_requests", review_requests) # Setup repository. repo_dir = os.path.abspath( os.path.join(sys.argv[0], "..", "scmtools", "testdata", "git_repo")) # Throw exception on error so transaction reverts. if not os.path.exists(repo_dir): raise CommandError("No path to the repository") self.repository = Repository.objects.create( name="Test Repository", path=repo_dir, tool=Tool.objects.get(name="Git")) if diffs: num_of_diffs = self.parseCommand("diffs", diffs) # Create the diff directory locations. diff_dir_tmp = os.path.abspath( os.path.join(sys.argv[0], "..", "reviews", "management", "commands", "diffs")) # Throw exception on error so transaction reverts. if not os.path.exists(diff_dir_tmp): raise CommandError("Diff dir does not exist") diff_dir = diff_dir_tmp + '/' # Add trailing slash. # Get a list of the appropriate files. files = [f for f in os.listdir(diff_dir) if f.endswith('.diff')] # Check for any diffs in the files. if len(files) == 0: raise CommandError("No diff files in this directory") if reviews: num_of_reviews = self.parseCommand("reviews", reviews) if diff_comments: num_of_diff_comments = self.parseCommand("diff-comments", diff_comments) # Users is required for any other operation. if not users: raise CommandError("At least one user must be added") # Start adding data to the database. for i in range(1, users + 1): new_user = User.objects.create( username=self.randUsername(), # Avoids having to flush db. first_name=random.choice(NAMES), last_name=random.choice(NAMES), email="*****@*****.**", is_staff=False, is_active=True, is_superuser=False) if password: new_user.set_password(password) new_user.save() else: new_user.set_password("test1") new_user.save() Profile.objects.create( user=new_user, first_time_setup_done=True, collapsed_diffs=True, wordwrapped_diffs=True, syntax_highlighting=True, show_submitted=True) # Review Requests. req_val = self.pickRandomValue(num_of_requests) if int(verbosity) > NORMAL: print "For user %s:%s" % (i, new_user.username) print "=============================" for j in range(0, req_val): if int(verbosity) > NORMAL: print "Request #%s:" % j review_request = ReviewRequest.objects.create(new_user, None) review_request.public = True review_request.summary = self.lorem_ipsum("summary") review_request.description = self.lorem_ipsum("description") review_request.shipit_count = 0 review_request.repository = self.repository # Set the targeted reviewer to superuser or 1st defined. if j == 0: review_request.target_people.add(User.objects.get(pk=1)) review_request.save() # Add the diffs if any to add. diff_val = self.pickRandomValue(num_of_diffs) # If adding diffs add history. if diff_val > 0: diffset_history = DiffSetHistory.objects.create( name='testDiffFile' + str(i)) diffset_history.save() # Won't execute if diff_val is 0, ie: no diffs requested. for k in range(0, diff_val): if int(verbosity) > NORMAL: print "%s:\tDiff #%s" % (i, k) random_number = random.randint(0, len(files) - 1) file_to_open = diff_dir + files[random_number] f = UploadedFile(open(file_to_open, 'r')) form = UploadDiffForm(review_request.repository, f) cur_diff = form.create(f, None, diffset_history) review_request.diffset_history = diffset_history review_request.save() review_request.publish(new_user) f.close() # Add the reviews if any. review_val = self.pickRandomValue(num_of_reviews) for l in range(0, review_val): if int(verbosity) > NORMAL: print "%s:%s:\t\tReview #%s:" % (i, j, l) reviews = Review.objects.create( review_request=review_request, user=new_user) reviews.publish(new_user) # Add comments if any. comment_val = self.pickRandomValue( num_of_diff_comments) for m in range(0, comment_val): if int(verbosity) > NORMAL: print "%s:%s:\t\t\tComments #%s" % (i, j, m) if m == 0: file_diff = cur_diff.files.order_by('id')[0] # Choose random lines to comment. # Max lines: should be mod'd in future to read # diff. max_lines = 220 first_line = random.randrange(1, max_lines - 1) remain_lines = max_lines - first_line num_lines = random.randrange(1, remain_lines) diff_comment = Comment.objects.create( filediff=file_diff, text="comment number %s" % (m + 1), first_line=first_line, num_lines=num_lines) review_request.publish(new_user) reviews.comments.add(diff_comment) reviews.save() reviews.publish(new_user) db.reset_queries() # No comments, so have previous layer clear queries. if comment_val == 0: db.reset_queries() if review_val == 0: db.reset_queries() if diff_val == 0: db.reset_queries() if req_val == 0: db.reset_queries() # Generate output as users & data is created. if req_val != 0: print "user %s created with %s requests" % ( new_user.username, req_val) else: print "user %s created successfully" % new_user.username
def editOrCreateStuff(project, request, creating): ## Note: if creating == true this is a post being created. #Because there are so many similarities in creating a post vs editing a post we are using this method, and using creating when we need to do something different for editing vs creating. ## postmode! We are getting pretty post data from the user!!! if request.method == 'POST': ## get the forms and check that they are valid formValid = False if creating: form = createForm(request.POST, project) form2 = defaulttag(request.POST) if form.is_valid() and form2.is_valid( ) and request.user.is_authenticated(): formValid = True # If we are creating the post we need to set the author and title. project.author = request.user project.title = form.cleaned_data["title"] else: form = ProjectForm(request.POST, project) if form.is_valid() and str(project.author) == str(request.user): formValid = True ## if the form is valid make the changes to the project! if formValid: # Editing the Readme.md file stuff. if not creating: # Delete the old body text file... cause I'm a bad person and I don't know how to just open and write to the old one easily. readme = project.bodyFile try: readme = project.bodyFile readmename = path.split(str(readme.filename))[1] readme.delete() except: pass # Save body as file bodyText = fileobject() bodyText.parent = project from django.core.files.uploadedfile import UploadedFile import base64 from io import BytesIO from io import TextIOWrapper from io import StringIO #io = TextIOWrapper(TextIOBase(form.cleaned_data["body"])) io = StringIO(form.cleaned_data["body"]) txfl = UploadedFile(io) #editfield may be renaming your readme to readme.md every time. That's not good. try: bodyText.filename.save(readmename, txfl) except: bodyText.filename.save('README.md', txfl) txfl.close() io.close() bodyText.save() #### this did not appear to be happening in the create.... but I think it should have been? project.bodyFile = bodyText # Done with editing the README.md textfile. # list_to_tags(form.cleaned_data["tags"], project.tags) if creating: list_to_tags(form2.cleaned_data["categories"], project.tags, False) # This may be redundant, but either way, this post is not a draft past this point. project.draft = False project.save() return HttpResponseRedirect('/project/' + str(project.pk)) #### If the form data was NOT valid else: if creating: return render_to_response( 'create.html', dict(user=request.user, form=form, form2=form2, project=project)) else: if str(project.author) == str(request.user): return render_to_response( 'edit.html', dict( project=project, user=request.user, form=form, )) else: return HttpResponse(status=403) #### Not POSTmode! We are setting up the form for the user to fill in. We are not getting form data from the user. ##### CREATE elif creating and request.user.is_authenticated(): form = createForm("", project) form2 = defaulttag() return render_to_response( 'create.html', dict(user=request.user, form=form, form2=form2, project=project)) ##### EDIT elif (not creating) and str(project.author) == str(request.user): if project.bodyFile: readme = project.bodyFile.filename.read() else: readme = project.body taglist = [] for i in project.tags.names(): taglist.append(i) taglist = ",".join(taglist) thumbnailstring = "/" + path.split(project.thumbnail.filename.url)[1] form = ProjectForm( { 'body': readme, 'thumbnail': thumbnailstring, 'tags': str(taglist) }, project) return render_to_response( 'edit.html', dict( project=project, user=request.user, form=form, )) #return HttpResponse(response_data, mimetype="application/json") else: return HttpResponse(status=403)
def editOrCreateStuff(project, request, creating): ## Note: if creating == true this is a post being created. #Because there are so many similarities in creating a post vs editing a post we are using this method, and using creating when we need to do something different for editing vs creating. ## postmode! We are getting pretty post data from the user!!! if request.method == 'POST': ## get the forms and check that they are valid formValid=False if creating: form = createForm(request.POST, project) form2 = defaulttag(request.POST) if form.is_valid() and form2.is_valid() and request.user.is_authenticated(): formValid=True # If we are creating the post we need to set the author and title. project.author = request.user project.title = form.cleaned_data["title"] else: form = ProjectForm(request.POST, project) if form.is_valid() and str(project.author) == str(request.user): formValid=True ## if the form is valid make the changes to the project! if formValid: # Editing the Readme.md file stuff. if not creating: # Delete the old body text file... cause I'm a bad person and I don't know how to just open and write to the old one easily. readme = project.bodyFile try: readme = project.bodyFile readmename = path.split(str(readme.filename))[1] readme.delete() except: pass # Save body as file bodyText = fileobject() bodyText.parent = project from django.core.files.uploadedfile import UploadedFile import base64 from io import BytesIO from io import TextIOWrapper from io import StringIO #io = TextIOWrapper(TextIOBase(form.cleaned_data["body"])) io = StringIO(form.cleaned_data["body"]) txfl = UploadedFile(io) #editfield may be renaming your readme to readme.md every time. That's not good. try: bodyText.filename.save(readmename, txfl) except: bodyText.filename.save('README.md', txfl) txfl.close() io.close() bodyText.save() #### this did not appear to be happening in the create.... but I think it should have been? project.bodyFile = bodyText # Done with editing the README.md textfile. # list_to_tags(form.cleaned_data["tags"], project.tags) if creating: for i in form2.cleaned_data["categories"]: project.tags.add(i) # This may be redundant, but either way, this post is not a draft past this point. project.draft=False project.save() return HttpResponseRedirect('/project/'+str(project.pk)) #### If the form data was NOT valid else: if creating: return render_to_response('create.html', dict(user=request.user, form=form, form2=form2, project=project)) else: if str(project.author) == str(request.user): return render_to_response('edit.html', dict(project=project, user=request.user, form=form, )) else: return HttpResponse(status=403) #### Not POSTmode! We are setting up the form for the user to fill in. We are not getting form data from the user. ##### CREATE elif creating and request.user.is_authenticated(): form = createForm("",project) form2 = defaulttag() return render_to_response('create.html', dict(user=request.user, form=form, form2=form2, project=project)) ##### EDIT elif (not creating) and str(project.author) == str(request.user): if project.bodyFile: readme = project.bodyFile.filename.read() else: readme = project.body taglist = [] for i in project.tags.names(): taglist.append(i) taglist = ",".join(taglist) thumbnailstring = "/"+path.split(project.thumbnail.filename.url)[1] form = ProjectForm({'body': readme, 'thumbnail': thumbnailstring, 'tags' : str(taglist)}, project) return render_to_response('edit.html', dict(project=project, user=request.user, form=form,)) #return HttpResponse(response_data, mimetype="application/json") else: return HttpResponse(status=403)
def handle_files(files, time, board): """ Check and save files. :param files: files fot handling :param time: current time :param board: post's board :return: json list of files features """ _files = [] for file in files.items(): size = file[1].size if size > config['max_filesize']: return False name = file[1].name ext = name.split('.')[-1] if not ext.lower() in config['allowed_ext']: return False # file saving index = file[0].replace('file', '') # equal 0 for first file and so on path = choose_path(board, 'src', time, ext, index) with open(path, 'wb+') as destination: for chunk in file[1].chunks(): destination.write(chunk) destination.close() # TODO: Refactor all this hell if ext.lower() == 'webm': temp_file = NamedTemporaryFile() temp_path = temp_file.name + '.png' call(["ffmpeg", "-i", path, "-vframes", "1", temp_path]) temp_file.close() temp_th = open(temp_path, 'rb+') preview = UploadedFile(file=temp_th) thumb = make_thumb(preview) preview.close() image = Image.open(temp_path) else: image = Image.open(path) thumb = make_thumb(file[1]) path = choose_path(board, 'thumb', time, 'jpg', index) destination = open(path, 'wb+') destination.write(thumb.read()) destination.close() thumb = Image.open(path) filename = '{0}-{1}.{2}'.format(time, index, ext) file_data = { "name": name, "type": 0, # content_type, "tmp_name": ".", # ??? "error": 0, "size": size, "filename": name, "extension": ext, "file_id": time, "file": filename, "thumb": '{0}-{1}.jpg'.format(time, index), "is_an_image": 0, # content_type.split('/')[0] == 'image', "hash": "c5c76d11ff82103d18c3c9767bcb881e", # TODO hash "width": image.width, "height": image.height, "thumbwidth": thumb.width, "thumbheight": thumb.height, "file_path": '{0}/src/{1}'.format(board, filename), "thumb_path": '{0}/thumb/{1}-{2}.jpg'.format(board, time, index) } image.close() thumb.close() _files.append(file_data) return _files
def handle_noargs(self, users=None, review_requests=None, diffs=None, reviews=None, diff_comments=None, password=None, verbosity=NORMAL, **options): num_of_requests = None num_of_diffs = None num_of_reviews = None num_of_diff_comments = None random.seed() if review_requests: num_of_requests = self.parseCommand("review_requests", review_requests) # Setup repository. repo_dir = os.path.abspath( os.path.join(sys.argv[0], "..", "scmtools", "testdata", "git_repo")) # Throw exception on error so transaction reverts. if not os.path.exists(repo_dir): raise CommandError("No path to the repository") self.repository = Repository.objects.create( name="Test Repository", path=repo_dir, tool=Tool.objects.get(name="Git")) if diffs: num_of_diffs = self.parseCommand("diffs", diffs) # Create the diff directory locations. diff_dir_tmp = os.path.abspath( os.path.join(sys.argv[0], "..", "reviews", "management", "commands", "diffs")) # Throw exception on error so transaction reverts. if not os.path.exists(diff_dir_tmp): raise CommandError("Diff dir does not exist") diff_dir = diff_dir_tmp + '/' # Add trailing slash. # Get a list of the appropriate files. files = [f for f in os.listdir(diff_dir) if f.endswith('.diff')] # Check for any diffs in the files. if len(files) == 0: raise CommandError("No diff files in this directory") if reviews: num_of_reviews = self.parseCommand("reviews", reviews) if diff_comments: num_of_diff_comments = self.parseCommand("diff-comments", diff_comments) # Users is required for any other operation. if not users: raise CommandError("At least one user must be added") # Start adding data to the database. for i in range(1, users + 1): new_user = User.objects.create( username=self.randUsername(), # Avoids having to flush db. first_name=random.choice(NAMES), last_name=random.choice(NAMES), email="*****@*****.**", is_staff=False, is_active=True, is_superuser=False) if password: new_user.set_password(password) new_user.save() else: new_user.set_password("test1") new_user.save() Profile.objects.create(user=new_user, first_time_setup_done=True, collapsed_diffs=True, wordwrapped_diffs=True, syntax_highlighting=True, show_submitted=True) # Review Requests. req_val = self.pickRandomValue(num_of_requests) if int(verbosity) > NORMAL: print "For user %s:%s" % (i, new_user.username) print "=============================" for j in range(0, req_val): if int(verbosity) > NORMAL: print "Request #%s:" % j review_request = ReviewRequest.objects.create(new_user, None) review_request.public = True review_request.summary = self.lorem_ipsum("summary") review_request.description = self.lorem_ipsum("description") review_request.shipit_count = 0 review_request.repository = self.repository # Set the targeted reviewer to superuser or 1st defined. if j == 0: review_request.target_people.add(User.objects.get(pk=1)) review_request.save() # Add the diffs if any to add. diff_val = self.pickRandomValue(num_of_diffs) # If adding diffs add history. if diff_val > 0: diffset_history = DiffSetHistory.objects.create( name='testDiffFile' + str(i)) diffset_history.save() # Won't execute if diff_val is 0, ie: no diffs requested. for k in range(0, diff_val): if int(verbosity) > NORMAL: print "%s:\tDiff #%s" % (i, k) random_number = random.randint(0, len(files) - 1) file_to_open = diff_dir + files[random_number] f = UploadedFile(open(file_to_open, 'r')) form = UploadDiffForm(review_request.repository, f) cur_diff = form.create(f, None, diffset_history) review_request.diffset_history = diffset_history review_request.save() review_request.publish(new_user) f.close() # Add the reviews if any. review_val = self.pickRandomValue(num_of_reviews) for l in range(0, review_val): if int(verbosity) > NORMAL: print "%s:%s:\t\tReview #%s:" % (i, j, l) reviews = Review.objects.create( review_request=review_request, user=new_user) reviews.publish(new_user) # Add comments if any. comment_val = self.pickRandomValue( num_of_diff_comments) for m in range(0, comment_val): if int(verbosity) > NORMAL: print "%s:%s:\t\t\tComments #%s" % (i, j, m) if m == 0: file_diff = cur_diff.files.order_by('id')[0] # Choose random lines to comment. # Max lines: should be mod'd in future to read # diff. max_lines = 220 first_line = random.randrange(1, max_lines - 1) remain_lines = max_lines - first_line num_lines = random.randrange(1, remain_lines) diff_comment = Comment.objects.create( filediff=file_diff, text="comment number %s" % (m + 1), first_line=first_line, num_lines=num_lines) review_request.publish(new_user) reviews.comments.add(diff_comment) reviews.save() reviews.publish(new_user) db.reset_queries() # No comments, so have previous layer clear queries. if comment_val == 0: db.reset_queries() if review_val == 0: db.reset_queries() if diff_val == 0: db.reset_queries() if req_val == 0: db.reset_queries() # Generate output as users & data is created. if req_val != 0: print "user %s created with %s requests" % (new_user.username, req_val) else: print "user %s created successfully" % new_user.username