Exemple #1
0
	def post(self):
		user = users.get_current_user()
		if user:
			request = self.request.get("button")
			
			if (request == "delete_clicked"):
				check_list = self.request.get_all("delete")
				for value in check_list:
					streamKey = ndb.Key(urlsafe=value)
					stream_obj = streamKey.get()
					images_query = Image.query(ancestor=streamKey)
					image_list = images_query.fetch()
					for imagez in image_list:
						blobstore.delete(imagez.blob_key)
						imagez.key.delete()
					stream_obj.key.delete()
			elif (request == "unsub_clicked"):
				check_list = self.request.get_all("unsubscribe")
				for value in check_list:
					streamKey = ndb.Key(urlsafe=value)
					stream_obj = streamKey.get()
					stream_obj.subscribers.remove(user.email())
					stream_obj.put()
			self.redirect('/manage')
		else:
			self.redirect('/')
    def get(self, student, filename):

        session = get_current_session()

        if (not loggedInAsAdmin(self)):
            return

        netid = session.get('netID')

        logging.debug("Checked for login")
        logging.debug("NetID: %s" % netid)
        logging.debug("Filename: %s" % filename)

        q = db.GqlQuery(
            "SELECT * FROM MyBlobFile WHERE netID = :1 AND fileName =:2",
            student, urllib.unquote(filename))
        results = q.get()

        logging.debug("Past the query. Result: %s" % str(results))

        # if it exists, delete it

        if (results != None):
            resource = results.blobstoreKey
            blobstore.delete(resource.key())
            db.delete(results)

            self.redirect("/adminview/%s" % student)
        else:
            errorMsg(
                self,
                "Sorry, this file does not seem to exist or you don't have permission to delete it"
            )
            return
 def fail():
     try:
         blobstore.delete(blob_info.key())
     except:
         pass
     self._json({'message': 'Failed to write the image to datastore.'},
                code=502)
Exemple #4
0
    def post(self):
        uploads = self.get_uploads()
        for file in uploads:
            filename = blobstore.BlobInfo(file.key()).filename

            user = users.get_current_user()
            my_user_key = ndb.Key(MyUser, user.user_id())
            my_user = my_user_key.get()

            currDirObj = my_user.currDir.get()
            path = ''
            if functions.isRootDirectory(my_user):
                path = currDirObj.dirPath + filename
            else:
                path = currDirObj.dirPath + '/' + filename

            file_id = my_user.key.id() + path
            file_key = ndb.Key(File, file_id)
            logging.info(file_id)
            if functions.exists(file_key, currDirObj.files):
                file_object = File(id=file_id)
                file_object.fileName = filename
                file_object.data = file.key()
                file_object.put()

                currDirObj.files.append(file_key)
                currDirObj.put()

            else:
                # Delete uploaded file from the blobstore
                blobstore.delete(file.key())
                logging.debug("File with the same name already exists!")

        self.redirect('/')
Exemple #5
0
    def get(self, id):
        user = users.get_current_user()
        if user is None:
            # go to login page
            print("View Stream Handler: Not logged in")
            self.redirect(users.create_login_page(self.request.uri))
            return

        current_stream = stream.get_by_id(int(id))
        if not current_stream:
            self.redirect("/error/" + 'Wrong stream or page number')
            return

        if current_stream:
            # delete all the imgs, because they are huge
            for i in current_stream.figures:
                if(not i.external):
                    blobstore.delete(i.blob_key)
            queried_user_profile = user_profile.query(user_profile.user_id == user.user_id()).get()
            if queried_user_profile:
                if current_stream.name in queried_user_profile.own_streams:
                    queried_user_profile.own_streams.remove(str(current_stream.name))
                if (not queried_user_profile.own_streams) and (not queried_user_profile.subscribed_streams):
                    # no own_streams nor subscribed_streams
                    queried_user_profile.key.delete()
                else:
                    queried_user_profile.put()
            current_stream.key.delete()
        time_sleep(NDB_UPDATE_SLEEP_TIME)
        self.redirect('/management')
        return
Exemple #6
0
        def _delete_model_blobs(self, model):
            """Deletes all blobs associated with the model (finds all BlobKeyProperty)"""

            for (name, prop) in model._properties.iteritems():
                if isinstance(prop, ndb.BlobKeyProperty):
                    if getattr(model, name):
                        blobstore.delete(getattr(model, name))
Exemple #7
0
    def get(self):
        user = users.get_current_user()
        if user:

            # Make sure we've cleared the datastore of a users old stuff, or we get blobstore errors
            collectionQuery = UserCollection.query(
                UserCollection.user == user.user_id())
            collectionResults = collectionQuery.fetch(1, keys_only=True)
            if collectionResults:
                screenshotQuery = UserScreenshot.query(
                    UserScreenshot.user == user.user_id())
                ssResults = screenshotQuery.fetch(None)
                for entry in ssResults:
                    #print "Deleting old data"
                    blobstore.delete(entry.blob_key)
                    entry.key.delete()
            else:
                userCollection = UserCollection(user=user.user_id(),
                                                parent=ndb.Key(
                                                    'UserCollection',
                                                    user.user_id()))
                userCollection.put()

            # There's less than 800 cards, 1600 incl. golden, 8 per page, + 10 for half full pages
            # 200 is above what we need.
            html = "<!DOCTYPE html>\n<html>\n  <meta charset=\"UTF-8\"/>\n"
            for i in range(200):
                upload_url = blobstore.create_upload_url("/upload")
                html += "  <meta class=\"uploadLink\" upload=\"" + upload_url + "\">\n"
            self.response.out.write(html + MAIN_PAGE_HTML)
        else:
            self.redirect(users.create_login_url(self.request.uri))
        return
Exemple #8
0
    def post(self):
        csvfile = self.get_uploads('label-upload')
        key = cgi.escape(self.request.get('label-type'))

        if key and csvfile:
            t = Template.get_by_id(key) or Alias.get_by_id(key)
            if t.__class__ == Alias:
                t = Template.query(Template.aliases == t.key).fetch()[0]
    
            blob = csvfile[0]
            iterator = BlobIterator(blobstore.BlobReader(blob.key()))

            labels = list()
            for row in csv.reader(iterator):
                if row: labels.append(row)
            
            blobstore.delete(blob.key())
            
            template = JINJA_ENVIRONMENT.get_template('labels.html')
            self.response.write(template.render({'key': t.key.string_id(),
                                                 'labels': labels,
                                                 'labels_per_page': int(t.layout.get().nx * t.layout.get().ny),
                                                 'label_barcode': cgi.escape(self.request.get('label-barcode')),
                                                 'label_orientation': cgi.escape(self.request.get('label-orientation')),
                                                 'label_font_size': cgi.escape(self.request.get('label-font-size')),
                                                 }))
        else:
            self.redirect('/') #TODO
Exemple #9
0
def generate_all():
    q = MyFile.all()
    mfiles = q.fetch(100)

    # delete all old files and data
    for mf in mfiles:
        bk = mf.blob_key
        blobstore.delete(bk)
        # delete data entity
        mf.delete()

    # dict of zipfiles being generated and their relevant generators
    generators = {
        'openvpn.zip': generate_ovpn,
        'windows.zip': generate_win,
        'linux.zip': generate_linux,
        'mac.zip': generate_mac,
        'android.zip': generate_android
    }

    ip_data = fetch_ip_data()

    for fn, g in generators.iteritems():
        data = g(ip_data)
        z = zipit(data)  #compress the data
        blob_file = files.blobstore.create('application/zip',
                                           _blobinfo_uploaded_filename=fn)
        with files.open(blob_file, 'a') as f:
            f.write(z)
        files.finalize(blob_file)
        blob_key = files.blobstore.get_blob_key(blob_file)
        mf = MyFile(name=fn, blob_key=str(blob_key))
        mf.update_date = datetime.datetime.now().date()
        mf.put()
Exemple #10
0
    def get(self):
        auth = authenticate(self)

        if auth[0]:
            current_user = User.query(
                User.username == auth[0]._User__email).get()

            form_data = cgi.FieldStorage()
            requests = form_data.getlist("chkDeleteStream")
            index = search.Index(INDEX_NAME)

            for key_str in requests:
                key = ndb.Key(urlsafe=key_str)
                stream = key.get()
                for pic_key in stream.pictures:
                    picture = pic_key.get()
                    picture_name = picture.name
                    filename = '/{}/Pictures'.format(
                        BUCKET_NAME) + "/" + picture_name
                    cloudstorage.delete(filename)
                    blob_key = picture.image
                    images.delete_serving_url(blob_key)
                    blobstore.delete(blob_key)
                    pic_key.delete()
                key.delete()
                index.delete(key_str)
                current_user.streams_owned.remove(key)

            current_user.put()
            time.sleep(.1)

            self.redirect('/ManageStream')
    def get(self, key=None):
        try: listing = db.get(db.Key(key))
        except:
            pass
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url('/logincheck'))
            return
        editing = True
        if key == None:
            listing = None
            editing = False
        if editing and not listing.owner == user:
            self.redirect('/')
            return

        if self.request.get('action') == 'delete-suite':
            suite = db.get(db.Key(self.request.get('suitekey')))
            blobstore.delete(suite.floorplan.key())
            suite.delete()

        values = {'user': user,
                  'is_admin': users.is_current_user_admin(),
                  'account': Account.all().filter("user =", user).get(),
                  'login_url': users.create_login_url('/logincheck'),
                  'logout_url': users.create_logout_url('/'),
                  'upload_url': blobstore.create_upload_url('/panel/listing/upload'),
                  'listing': listing,
                  'editing': editing,
                  'type': listing.sale_type,
                  }
        path = os.path.join(os.path.dirname(__file__), '_templates/panel_listing_edit.html')
        self.response.out.write(template.render(path, values))
    def get(self, key):

        photo_key = ndb.Key(urlsafe=key)

        url = self.request.url
        parsed = urlparse.urlparse(url)
        query = urlparse.parse_qs(parsed.query)

        if 'id_token' in query:
            id_token = query['id_token'][0]
            if User.auth_photo_user(key, id_token):
                self.response.set_cookie('id_token',
                                         id_token,
                                         max_age=3600,
                                         path='/')

                # Delete from storage
                blobstore.delete(photo_key.get().b_key)
                # Remove record from the corresponding user entity
                User.delete_photo(photo_key, id_token)
                # Delete the corresponding photo entity
                photo_key.delete()

                self.response.out.write("Successfully deleted. \r\n")
            else:
                self.response.out.write("401 No Authorization \r\n")
                self.response.set_status(401)
        else:
            self.response.out.write("401 No Authorization \r\n")
            self.response.set_status(401)
Exemple #13
0
    def post(self, resource):
        accessAllowed = self.verifyAdminStatus(userInfo.poster, 'Delete Photo')
        if not accessAllowed:
            return

        try:
            # Get the photo object and key for the given photo
            photo, key = self.getPhotoAndKey(resource)

            # If either the photo or the key could not be found, raise and exception (to be caught by the
            #	proceeding except).
            if photo == None or key == None:
                raise Exception()

            # Delete both the blobstore photo (the actual file) and the datastore object (the photo title,
            #	description, etc.
            ImageInfo.delete(photo)
            blobstore.delete(key)

            time.sleep(0.2)
            self.redirect('/photos')
            return
        except:
            error = 'Error: no photo found with the given id. Cannot delete photo.'

        self.renderPage(error=error)
    def post(self, secret, key):
        hacker = hacker_page.getHacker(secret)

        if hacker is None:
            logging.error("Attempted to change hacker's uploaded" + key + " but no hacker with key: " + secret)
            return self.redirect('/')

        newFiles = map(lambda f: f.key(), self.get_uploads(key))
        multipleFileUpload = self.request.get('multiple') == "true"

        if multipleFileUpload:
            existingFiles = getattr(hacker, key, [])
            value = existingFiles + newFiles
        elif len(newFiles) > 0:
            existingFile = getattr(hacker, key, None)
            if existingFile:
                blobstore.delete(existingFile)
            value = newFiles[0]
        else:
            value = None

        setattr(hacker, key, value)
        hacker_page.putHacker(hacker)

        downloadLinks = map(getDownloadLink, newFiles)
        fileNames = getFileNames(newFiles)

        self.response.write(json.dumps({"downloadLinks": downloadLinks, "fileNames" : fileNames}))
Exemple #15
0
    def post(self):
        enforce_login(self)
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]

        epub = model.ePubFile(blob = blob_info, blob_key = blob_info.key())
        epub.put()
        entry = model.LibraryEntry(epub = epub, user = get_current_session().get("account"))
        entry.put()
        
        unpacker = unpack.Unpacker()
        existing, error = unpacker.unpack(epub)

        if error is None:
            epub_key = epub.key() if existing is None else existing.key()
            logging.info("Indexing epub with key %s" % epub_key)
            taskqueue.add(queue_name = 'index', url='/index', countdown=2, params={
                'key':epub_key,
                'user':get_current_session().get("account")
            })
            epub.get_cover()
            self.redirect("/book/"+str(epub.key().id()))
        else:
            db.delete(entry)
            blobstore.delete(epub.blob.key())
            db.delete(epub)
            error = "Invalid EPUB file" if error.find("File is not a zip")>0 else error
            respondWithMessage(self, "Upload error: "+error)
Exemple #16
0
 def post(self):
     upload_files = self.get_uploads('file')  
     blob_info = upload_files[0]
     # Resize the image
     image = images.Image(blob_key=blob_info.key())
     image.resize(width=WOOF_FEED_ITEM_IMAGE_MAX_WIDTH, height=WOOF_FEED_ITEM_IMAGE_MAX_HEIGHT)
     thumbnail = image.execute_transforms(output_encoding=images.JPEG)
     # Save Resized Image back to blobstore
     file_name = files.blobstore.create(mime_type='image/jpeg')
     with files.open(file_name, 'a') as f:
         f.write(thumbnail)
     files.finalize(file_name)
     # Remove the original image
     blobstore.delete(blob_info.key())
     blob_key = files.blobstore.get_blob_key(file_name)
     # New FeedImage
     feed_image = FeedImage()
     feed_image.data = BlobInfo.get(blob_key)
     feed_image.width = image.width
     feed_image.height = image.height
     feed_image.put()
     # Create new FeedItem
     feed_item = FeedItem()
     feed_item.text = self.request.get("text")
     feed_item.image = feed_image
     feed_item.put()
     self.redirect('/')
	def get(self, student, filename):

		session = get_current_session()

		if(not loggedInAsAdmin(self)):
			return
		
		netid = session.get('netID')
		
		logging.debug("Checked for login")
		logging.debug("NetID: %s" % netid)
		logging.debug("Filename: %s" % filename)

		
		q = db.GqlQuery("SELECT * FROM MyBlobFile WHERE netID = :1 AND fileName =:2", student, urllib.unquote(filename))
		results = q.get()

		logging.debug("Past the query. Result: %s" % str(results))

		# if it exists, delete it

		if(results != None):
			resource = results.blobstoreKey
			blobstore.delete(resource.key())
			db.delete(results)

			self.redirect("/adminview/%s" % student)
		else:
			errorMsg(self, "Sorry, this file does not seem to exist or you don't have permission to delete it")
			return
Exemple #18
0
    def post(self):
	user_ = self.request.get('user')
	user_query = ndb.Key(Webusers, str(user_)).get()
	if not user_query:
	    user_query = Webusers(id=str(user_))
	    user_query.google_calendar = False
	user_query.nickname = self.request.get('nick')
	user_query.gender = self.request.get('gender')
	birthday_ = self.request.get('birthday')
	if birthday_:
	    user_query.birthday = datetime(int(birthday_[0:4]),int(birthday_[5:7]),int(birthday_[8:10]))
	user_query.introduce = self.request.get('introduce')
	#interest
	if user_query.interest:
	    user_query.interest = []
	for item in self.request.get_all('interest'):
	    user_query.interest.append(str(item))
	photo = self.get_uploads('photo')
	if photo:
	    if user_query.photo:
                blobstore.delete(urllib.unquote(user_query.photo))
	    blob_info = photo[0]
	    user_query.photo = str(blob_info.key())
	user_query.put()
	self.redirect('/my_city') 
Exemple #19
0
    def post(self, resource):
        accessAllowed = self.verifyAdminStatus(userInfo.poster, 'Delete File')
        if not accessAllowed:
            return

        try:
            # Get the FileInfo object to edit
            file, blobInfo = self.getFileAndBlobInfo(resource)
            if file == None:
                error = 'Could not find FileInfo object with provided id. Cannot delete file.'
                self.renderPage(error=error)
                return
            if blobInfo == None:
                error = 'Could not find BlobInfo object with provided id. Cannot delete file.'
                self.renderPage(error=error)
                return

            FileInfo.delete(file)
            blobstore.delete(blobInfo.key())

            time.sleep(0.2)
            self.redirect('/files')
            return
        except:
            error = 'Error: no file found with the given id. Cannot delete file.'

        self.renderPage(error=error)
Exemple #20
0
 def get(self):
     deletion_cutoff = datetime.datetime.now() - datetime.timedelta(days=14)
     for document in Document.query(
             ndb.AND(Document.deletion_time < deletion_cutoff,
                     Document.deletion_time != None)):
         blobstore.delete(document.blob_key)
         document.key.delete()
    def handleAndRelogin(self):
        if self.getUploadIsValid():
            blob_key = self.get_uploads()[0].key()
            blobstore.delete(blob_key)

        url = "/view_single?stream_key=" + self.request.get("stream_key")
        self.redirect(users.create_login_url(url))
Exemple #22
0
def generate_all():
    q=MyFile.all()
    mfiles=q.fetch(100)
    
    # delete all old files and data
    for mf in mfiles:
        bk=mf.blob_key
        blobstore.delete(bk)
        # delete data entity
        mf.delete()
    
    # dict of zipfiles being generated and their relevant generators
    generators={'openvpn.zip':generate_ovpn,
            'windows.zip':generate_win,
            'linux.zip':generate_linux,
            'mac.zip':generate_mac,
            'android.zip':generate_android
            }
    
    ip_data = fetch_ip_data()
    
    for fn,g in generators.iteritems():
        data=g(ip_data)
        z=zipit(data) #compress the data
        blob_file=files.blobstore.create('application/zip', 
                                         _blobinfo_uploaded_filename=fn)
        with files.open(blob_file,'a') as f:
            f.write(z)
        files.finalize(blob_file)
        blob_key = files.blobstore.get_blob_key(blob_file)
        mf=MyFile(name=fn,blob_key=str(blob_key))
        mf.update_date=datetime.datetime.now().date()
        mf.put()
Exemple #23
0
    def post(self):
        deleteStreamList = []
        deleteStreamList = self.request.get_all('deleteCheckbox')
        stream_query = Stream.query()
        keywordObject = Keywords.query()
        for stream in stream_query:
            if str(stream.key.id()) in deleteStreamList:
                print stream.streamName
                try:
                    for q in keywordObject:
                        q.globalKeywordsArray.remove(stream.streamName)
                        q.put()

                    for tag in stream.tags.split(' '):
                        for q in keywordObject:
                            q.globalKeywordsArray.remove(tag)
                            q.put()
                except:
                    pass

                for pic in stream.photos:
                    images.delete_serving_url(pic.blob_key)
                    blobstore.delete(pic.blob_key, rpc=None)
                stream.key.delete()
        self.redirect('/manage')
    def get(self):
        # Get the default Cloud Storage Bucket name and create a file name for
        # the object in Cloud Storage.
        bucket = app_identity.get_default_gcs_bucket_name()

        # Cloud Storage file names are in the format /bucket/object.
        filename = '/{}/blobstore_demo'.format(bucket)

        # Create a file in Google Cloud Storage and write something to it.
        with cloudstorage.open(filename, 'w') as filehandle:
            filehandle.write('abcde\n')

        # In order to read the contents of the file using the Blobstore API,
        # you must create a blob_key from the Cloud Storage file name.
        # Blobstore expects the filename to be in the format of:
        # /gs/bucket/object
        blobstore_filename = '/gs{}'.format(filename)
        blob_key = blobstore.create_gs_key(blobstore_filename)

        # Read the file's contents using the Blobstore API.
        # The last two parameters specify the start and end index of bytes we
        # want to read.
        data = blobstore.fetch_data(blob_key, 0, 6)

        # Write the contents to the response.
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(data)

        # Delete the file from Google Cloud Storage using the blob_key.
        blobstore.delete(blob_key)
Exemple #25
0
    def get(self, id, fig_key):
        user = users.get_current_user()
        if user is None:
        # go to login page
            print("View Stream Handler: Not logged in")
            self.redirect(users.create_login_page(self.request.uri))
            return



        current_stream = stream.get_by_id(int(id))
        if(not current_stream):
            self.redirect("/error/" + 'Wrong stream or page number')
            return

        flag = False
        if current_stream:
            #delete all the imgs, because they are huge
            for i in current_stream.figures:
                if str(i.blob_key) == fig_key:
                    blobstore.delete(i.blob_key)
                    current_stream.figures.remove(i)
                    flag = True
                    break

        if(not flag):
            self.redirect("/error/" + 'Designated fig does not exist')
            return
        current_stream.num_of_pics -= 1
        current_stream.put()
        time_sleep(NDB_UPDATE_SLEEP_TIME)

        return
    def handleAndRelogin(self):
        if self.getUploadIsValid():
            blob_key = self.get_uploads()[0].key()
            blobstore.delete(blob_key)

        url = "/view_single?stream_key=" + self.request.get("stream_key")
        self.redirect(users.create_login_url(url))
Exemple #27
0
    def post(self):
        """ Get fields from POST dict """

        manuscript_id = self.request.POST.get('manuscript_id')
        manuscript = bmodels.Manuscript.get_by_id(long(manuscript_id))
        upload_full_manuscript = self.get_uploads()

        if upload_full_manuscript:
            try:
                blobstore.delete(manuscript.full_manuscript_key)
            except:
                pass
            full_manuscript_key = upload_full_manuscript[0].key()
            full_manuscript_filename = upload_full_manuscript[0].filename
            full_manuscript_uploaded_on = datetime.now()
        else:
            full_manuscript_key = None

        pprint(upload_full_manuscript[0].__dict__)
        print upload_full_manuscript[0].filename
        print full_manuscript_key

        try:
            message = ''
            manuscript.full_manuscript_key = full_manuscript_key
            manuscript.full_manuscript_filename = full_manuscript_filename
            manuscript.full_manuscript_uploaded_on = full_manuscript_uploaded_on
            manuscript.put()
            message += " " + _('Full manuscript successfully uploaded.')
            self.add_message(message, 'success')

        except (AttributeError, KeyError, ValueError), e:
            logging.error('Error creating/updating manuscript: ' + e)
            message = _('Unable to create/update manuscript. Please try again later.')
            self.add_message(message, 'error')
Exemple #28
0
    def post(self):
        upload = self.get_uploads()[0]

        # Path
        root = self.session.get('root')
        path = self.request.POST.get('path')
        if path != '':
            fpath = root + '/' + path
        else:
            fpath = root

        fname = upload.filename
        fsize = round(blobstore.BlobInfo(upload.key()).size / 1000, 2)
        if fsize < 1:
            fsize = 1
        fdate = blobstore.BlobInfo(upload.key()).creation

        qry = File.query(File.path == fpath, File.name == fname)
        result = qry.fetch()
        if len(result) > 0:
            result[0].key.delete()
            blobstore.delete(result[0].blob_key)

            # Get file info from blobinfo
        file = File(name=fname,
                    path=fpath,
                    blob_key=upload.key(),
                    size=str(fsize),
                    cdate=str(fdate.strftime("%m/%d/%Y %H:%M:%S")))
        file.put()

        # self.response.write(upload.filename)
        self.redirect('/?path=' + path)
  def post(self):
    upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
    blob_info = upload_files[0]
    self.process_csv(blob_info)
 
    blobstore.delete(blob_info.key())  # optional: delete file after import
    self.redirect("/admin/loaddata")
Exemple #30
0
    def post(self):

        user = users.get_current_user()
        if not user:
            return self.redirect('/error')

        my_name = self.request.get('name').strip()
        if not my_name:
            return self.redirect('/error')

        my_image = self.request.get('image').strip()
        if not my_image:
            return self.redirect('/error')

        my_gallery_key = ndb.Key('MyGallery', user.user_id() + "-" + my_name)
        my_gallery = my_gallery_key.get()
        if not my_gallery:
            return self.redirect('/error')

        for i, image in enumerate(my_gallery.images[:]):
            if my_image != image.name:
                continue
            my_image = my_gallery.images.pop(i)
            blobstore.delete(my_image.blob)
            break

        my_gallery.put()

        self.redirect('/gallery?name=' + my_name)
 def delete(self, slash, item_id):
     item_key = ndb.Key(urlsafe=item_id)
     item = item_key.get()
     images = item.images
     for image  in images:
         blobstore.delete(image['blob_key'])
     item_key.delete()
Exemple #32
0
 def post(self):
     upload_files = self.get_uploads('file')
     blob_info = upload_files[0]
     # Resize the image
     image = images.Image(blob_key=blob_info.key())
     image.resize(width=WOOF_FEED_ITEM_IMAGE_MAX_WIDTH,
                  height=WOOF_FEED_ITEM_IMAGE_MAX_HEIGHT)
     thumbnail = image.execute_transforms(output_encoding=images.JPEG)
     # Save Resized Image back to blobstore
     file_name = files.blobstore.create(mime_type='image/jpeg')
     with files.open(file_name, 'a') as f:
         f.write(thumbnail)
     files.finalize(file_name)
     # Remove the original image
     blobstore.delete(blob_info.key())
     blob_key = files.blobstore.get_blob_key(file_name)
     # New FeedImage
     feed_image = FeedImage()
     feed_image.data = BlobInfo.get(blob_key)
     feed_image.width = image.width
     feed_image.height = image.height
     feed_image.put()
     # Create new FeedItem
     feed_item = FeedItem()
     feed_item.text = self.request.get("text")
     feed_item.image = feed_image
     feed_item.put()
     self.redirect('/')
Exemple #33
0
    def post(self):
        """Do upload post."""
        error_messages = []
        blob_info_dict = {}

        for key, value in self.request.params.items():
            if isinstance(value, cgi.FieldStorage):
                if 'blob-key' in value.type_options:
                    blob_info = blobstore.parse_blob_info(value)
                    blob_info_dict[value.name] = blob_info
                    logging.info("got blob: %s" % value.name)
                    self.store_blob(value.name, blob_info, error_messages)

        if error_messages:
            logging.error('Upload errors: %r', error_messages)
            blobstore.delete(blob_info_dict.values())
            self.response.set_status(303)
            # TODO: fix up this format
            self.response.headers.add_header(
                "Location",
                '/error?%s' % '&'.join('error_message=%s' % urllib.quote(m)
                                       for m in error_messages))
        else:
            query = ['/nonstandard/upload_complete?camliversion=1']
            query.extend('blob%d=%s' % (i + 1, k)
                         for i, k in enumerate(blob_info_dict.iterkeys()))
            self.response.set_status(303)
            self.response.headers.add_header("Location", str('&'.join(query)))
Exemple #34
0
    def post(self):
        workbook = self.request.get("workbook")
        token =  self.request.get("token")
        secret =  self.request.get("secret")
        username = self.request.get("username")
        uuid = self.request.get("uuid")
        blobkey = self.request.get("content")
        try:
            service = springpad.SpringRpcService()
            service.fetcher = springpad.OAuthFetcher(APPLICATION_KEY, APPLICATION_SECRET,access_token=oauth.OAuthToken(token,secret))
            service.set_user_context(username, uuid)
            mutator = service.get_mutator()

            blob_reader = blobstore.BlobReader(blobkey)
            parser = evernotebookparser.NotebookParser2(blob_reader)
            parser.get_items(lambda x: createItem(mutator,workbook,x))
            logging.info("Serviced a customer")
            blob_reader.close()
            blobstore.delete(blobkey)
        except Exception,e:
            logging.exception("-------%s"%e.message)
            try:
                blobstore.delete(blobkey)
            except :
                pass
    def post(self):
		
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]

	session = get_current_session()

	if(not session.is_active()):
		blob_info.delete()
		self.redirect("/login")

	mynetid = self.request.get("netid")
	myfilename = str(blob_info.filename)

	# let's check if the file reference already exists in datastore
	q = db.GqlQuery("SELECT * FROM MyBlobFile WHERE netID =:1 AND fileName =:2", mynetid, myfilename)
	results = q.get()

	# if it exists, delete the blob and then delete the datastore entry
	if(results != None):
		resource = results.blobstoreKey
		blobstore.delete(resource.key())
		db.delete(results)

	# now upload the new file

	f = MyBlobFile()
	f.netID= mynetid
	f.fileName= myfilename
	f.blobstoreKey =  blob_info.key()

	f.put()

        #self.redirect('/pub/%s' % (mynetid+"/"+myfilename))
	self.redirect('/list')
Exemple #36
0
	def post(self):
		blobimages = self.get_uploads("images")
		try:
			event = Event()
			event.name = self.request.get("name")
			event.tags = self.request.get_all("tags")
			event.description = self.request.get("description")
			event.date = datetime.datetime.now()
			event.location = db.GeoPt(self.request.get("lat"), self.request.get("lon"))
			event.links = self.request.get_all("links")
			event.event_type = self.request.get("event_type")
			event.gravity = float(self.request.get("gravity"))
			event.gravity_unit = self.request.get("gravity_unit")
			event.gravity_universal = float(self.request.get("gravity_universal"))
			# images uploading
			event.images = []
			for blobimage in blobimages:
				event.images.append(blobimage.key())
			event.put()
		except:
			# if something on save went wrong, clean the uploaded blobs
			for blobimage in blobimages:
				blobstore.delete(blobimage.key())
			raise
		# send redirect after posting, return to main page
		self.redirect("/")
Exemple #37
0
    def delete(self):
        '''Delete the photograph.

        To delete a photograph, need to delete the following:
            all comments
            all scores
            the UserComp if it exists
            the blob
            Also, if an extra photo, reduce the extra photo count by one
        '''
        all_keys = []
        if self.competition:
            user_comp = UserComp.query(
                UserComp.user == self.user,
                UserComp.comp == self.competition
            ).get()
            all_keys.append(user_comp.key)
        else:
            user = self.user.get()
            user.extra_photo_count -= 1
            user.put()
        for comment_key in Comment.query(
                Comment.photo == self.key).fetch(keys_only=True):
            all_keys.append(comment_key)
        for score_key in Scores.query(
                Scores.photo == self.key).fetch(keys_only=True):
            all_keys.append(score_key)
        all_keys.append(self.key)
        blobstore.delete(self.blob)
        ndb.delete_multi(all_keys)
Exemple #38
0
  def post(self):
    """Do upload post."""
    error_messages = []
    blob_info_dict = {}

    for key, value in self.request.params.items():
      if isinstance(value, cgi.FieldStorage):
        if 'blob-key' in value.type_options:
          blob_info = blobstore.parse_blob_info(value)
          blob_info_dict[value.name] = blob_info
          logging.info("got blob: %s" % value.name)
          self.store_blob(value.name, blob_info, error_messages)

    if error_messages:
      logging.error('Upload errors: %r', error_messages)
      blobstore.delete(blob_info_dict.values())
      self.response.set_status(303)
      # TODO: fix up this format
      self.response.headers.add_header("Location", '/error?%s' % '&'.join(
          'error_message=%s' % urllib.quote(m) for m in error_messages))
    else:
      query = ['/nonstandard/upload_complete?camliversion=1']
      query.extend('blob%d=%s' % (i + 1, k)
                   for i, k in enumerate(blob_info_dict.iterkeys()))
      self.response.set_status(303)
      self.response.headers.add_header("Location", str('&'.join(query)))
Exemple #39
0
    def post(self):
        user = users.get_current_user()
        minIndex = int(self.request.get("minIndex"))
        maxIndex = int(self.request.get("maxIndex"))
        userID = self.request.get("userID")
        query = UserScreenshot.query(UserScreenshot.index >= minIndex,
                                     UserScreenshot.index < maxIndex,
                                     ancestor=ndb.Key('UserCollection',
                                                      userID)).order(
                                                          UserScreenshot.index)

        results = query.fetch(None)
        images = []
        for entry in results:
            blob_key = entry.blob_key
            blobstore.get(blob_key)
            reader = blobstore.BlobReader(blob_key)
            image = Image.open(reader)
            image.load()
            images.append(image)

        p = ScreenshotParser()
        cards = p.getCardsFromImages(images)
        for i in range(0, len(results)):
            # Bit of a fudge here, could get the ScreenshotParser to give us how many cards per image
            results[i].cards = ""
            for j in range(i * 8, min(i * 8 + 8, len(cards))):
                results[i].cards += str(cards[j].toDict()) + "\n"
            results[i].cards = results[i].cards[:-1]  # Remove last newline
            blobstore.delete(results[i].blob_key)
            results[i].processed = True
            results[i].put()
        return
Exemple #40
0
    def delete(self, **kwargs):
        #Check that photo exists
        if 'pid' in kwargs:
            photo = ndb.Key(db_defs.Photos, int(kwargs['pid'])).get()
            if not photo:
                self.response.set_status(404)
                self.response.write('Photo not found\n')
                return

            #removes all references to photo from any Day object
            q = db_defs.Day.query().fetch()
            
            for d in db_defs.Day.query().fetch():
                for p in d.photos:
                    if photo.key == p:
                        d.photos.remove(p)
                        d.put()

            #delete blobstore blob            
            blobstore.delete(photo.photo)
            #delete comments associated with photo
            ndb.delete_multi(ndb.Query(ancestor=photo.key).iter(keys_only=True))
            #delete photo
            photo.key.delete()
            
            #gather photos to show deletion
            q = db_defs.Photos.query()
            keys = q.fetch(keys_only=True)
            results = {'keys': [x.id() for x in keys]}
            self.response.headers['Content=Type'] = 'application/json'
            self.response.write(json.dumps(results))
            
        return
Exemple #41
0
 def get(self):
     
     #get specific trip     
     trip_key = self.request.get('trip_id')
     tripk = ndb.Key(urlsafe=trip_key)
     trip = tripk.get()
             
     #get tracks for trip
     track_query = Track.query(ancestor=tripk)
     tracks = track_query.fetch()
     for track in tracks:
         #delete .gpx files from blobstore
         bolob = track.blob_key
         blobstore.delete(bolob)
         #delete statistic
         for item in TrackStatistic.query(ancestor=track.key):
             item.key.delete()
         
         #delete track
         track.key.delete()
     
     #redirect to mytrips because showing all tips will only be consistent in scope of user
     # and only eventually consistent for whole datastore  
     trip.key.delete()
     
     self.redirect('/tripmanager')
Exemple #42
0
 def post(self):
     keys = self.request.POST['keys']
     if not keys:
         return
     keys = [k.strip() for k in keys.split(',')]
     blobstore.delete(keys)
     self.emit_json({})
Exemple #43
0
 def delete(self):
     key = self.request.get('key') or ''
     blobstore.delete(key)
     s = json.dumps({key: True}, separators=(',', ':'))
     if 'application/json' in self.request.headers.get('Accept'):
         self.response.headers['Content-Type'] = 'application/json'
     self.response.write(s)
        def _delete_model_blobs(self, model):
            """Deletes all blobs associated with the model (finds all BlobKeyProperty)"""

            for (name, prop) in model._properties.iteritems():
                if isinstance(prop, ndb.BlobKeyProperty):
                    if getattr(model, name):
                        blobstore.delete(getattr(model, name))
Exemple #45
0
 def _pre_delete_hook(c, k):
     self = k.get()
     blobstore.delete([self.smallfile, self.largefile])
     ndb.delete_multi(
         c.query(
             ndb.OR(c.kusr == self.key, c.kart == self.key,
                    c.kitm == self.key)).fetch(keys_only=True))
    def get(self):
        # Get the default Cloud Storage Bucket name and create a file name for
        # the object in Cloud Storage.
        bucket = app_identity.get_default_gcs_bucket_name()

        # Cloud Storage file names are in the format /bucket/object.
        filename = '/{}/blobstore_demo'.format(bucket)

        # Create a file in Google Cloud Storage and write something to it.
        with cloudstorage.open(filename, 'w') as filehandle:
            filehandle.write('abcde\n')

        # In order to read the contents of the file using the Blobstore API,
        # you must create a blob_key from the Cloud Storage file name.
        # Blobstore expects the filename to be in the format of:
        # /gs/bucket/object
        blobstore_filename = '/gs{}'.format(filename)
        blob_key = blobstore.create_gs_key(blobstore_filename)

        # Read the file's contents using the Blobstore API.
        # The last two parameters specify the start and end index of bytes we
        # want to read.
        data = blobstore.fetch_data(blob_key, 0, 6)

        # Write the contents to the response.
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(data)

        # Delete the file from Google Cloud Storage using the blob_key.
        blobstore.delete(blob_key)
Exemple #47
0
def doCleanupDeletedFiles(cursor = None):
	maxIterCount = 2  # How often a file will be checked for deletion
	gotAtLeastOne = False
	query = db.Query("viur-deleted-files").cursor( cursor )
	for file in query.run(100):
		gotAtLeastOne = True
		if not "dlkey" in file.keys():
			db.Delete(file.key())
		elif db.Query("viur-blob-locks").filter("active_blob_references =", file["dlkey"]).get():
			logging.info("is referenced, %s" % file["dlkey"])
			db.Delete(file.key())
		else:
			if file["itercount"] > maxIterCount:
				logging.info("Finally deleting, %s" % file["dlkey"])
				blobstore.delete(file["dlkey"])
				db.Delete(file.key())
				# There should be exactly 1 or 0 of these
				for f in db.Query("file").filter("dlkey =", file["dlkey"]).iter(keysOnly=True):
					db.Delete(f)
			else:
				logging.error("Increasing count, %s" % file["dlkey"])
				file["itercount"] += 1
				db.Put(file)
	newCursor = query.getCursor()
	if gotAtLeastOne and newCursor and newCursor.urlsafe() != cursor:
		doCleanupDeletedFiles(newCursor.urlsafe())
	def deleteUser(self):
				
		# We first have to remove the user's image from the blobstore, the Moodstocks DB and his reference in the Datastore
		image = self.user.imageRef
		if(image):
			imageID = str(image.key().id())
			
			# We delete the image from Moodstocks DB
			moodstocksHandler = Moodstocks()
			moodstocksHandler.deleteObject(imageID)
			
			# We delete the image in the blobstore
			blobInfo = image.blobKey
			blobKey = blobInfo.key()
			blobstore.delete(blobKey)
			
			# We can now remove the image reference
			image.delete() 
		
		# We first remove the vcard
		vcard = self.user.vcardRef
		if(vcard):
			vcard.delete()
		
		self.user.delete()
		logging.info("User has been removed from Captur.io")
		path = os.path.join(os.path.dirname(__file__), 'templates/delete/information_removed.html')
		self.response.out.write(template.render(path, None))
Exemple #49
0
def delete_user(user_id, anonymous=False):
  """Delete the User and associated pic, UserQuestions and UserSeasons."""

  cls = models.AnonymousUser if anonymous else models.User
  user = cls.get_by_id(user_id)

  if not user:
    return

  user_key = user.key

  to_delete = [user_key]

  # Delete UserQuestion.
  user_question_query = models.UserQuestion.query(
    models.UserQuestion.user == user_key)
  for user_question_key in user_question_query.iter(keys_only=True):
    to_delete.append(user_question_key)

  # Delete UserSeason.
  if not anonymous:
    user_season_query = models.UserSeason.query(
      models.UserSeason.user == user_key)
    for user_season_key in user_season_query.iter(keys_only=True):
      to_delete.append(user_season_key)

  ndb.delete_multi(to_delete)

  # Delete the user pic.
  if user.pic:
    blobstore.delete(user.pic)
Exemple #50
0
    def attachments_delete(self, request):
        """Remove metainfo for a single attachments for a timeline card"""

        current_user = endpoints.get_current_user()
        if current_user is None:
            raise endpoints.UnauthorizedException("Authentication required.")

        card = ndb.Key("TimelineItem", request.itemId).get()

        if card is None or card.user != current_user:
            raise endpoints.NotFoundException("Attachment not found.")

        if card.attachments is not None:
            for att in card.attachments:
                if att.id == request.attachmentId:
                    # Delete attachment from blobstore
                    blobkey = blobstore.BlobKey(request.attachmentId)
                    blobstore.delete(blobkey)

                    # Remove attachment from timeline card
                    card.attachments.remove(att)
                    card.put()

                    return AttachmentResponse(id=att.id)

        raise endpoints.NotFoundException("Attachment not found.")
Exemple #51
0
def delete_bucket_file(bucket_key):
    try:
        blobstore.delete(bucket_key)
        return True
    except Exception, e:
        logging.error(e)
        return False
Exemple #52
0
def paper_upload_view():
	# big thanks to Stack Overflow user Koffee for explaining how this works
	# http://stackoverflow.com/a/18073466/2752467
	f = request.files['file']
	header = f.headers['Content-Type']
	parsed_header = parse_options_header(header)
	blob_key = blobstore.BlobKey(parsed_header[1]['blob-key'])
	
	paper_key = request.args.get("id")
	paper = ndb.Key(urlsafe=paper_key).get()

	blob_stream = blobstore.BlobReader(blob_key)
	if not is_pdf(blob_stream):
		blob_stream.close()
		blobstore.delete(blob_key)
		return redirect(url_rule["paper"] + "?id=" + paper_key
			+ "&ispdf=false")
	blob_stream.close()

	# sanity check: file is associated with a paper owned by the current user
	if paper and paper.author == \
		lookup_user(users.get_current_user().user_id()).key:
		if paper.file != None:
			# prevent old versions of file from being orphaned
			blobstore.delete(paper.file)
		paper.file = blob_key
		paper.put()
	
	# paper_view_get() will handle most error scenarios
	return redirect(url_rule["paper"] + "?id=" + paper_key
		+ "&update=success")
Exemple #53
0
    def get(self):
        confirm = self.request.get('confirm')
        if confirm != "true":
            return
        epub_key = self.request.get('key')
        epub = db.get(epub_key)
        account = get_current_session().get("account")
        entry = model.LibraryEntry.all().filter("epub = ", epub).filter(
            "user ="******"private", "public"]:
                    index = search.Index(indexName)
                    opts = search.QueryOptions(limit=1000, ids_only=True)
                    query = search.Query(query_string="book:%s" % epub_key,
                                         options=opts)
                    docs = index.search(query)
                    for doc in docs:
                        index.remove(doc.doc_id)
                blobstore.delete(epub.blob.key())
                db.delete(epub)

            self.redirect('/list')
        else:
            self.response.out.write("Not permitted")
Exemple #54
0
    def post(self):
        enforce_login(self)
        upload_files = self.get_uploads(
            'file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]

        epub = model.ePubFile(blob=blob_info, blob_key=blob_info.key())
        epub.put()
        entry = model.LibraryEntry(epub=epub,
                                   user=get_current_session().get("account"))
        entry.put()

        unpacker = unpack.Unpacker()
        existing, error = unpacker.unpack(epub)

        if error is None:
            epub_key = epub.key() if existing is None else existing.key()
            logging.info("Indexing epub with key %s" % epub_key)
            taskqueue.add(queue_name='index',
                          url='/index',
                          countdown=2,
                          params={
                              'key': epub_key,
                              'user': get_current_session().get("account")
                          })
            epub.get_cover()
            self.redirect("/book/" + str(epub.key().id()))
        else:
            db.delete(entry)
            blobstore.delete(epub.blob.key())
            db.delete(epub)
            error = "Invalid EPUB file" if error.find(
                "File is not a zip") > 0 else error
            respondWithMessage(self, "Upload error: " + error)
	def post(self):
		upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
		blob_info = upload_files[0]
		Process_CSV(blob_info)

		blobstore.delete(blob_info.key())  # optional: delete file after import
		self.redirect('/')
Exemple #56
0
	def POST(self, request):
		request = web.ctx.path
		data = web.data()
		s = data.find('blob-key=')
		if s < 0:
			raise web.BadRequest()
		s += 10
		e = data.find('"', s)
		if e < 0:
			raise web.BadRequest()
		data = data[s:e]
		if not data:
			raise web.BadRequest()
		if not blobstore.BlobInfo.get(data):
			# The blobKey does not exist
			raise web.NotFound()
		meta = _StoreMeta.get_by_key_name(request)
		if meta:
			# Delete the existing blob
			blobstore.delete(meta.blobKey)
		meta = _StoreMeta(key_name=request)
		meta.blobKey = data
		logging.warning('BlobKey: %s' % meta.blobKey)
		meta.put()
		# This redirection is empty and useless,
		# required from the appengine SDK...
		raise web.HTTPError(status='302 Found')
Exemple #57
0
 def admin_delete(self, key):
     try:
         item = self.util.decode_key(key).get()
         file_key = item.file
         blobstore.delete(file_key)
     except:
         pass
     return scaffold.delete(self, key)