def calculate_result(self, url, projectPK, **kwargs): print("downloading {}".format(url)) response=get_response(url) name=urlparse.urlparse(response.url)[2].split("/")[-1] name=name.replace("_display_large","") print(name) fileobjectInstance=fileobject() fileobjectInstance.parent=Project.objects.get(pk=projectPK) from django.core.files.base import ContentFile fileobjectInstance.filename = ContentFile('') fileobjectInstance.filename.name = name while True: chunk = response.read(2048) if not chunk: break fileobjectInstance.filename.write(chunk) print('done') #fileobjectInstance.filename = newFile#.save(name,newFile) fileobjectInstance.filename.close() super(fileobject,fileobjectInstance).save() return(url,projectPK)
def updateReadme(self, text): if self.bodyFile: self.bodyFile.fromText(text) else: newFileObject = fileobject(parent=self) newFileObject.fromText(text, title="README.md") newFileObject.save() print(newFileObject) self.bodyFile = newFileObject
def edit(request): # data is the users userdata. data = request.user ## redirect to login if not logged in. try: profile = userProfile.objects.filter(user=data)[0] except: return redirect("/register/") ## User submitting profile data. if request.method == 'POST': #get data from the forms profileform = UserProfileForm(request.POST) pictureform = UserPictureForm(request.POST, request.FILES) ## puts the users stuff in the database if it is valid. if profileform.is_valid() and pictureform.is_valid(): ###Create user's profile profile.bio = profileform.cleaned_data["bio"] #Create users picture. if pictureform.cleaned_data["filename"]: ## if they don't have a userpic yet we can't delete it. try: profile.userpic.delete() except: pass thumbiwumbi = fileobject(parent=profile) thumbiwumbi.filename = request.FILES["filename"] thumbiwumbi.save() profile.userpic = thumbiwumbi profile.save() return redirect("/userProfile/" + str(data.pk)) #returns form with error messages. else: return render_to_response( 'editProfile.html', dict(user=request.user, form2=profileform, form3=pictureform)) ## Initializes the page with the forms. else: #form = PasswordChangeForm(data) profileform = UserProfileForm({'bio': profile.bio}) pictureform = UserPictureForm() #{"filename":profile.}) return render_to_response( 'editProfile.html', dict(user=request.user, form2=profileform, form3=pictureform))
def edit(request): # data is the users userdata. data = request.user ## redirect to login if not logged in. try: profile = userProfile.objects.filter(user=data)[0] except: return redirect("/register/") ## User submitting profile data. if request.method == 'POST': #get data from the forms profileform = UserProfileForm(request.POST) pictureform = UserPictureForm(request.POST, request.FILES) ## puts the users stuff in the database if it is valid. if profileform.is_valid() and pictureform.is_valid(): ###Create user's profile profile.bio = profileform.cleaned_data["bio"] #Create users picture. #if pictureform.cleaned_data["filename"]: if False: ## if they don't have a userpic yet we can't delete it. try: profile.userpic.delete() except: pass thumbiwumbi = fileobject(parent=profile) thumbiwumbi.filename = request.FILES["filename"] thumbiwumbi.save() profile.userpic = thumbiwumbi profile.save() return redirect("/userProfile/"+str(data.pk)) #returns form with error messages. else: return render_to_response('editProfile.html', dict( user=request.user, form2=profileform, form3=pictureform)) ## Initializes the page with the forms. else: #form = PasswordChangeForm(data) profileform = UserProfileForm({'bio':profile.bio}) pictureform = UserPictureForm()#{"filename":profile.}) return render_to_response('editProfile.html', dict( user=request.user, form2=profileform, form3=pictureform))
def multiuploader(request, pk): project=Project.objects.filter(pk=pk)[0] """ Main Multiuploader module. Parses data from jQuery plugin and makes database changes. """ if request.method == 'POST' and str(project.author) == str(request.user): log.info('received POST to main multiuploader view') if request.FILES == None: return HttpResponseBadRequest('Must have files attached!') #getting file data for farther manipulations projectfiles = fileobject(parent=project) # projectfiles.project = project # this is nonsense... is this not nonsense? Cruft. projectfiles.filename = request.FILES[u'files[]'] projectfiles.save() projectfiles.filename.close() log.info ('Got file: "%s"' % str(projectfiles.filename.name)) #settings imports try: file_delete_url = settings.MULTI_FILE_DELETE_URL+'/' except AttributeError: file_delete_url = 'multi_delete/' #thumbnail = thumbobject.objects.get_or_create( fileobject = projectfiles, filex=64, filey=64 )[0] result = [] ##It waits for the thumbnail to generate before sending the json, which should work. images=[projectfiles.get_thumb(64,64)] thumburl = render_to_string("gallery.html", dict(images=images, galleryname="ajax")) result.append({"name":projectfiles.subfolder+os.path.split(str(projectfiles.filename.name))[1], "size":projectfiles.filename.size, "thumbnail_url":thumburl, "delete_url":"/multi_delete/"+str(projectfiles.pk)+"/", "delete_type":"POST",}) response_data = simplejson.dumps(result) #checking for json data type #big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype) else: #GET object_type = ContentType.objects.get_for_model(project) projectfiles = fileobject.objects.filter(content_type=object_type,object_id=project.id) file_delete_url = settings.MULTI_FILE_DELETE_URL+'/' result = [] for image in projectfiles: #thumbnail = thumbobject.objects.get_or_create( fileobject = image, filex=64, filey=64 )[0] images=[image.get_thumb(64,64)] thumburl = render_to_string("gallery.html", dict(images=images, galleryname="ajax")) ##json stuff result.append({"name":image.subfolder+os.path.split(image.filename.name)[1], "size":image.filename.size, "thumbnail_url":thumburl, "delete_url":"/multi_delete/"+str(image.pk)+"/", "delete_type":"POST",}) response_data = simplejson.dumps(result) if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype)
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 calculate_result(self, url, userPK, **kwargs): print("importing project : {}".format(url)) response=get_response(url) goo=response.read() dom=html.fromstring(goo) #print(dom.xpath('//*[contains(@class,\'thing-header-data\')]/h1/text()')) # Getting some metadatas for the project. #this is probably fine. If you're confused, feel free to make it more verbose. project=Project() project.author_id = userPK# User.objects.get(pk=userPK) project.title = dom.xpath('//*[contains(@class,\'thing-header-data\')]/h1/text()')[0].strip() tags = dom.xpath("//*[contains(@class,\'thing-info-content thing-detail-tags-container\')]/div/a/text()") project.draft=True if Project.objects.filter(title=project.title): import datetime project.title+= " -- "+str(datetime.datetime.today()) project.save() for tag in tags: project.tags.add(tag) ## get special text files. (readme, instructions, license) import html2text h2t = html2text.HTML2Text() #Get the reame file, do stuff to it. readme = etree.tostring(dom.xpath("//*[@id = 'description']")[0]) readme = readme.encode("utf-8") readme = h2t.handle(readme) import unicodedata readmeItem=fileobject() readmeItem.parent=project#projectObject['SID'] readmeItem.isReadme = True readmename="README.md" readmefile=u""+unicodedata.normalize('NFKD',readme).encode('ascii','ignore') print(readmename) print(readmefile) readmeItem.fromText(readmefile,readmename) readmeItem.save() project.bodyFile=readmeItem project.save() print("bodyFile:") print(project.bodyFile) #projectObject['readme'] = u""+unicodedata.normalize('NFKD',readme).encode('ascii','ignore') #also a markdown file I guess we'd want. try: instructions = etree.tostring(dom.xpath("//*[@id = 'instructions']")[0]) instructions = u""+h2t.handle(instructions).encode('ascii','ignore') instructionItem=fileobject() instructionItem.parent=project#Object['SID'] name="Instructions.md" filename=instructions instructionItem.fromText(filename,name) instructionItem.save() except IndexError: pass #print("xpath to get the instructions IndexError'd") ## now, because the format of the license on thingi is always the same, we can pull this off. ## but I expect it is rather fragile. licenseurl =dom.xpath("//*[contains(@class,\'license-text\')]/a/@href")[2].strip() licensetext = dom.xpath("//*[contains(@class,\'license-text\')]/a/text()")[1].strip() licenceItem=fileobject() licenceItem.parent=project#Object['SID'] lname="License.md" lfile="["+licensetext+"]("+licenseurl+")" licenceItem.fromText(lfile,lname) licenceItem.save() ## get all the projects image and file objects #grab files filelist = dom.xpath('//*[contains(@class,\'thing-file\')]/a/@href') #Grab only raw images. imagelist = dom.xpath('//*[contains(@class,\'thing-gallery-thumbs\')]/div[@data-track-action="viewThumb"][@data-thingiview-url=""]/@data-large-url') fileurls=[urlparse.urljoin('http://www.thingiverse.com/', fl) for fl in imagelist+filelist] print("fileurls:") print(fileurls) bundle_o_tasks=[] for fileurl in fileurls: bundle_o_tasks+=[ThingiFileTask().si(url=fileurl,projectPK=project.pk)] filetask = chord(bundle_o_tasks) filetask(ResaveProjectTask().si(projectPK=project.pk)) return(project.title)
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 multiuploader(request, pk): project=Project.objects.filter(pk=pk)[0] """ Main Multiuploader module. Parses data from jQuery plugin and makes database changes. """ if request.method == 'POST' and str(project.author) == str(request.user): log.info('received POST to main multiuploader view') if request.FILES == None: return HttpResponseBadRequest('Must have files attached!') #getting file data for farther manipulations projectfiles = fileobject() projectfiles.project = project projectfiles.filename = request.FILES[u'files[]'] projectfiles.save() log.info ('Got file: "%s"' % str(projectfiles.filename.name)) #settings imports try: file_delete_url = settings.MULTI_FILE_DELETE_URL+'/' except AttributeError: file_delete_url = 'multi_delete/' thumbnail = thumbObjectProxy.objects.get_or_create( fileobject = projectfiles, filex=64, filey=64 )[0] result = [] ##It waits for the thumbnail to generate before sending the json, which should work. thumburl="" try: thumburl=thumbnail.filename.url except: pass result.append({"name":projectfiles.subfolder+os.path.split(str(projectfiles.filename.name))[1], "size":projectfiles.filename.size, "url":str(projectfiles.filename.url), "thumbnail_url":thumburl, "delete_url":"/multi_delete/"+str(projectfiles.pk)+"/", "delete_type":"POST",}) response_data = simplejson.dumps(result) #checking for json data type #big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype) else: #GET projectfiles = fileobject.objects.filter(project=project) file_delete_url = settings.MULTI_FILE_DELETE_URL+'/' result = [] for image in projectfiles: thumbnail = thumbobject.objects.get_or_create( fileobject = image, filex=64, filey=64 )[0] if thumbnail.filename != "False": thumburl=thumbnail.filename.url else: thumburl="" ##json stuff result.append({"name":image.subfolder+os.path.split(image.filename.name)[1], "size":image.filename.size, "url":"/preview/"+str(image.filetype+image.filename.url), "thumbnail_url":thumburl, "delete_url":"/multi_delete/"+str(image.pk)+"/", "delete_type":"POST",}) response_data = simplejson.dumps(result) if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype)