def add_new_item(name, item_parent_id, db=None, data=None): try: print(f'add new item {name} {item_parent_id}') # add to item table is_dir = True if data == None else False size = len(data) if data else 0 new_item_id = Item.add_item(name=name, size=size, is_dir=is_dir) # add to file table if data: File.add_item(file_id=new_item_id, data=data) # add to ancestors table Ancestors.add_item(child_id=new_item_id, parent_id=item_parent_id) # update item_parent_id size parent_item = db.session.query(Item).filter( Item.id == item_parent_id).first() if parent_item: parent_item.update_item_size(size=parent_item.size + 1) # Item(id=parent_item.id, # name=parent_item.name, # created_at=parent_item.created_at, # updated_at=parent_item.updated_at, # size=parent_item.size, # is_dir=parent_item.is_dir).update_item_size(parent_item.size + 1) db.session.add(parent_item) return new_item_id except Exception as e: db.session.rollback() raise e
def generate_files(self): """ generate_data method generate random file data so we can map it """ # generate 500 file records total_records = 500 messagelog = [] for i in range(total_records): # create new file entity download_count = random.randint(1, 10) file_name = uuid.uuid4() new_file = File() new_file.name = str(file_name) + '.txt' new_file.download_count = download_count new_file.put() messagelog.append("File added: {0} - {1}".format(i, new_file.name)) context = {'messagelog': messagelog} self.render_response('file.html', context=context)
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 delete(cls, _id): try: obj = Model() obj.set_id(_id) obj.remove() return True except Exception, err: return err.message
def save_fileinfo_to_db(username, file_id, content_type, file_length): """Save info about file into DB """ user = User.get(User.username == username) File.create(filename=file_id, type=content_type, user=user, length=file_length)
def create_file(cls, name, hash, user): file = cls.find_file_by_hash(hash) if file is None: # Create new file newFile = File(name = name, hash = hash, session_id = user.session_id) newFile.save() else: # Update file name cls.update_file(file, name)
def create_file(cls, name, hash, user): mongodbmanager.connect() file = cls.find_file_by_hash_and_sessionid(hash, user.session_id) if file is None: # Create new file newFile = File(name = name, hash = hash, session_id = user.session_id) newFile.save() else: # Update file name cls.update_file(file, name)
def load_my_files(cls): peer_me = Peer.get_local_peer() for dirname, dirnames, filenames in os.walk(SHARED_PATH): for filename in filenames: path = dirname + "/" + filename file = File(hashing.generate_file_id(), filename,path) #Set that I have all this file for p in range(0, file.parts_count): file.set_peer_has_part(peer_me, p, True) cls.get_files().append(file)
def create_file(): file_dictionary = request.json try: cloud_response = upload(file_dictionary['content'], folder=f'/share-app-{os.getenv("FLASK_ENV")}') file = File(url=cloud_response['secure_url'], cloud_id=cloud_response['asset_id'], user_id=g.current_user.id) except ValidationError as e: return {'errors': e.messages, 'messages': 'Something went wrong'} file.save() return file_schema.jsonify(file), 200
def get(self, id=None): if id is not None: file = File.get_file(id) return file, 200 else: try: current_user = flask_praetorian.current_user() files = File.get_all_files(current_user.id) ret = {"files": files} return ret, 200 except: return 500 ret = {"message": "Error"} return ret, 400
def move(data: dict, user: str, device: Device, file: File) -> dict: new_filename = data["new_filename"] new_parent_dir_uuid = data["new_parent_dir_uuid"] target_file: Optional[File] = (wrapper.session.query(File).filter_by( device=device.uuid, filename=new_filename, parent_dir_uuid=new_parent_dir_uuid).first()) if target_file is not None: return file_already_exists target_dir: Optional[File] = (wrapper.session.query(File).filter_by( device=device.uuid, is_directory=True, uuid=new_parent_dir_uuid).first()) if target_dir is None and new_parent_dir_uuid is not None: return parent_directory_not_found if file.is_directory: parent_to_check: Optional[File] = ( wrapper.session.query(File).filter_by( device=device.uuid, uuid=new_parent_dir_uuid).first()) if parent_to_check is not None: while parent_to_check is not None: if parent_to_check.uuid == file.uuid: return can_not_move_dir_into_itself parent_to_check: Optional[File] = ( wrapper.session.query(File).filter_by( device=device.uuid, uuid=parent_to_check.parent_dir_uuid).first()) file.filename = new_filename file.parent_dir_uuid = new_parent_dir_uuid wrapper.session.commit() m.contact_user( user, { "notify-id": "file-update", "origin": "update", "device_uuid": device.uuid, "data": { "created": [], "deleted": [], "changed": [file.uuid] }, }, ) return file.serialize
def get_file_info(self, request): file_path = request.get_url().decode() file_path = urllib.parse.unquote(file_path, encoding='utf-8', errors='replace') if len(file_path.split('../')) > 1: raise ForbidenError if file_path[-1:] == '/': file = self.files_root + file_path + "index.html" else: file = self.files_root + file_path if file.split('.')[-1:][0] in self.content_types: content = self.content_types[file.split('.')[-1]] else: content = '' if not os.path.isfile(file): if file_path[-1:] == '/' and file_path.count(".") < 1: raise ForbidenError else: raise NotFoundError return File(filename=file, file_path=file_path, content_type=content, content_length=os.path.getsize(file))
def test__model__file__serialize(self): file = File( uuid="file identifier", device="the device", filename="some_file.txt", content="hello world", parent_dir_uuid="some_other_identifier", is_directory=False, is_changeable=False, ) expected_result = { "uuid": "file identifier", "device": "the device", "filename": "some_file.txt", "content": "hello world", "parent_dir_uuid": "some_other_identifier", "is_directory": False, "is_changeable": False, } serialized = file.serialize self.assertEqual(expected_result, serialized) serialized["content"] = "hi!" self.assertEqual(expected_result, file.serialize)
def post(self): artistKeyString = self.request.get('editArtistKey') artistID = int(artistKeyString) firstName = self.request.get('editFirstName') lastName = self.request.get('editLastName') biography = self.request.get('editBiography') photoName = self.request.get('editPhotoName') #get the photo specified by the user photo = File.query(File.file_name == photoName.upper()).get() #get the artist based on the key and update all fields artist = Artist.get_by_id(artistID) artist.biography = biography artist.firstName = firstName artist.lastName = lastName artist.picture = photo.key artist.uploaded_by = users.get_current_user() artist.put() artist.add_to_search_index() message = "Successfully updated artist record: " + artist.firstName + " " + artist.lastName self.response.write(message)
def post(self): categoryName = self.request.get('categoryName') photoName = self.request.get('photoName') #get the photo specified by the user photo = File.query(File.file_name==photoName.upper()).get() #check to see if a category with that name already exists existingCategory = Category.query(Category.categoryName==categoryName).get() if existingCategory: #if an category with that name already exists, then update the information with the form data existingCategory.categoryName=categoryName existingCategory.picture=photo.key existingCategory.uploaded_by=users.get_current_user() existingCategory.put() existingCategory.add_to_search_index() message = "Successfully updated category record: " + existingCategory.categoryName else: #add a new category entry if no category with that name already exists category = Category(categoryName=categoryName, picture=photo.key, uploaded_by=users.get_current_user()) category.put() category.add_to_search_index() message = "Successfully created category record: " + category.categoryName self.response.write(message)
def process_csv(): token_result = validate_jwt(request.headers.get('Authorization')) if token_result == 401: return "Unauthorized", 401 if request.method == 'GET': result = engine.execute( """ SELECT id, filename, keywords, created, (SELECT COUNT(*) >= f.keywords FROM data WHERE file_id=f.id) AS status FROM file f WHERE user_id=%s ORDER BY created DESC; """, [token_result["sub"]]) converted_result = [list(row) for row in result] if converted_result: return jsonify(converted_result), 200 return "Not Found", 404 elif request.method == 'POST': if not find_user(token_result['sub']): return "Unauthorized", 401 request_body = request.json new_file = File(user_id=token_result["sub"], filename=request_body["filename"], keywords=len(request_body["keywords"])) db_session.add(new_file) db_session.commit() for keyword in request_body["keywords"]: scrape_data_from_google.apply_async(args=[new_file.id, keyword]) return "Upload Completed", 200
def post(self): artistKeyString = self.request.get('editArtistKey') artistID = int(artistKeyString) firstName = self.request.get('editFirstName') lastName = self.request.get('editLastName') biography = self.request.get('editBiography') photoName = self.request.get('editPhotoName') #get the photo specified by the user photo = File.query(File.file_name==photoName.upper()).get() #get the artist based on the key and update all fields artist = Artist.get_by_id(artistID) artist.biography=biography artist.firstName=firstName artist.lastName=lastName artist.picture=photo.key artist.uploaded_by=users.get_current_user() artist.put() artist.add_to_search_index() message = "Successfully updated artist record: " + artist.firstName + " " + artist.lastName self.response.write(message)
def post(self): firstName = self.request.get('firstName') lastName = self.request.get('lastName') biography = self.request.get('biography') photoName = self.request.get('photoName') #get the photo specified by the user photo = File.query(File.file_name==photoName.upper()).get() #check to see if a artist with that name already exists existingArtist = Artist.query(Artist.firstName==firstName and Artist.lastName==lastName).get() if existingArtist: #if an artist with that name already exists, then update the information with the form data existingArtist.biography=biography existingArtist.firstName=firstName existingArtist.lastName=lastName existingArtist.picture=photo.key existingArtist.uploaded_by=users.get_current_user() existingArtist.put() existingArtist.add_to_search_index() message = "Successfully updated artist record: " + existingArtist.firstName + " " + existingArtist.lastName else: #add a new artist entry if no artist with that name already exists artist = Artist(biography=biography, firstName=firstName, lastName=lastName, picture=photo.key, uploaded_by=users.get_current_user()) artist.put() artist.add_to_search_index() message = "Successfully created artist record: " + artist.firstName + " " + artist.lastName self.response.write(message)
def post(self): categoryName = self.request.get('categoryName') photoName = self.request.get('photoName') #get the photo specified by the user photo = File.query(File.file_name == photoName.upper()).get() #check to see if a category with that name already exists existingCategory = Category.query( Category.categoryName == categoryName).get() if existingCategory: #if an category with that name already exists, then update the information with the form data existingCategory.categoryName = categoryName existingCategory.picture = photo.key existingCategory.uploaded_by = users.get_current_user() existingCategory.put() existingCategory.add_to_search_index() message = "Successfully updated category record: " + existingCategory.categoryName else: #add a new category entry if no category with that name already exists category = Category(categoryName=categoryName, picture=photo.key, uploaded_by=users.get_current_user()) category.put() category.add_to_search_index() message = "Successfully created category record: " + category.categoryName self.response.write(message)
def setUp(self): super().setUp() self.user_id = 1 self.email = "[email protected]" self.password = "******" self.file_id = 1 self.filename = "test-file.csv" self.keywords = 1 self.data_id = 1 self.keyword = "test-keyword" self.total_adword = 1 self.total_link = 1 self.total_search_result = "about 1,000" self.html_code = "test-html-code" self.new_user = User(id=self.user_id, email=self.email, password=self.password) self.db_session.add(self.new_user) self.db_session.commit() self.new_file = File(user_id=self.user_id, id=self.file_id, filename=self.filename, keywords=self.keywords) self.db_session.add(self.new_file) self.db_session.commit()
def update(data: dict, user: str, device: Device, file: File) -> dict: """ Update the content of a file. :param data: The given data. :param user: The user uuid. :param device: The device of the file. :param file: The file. :return: The response """ if file.is_directory: return directories_can_not_be_updated file.content = data["content"] wrapper.session.commit() m.contact_user( user, { "notify-id": "file-update", "origin": "update", "device_uuid": device.uuid, "data": { "created": [], "deleted": [], "changed": [file.uuid] }, }, ) return file.serialize
def get(self): #sort the art by artist name and then by artpiece name photos = File.query().order(File.file_name) html = "" for photo in photos: html+="""<li class="span3"> <div class="thumbnail"> <img data-src="holder.js/300x200" alt="300x200" style="width: 300px; height: 200px;" src=""" + photo.url + """> <div class="caption"> <div class="artpieceName"> <h5>""" + photo.file_name + """</h5> <a data-toggle="modal" href="#editPhotoModal" onclick="fillEditPhotoModalDefaults(""" + str(photo.key.id()) + """,'""" + photo.file_name + """');" class="btn btn-medium"> <span class="glyphicon icon-edit"></span> </a> <a data-toggle="modal" data-id=""" + str(photo.key.id()) + """ href="#deletePhotoModal" class="open-deletePhotoModal btn btn-medium"> <span class="glyphicon icon-remove"></span> </a> </div> </div> </div> </li>""" self.response.write(html)
def post(self): fileKeyString = self.request.get('editPhotoKey') newPhotoName = self.request.get('editPhotoName').upper() fileToEdit = File.get_by_id(int(fileKeyString)) #check if another file with that name already exists oldFile= File.query(File.file_name==newPhotoName).get() if oldFile: message = "ERROR: A file with that name already exists" else: #Find and update the file name of the current file(not possible to do on the actual blob) fileToEdit.file_name = newPhotoName fileToEdit.put() message = "Successfully updated photo name: " + fileToEdit.file_name self.response.write(message)
def setUp(self): super().setUp() self.user_id = 1 self.email = "[email protected]" self.password = "******" self.file_id = 1 self.filename = "test-file.csv" self.keywords = 1 self.new_user = User( id = self.user_id, email = self.email, password = self.password ) self.db_session.add(self.new_user) self.db_session.commit() self.new_file = File( user_id = self.user_id, id = self.file_id, filename = self.filename, keywords = self.keywords ) self.db_session.add(self.new_file) self.db_session.commit()
def post(self): fileKeyString = self.request.get('editPhotoKey') newPhotoName = self.request.get('editPhotoName').upper() fileToEdit = File.get_by_id(int(fileKeyString)) #check if another file with that name already exists oldFile = File.query(File.file_name == newPhotoName).get() if oldFile: message = "ERROR: A file with that name already exists" else: #Find and update the file name of the current file(not possible to do on the actual blob) fileToEdit.file_name = newPhotoName fileToEdit.put() message = "Successfully updated photo name: " + fileToEdit.file_name self.response.write(message)
def get(self): #sort the art by artist name and then by artpiece name photos = File.query().order(File.file_name) html = "" for photo in photos: html += """<li class="span3"> <div class="thumbnail"> <img data-src="holder.js/300x200" alt="300x200" style="width: 300px; height: 200px;" src=""" + photo.url + """> <div class="caption"> <div class="artpieceName"> <h5>""" + photo.file_name + """</h5> <a data-toggle="modal" href="#editPhotoModal" onclick="fillEditPhotoModalDefaults(""" + str( photo.key.id() ) + """,'""" + photo.file_name + """');" class="btn btn-medium"> <span class="glyphicon icon-edit"></span> </a> <a data-toggle="modal" data-id=""" + str( photo.key.id() ) + """ href="#deletePhotoModal" class="open-deletePhotoModal btn btn-medium"> <span class="glyphicon icon-remove"></span> </a> </div> </div> </div> </li>""" self.response.write(html)
def get(self): photos = File.query().order(File.file_name) self.render_template("/templates/fileUpload.html", { "title": "Manage Photos", 'form_url': blobstore.create_upload_url('/admin/photos/upload'), 'logout_url': users.create_logout_url('/'), 'photos' : photos })
def get(self, file_id): file_id = self.request.get('photoID') file = File.get_by_id(long(file_id)) if not blobstore.get(file.blob): self.error(404) else: self.send_blob(file.blob)
def upload_file(current_user,curent_preference,filename,db): if File.query.filter_by(file_name=str( Constants.cloud_script_folder_path(current_user, curent_preference) + filename)).first() is None: file = File( file_name=str(Constants.cloud_script_folder_path(current_user, curent_preference) + filename)) db.session.add(file) db.session.commit() preference_file = Preference_file(file_id=file.id, preference_id=curent_preference.preference_id) db.session.add(preference_file) db.session.commit()
def get(self): photos = File.query().order(File.file_name) self.render_template( "/templates/fileUpload.html", { "title": "Manage Photos", 'form_url': blobstore.create_upload_url('/admin/photos/upload'), 'logout_url': users.create_logout_url('/'), 'photos': photos })
def get_file_info(self, request): file_path = self.check_last_slash(request.url) self.check_dots(file_path) file_path = self.try_decode(file_path) full_file_path = os.path.join(self._files_root, file_path) self._log.debug("full_file_path: " + full_file_path) content_type = self.get_content_type(file_path) return File(full_file_path, content_type)
def get(self, user_id): """ A list of files that belong to a particular user """ try: return orm_file.select('uuid', 'name', 'mime', 'created_at', 'updated_at').where( 'user_id', '=', user_id).get().serialize(), 200 except ModelNotFound: API.abort(404)
def test__model__file__create(self): actual_result = File.create("my-device", "foo.bar", "super", "baz", False) self.assertEqual("my-device", actual_result.device) self.assertEqual("foo.bar", actual_result.filename) self.assertEqual("baz", actual_result.parent_dir_uuid) self.assertEqual(False, actual_result.is_directory) self.assertRegex(actual_result.uuid, r"[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}") mock.wrapper.session.add.assert_called_with(actual_result) mock.wrapper.session.commit.assert_called_with()
def post(self): try: current_user = flask_praetorian.current_user() data = request.get_json() article_title = data["title"] article_url = data["fileUrl"] article_id = str(uuid4()) article_filename = secure_filename(f"{article_id}.pdf") article_path = f"/tmp/{article_filename}" pdf = weasyprint.HTML(article_url).write_pdf() open(article_path, "wb").write(pdf) artice_doc = fitz.open(article_path) preview_page = artice_doc.load_page(0) preview_pixmap = preview_page.get_pixmap() preview_filename = secure_filename(f"{article_id}.png") preview_path = f"/tmp/{preview_filename}" preview_image = open(preview_path, "w") artice_preview = preview_pixmap.writePNG(preview_path) storage_client.upload_file( Filename=article_path, Bucket=bucket_name, Key=article_filename, ) storage_client.upload_file(Filename=preview_path, Bucket=bucket_name, Key=preview_filename) article = File( title=article_title, user_id=current_user.id, file_url= f"https://ec500-news-analyzer.s3.us-east-2.amazonaws.com/{article_filename}", preview_url= f"https://ec500-news-analyzer.s3.us-east-2.amazonaws.com/{preview_filename}", ) db.session.add(article) db.session.commit() ret = {"message": "OK"} return ret, 200 except: ret = {"message": "Server Error"} return ret, 500 ret = {"message": "Error"} return ret, 400
def post(self): fileKeyString = self.request.get('deletePhotoKey') #generate message file = File.get_by_id(int(fileKeyString)) message = "Successfully deleted photo: " + file.file_name #delete file blobstore.delete(file.blob) key = file.key key.delete() self.response.write(message)
def conv(id): settings = {} for key, value in request.values.items(): settings[key] = value if value.isdigit(): settings[key] = int(value) if value == 'true': settings[key] = True if value == 'false': settings[key] = False extension = settings['extension'] file = File.query.get(id) path = file.path basename = Tools.get_basename(file.name) new_name = (basename + '.' + extension).encode('utf-8') document = Document(path) properties = document.get_export_options(extension) properties['FilterData'] = settings document.set_properties(properties) # return jsonify({ # 'id': id, # 'settings': settings, # 'options': options, # 'export_options': document.get_export_options(extension), # 'arguments': dir(document.arguments), # 'statistics': document.get_statistics(), # 'get_settings': file.get_settings() # }) new = path + '.' + extension save = document.save(new) if save is False: return jsonify({ 'error': 'File not saved' }) type = mimetypes.guess_type(urllib.pathname2url(new_name)) model = File.query.filter_by(path=new).first() if model is None: model = File() model.user_id = g.user.id model.name = new_name.decode('utf-8') model.path = new model.size = os.path.getsize(new) model.type = type[0] model.extension = extension db.session.add(model) db.session.commit() return jsonify({ 'save': save, 'name': new_name, 'url': '/download/' + str(model.id), 'type': type[0], 'path': new, 'properties': properties })
def run(self): gallery = Gallery().search(tgid = self.chat_id) if gallery: newfile = self.bot.getFile(self.update.message.document.file_id) file_name = self.update.message.document.file_id newfile.download(file_name) writed = False if os.path.exists(file_name): writed = write_file(file_name, read_file(file_name, storage = 'local', append_path = False), acl = 'public-read', mime_type = self.update.message.document.mime_type) thumbnail(file_name) os.remove(file_name) write_file('%s.json' % file_name, self.update.to_json()) if writed: file_id = File(gallery_eid = gallery.eid.value, file_id = self.update.message.document.file_id) file_id.save() sendLink = getattr(gallery, 'sendLink', None) if sendLink and sendLink.value: self.text = 'File URL: %s' % url_for('image', file_id = file_id.eid.value, _external = True, disable_web_page_preview = True) else: self.text = 'Failed to download file' else: self.text = 'Gallery does not exist, please create first'
def find_files_by_query(cls, query): mongodbmanager.connect() files = File.objects(name__icontains = query) #do manual distinct returning_files = [] returning_files_hashes = [] for f in files: if f.hash not in returning_files_hashes: returning_files.append(f) returning_files_hashes.append(f.hash) return returning_files
def get(self): template = JINJA_ENVIRONMENT.get_template( '/templates/publicArtPiece.html') itemNumber = self.request.get('itemNumber') artpiece = ArtPiece.query(ArtPiece.itemNumber == itemNumber).get() photo = File.query(File.key == artpiece.picture).get() artist = Artist.query(Artist.key == artpiece.artist).get() #create a comma separated string of categories categories = ndb.get_multi(artpiece.categories) categoryNamesList = [] for category in categories: categoryNamesList.append(str(category.categoryName)) categoriesString = ",".join(categoryNamesList) #check for additional sizes if this is a master or a slave piece additionalPieces = [] if artpiece.masterArtFlag: slavePieces = ArtPiece.query( ArtPiece.masterArtPiece == artpiece.key) for slavepiece in slavePieces: additionalPieces.append(slavepiece) if artpiece.slaveArtFlag: masterArtPiece = ArtPiece.query( ArtPiece.key == artpiece.masterArtPiece).get() otherSlavePieces = ArtPiece.query( ArtPiece.masterArtPiece == masterArtPiece.key, ArtPiece.key <> artpiece.key) additionalPieces.append(masterArtPiece) for slavepiece in otherSlavePieces: additionalPieces.append(slavepiece) #generate the appropriate html to link to these other pieces if they exist additionalSizes = "" if len(additionalPieces) > 0: additionalSizes += "<h5>Also Available in Other Sizes:</h5><ul>" for additionalPiece in additionalPieces: additionalSizes += "<a href=\"/artpiece?itemNumber=" + additionalPiece.itemNumber + "\"><li>" + additionalPiece.name + " (" + additionalPiece.priceDisplay + ")</li></a>" additionalSizes += "</ul>" templateVars = { "title": artpiece.name, "additionalSizes": additionalSizes, "artpiece": artpiece, "artist": artist, "photo": photo, "categories": categoriesString } self.response.write(template.render(templateVars))
def find(cls, _filter={}, page=-1, per_page=15, sort='personnel_id', order=1): try: obj = Model.objects(__raw__=_filter) result = [] for item in obj: result.append({ 'id': obj.id, 'kootaj': obj.kootaj, 'desc': obj.desc, }) return result except Exception, err: return err.message
def getTorrentsByCategory(self, category: str) -> List[Torrent]: torrents = [] for torrent in self._client.torrents_info(category=category, sort='added_on', reverse=True): # only get complete torrents if not torrent.state_enum.is_complete: continue files = [] for file in torrent.files: files.append(File(file['name'])) newTorrent = Torrent(torrent['hash'], torrent['name'], torrent['category'], torrent['save_path'], files) torrents.append(newTorrent) return torrents
def create_file(data: dict, user: str, device: Device) -> dict: """ Create a new file. :param data: The given data. :param user: The user uuid. :param device: The device of the file. :return: The response """ filename: str = data["filename"] content: str = data["content"] is_directory: bool = data["is_directory"] parent_dir_uuid: str = data["parent_dir_uuid"] file_count: int = (wrapper.session.query(func.count( File.uuid)).filter_by(device=device.uuid, filename=filename, parent_dir_uuid=parent_dir_uuid).scalar()) if file_count > 0: return file_already_exists parent_dir: File = (wrapper.session.query(File).filter_by( device=device.uuid, uuid=parent_dir_uuid, is_directory=True).first()) if not parent_dir and parent_dir_uuid is not None: return parent_directory_not_found if is_directory and content != "": return directory_can_not_have_textcontent file: File = File.create(device.uuid, filename, content, parent_dir_uuid, is_directory) m.contact_user( user, { "notify-id": "file-update", "origin": "create", "device_uuid": device.uuid, "data": { "created": [file.uuid], "deleted": [], "changed": [] }, }, ) return file.serialize
def file_upload(): u = current_user() if u is None: redirect(url_for("index.profile")) file = request.files['file'] if file.filename == '': return redirect(request.url) filename = file.filename file.save(os.path.join(data_file_directory, filename)) f = File.new() f.filename = filename f.user_id = u.id f.type = filename.split('.')[-1] f.save() return redirect(url_for('.index'))
def get(cls, _id): try: obj = Model() obj.set_id(_id) obj.load() result = {'kootaj': obj.kootaj, 'desc': obj.desc, '_id': obj.get_id(), } return result except Exception, err: return err.message
def get(self): template = JINJA_ENVIRONMENT.get_template('/templates/publicArtPiece.html') itemNumber = self.request.get('itemNumber') artpiece = ArtPiece.query(ArtPiece.itemNumber==itemNumber).get() photo = File.query(File.key==artpiece.picture).get() artist = Artist.query(Artist.key==artpiece.artist).get() #create a comma separated string of categories categories = ndb.get_multi(artpiece.categories) categoryNamesList = [] for category in categories: categoryNamesList.append(str(category.categoryName)) categoriesString = ",".join(categoryNamesList) #check for additional sizes if this is a master or a slave piece additionalPieces = [] if artpiece.masterArtFlag: slavePieces=ArtPiece.query(ArtPiece.masterArtPiece==artpiece.key) for slavepiece in slavePieces: additionalPieces.append(slavepiece) if artpiece.slaveArtFlag: masterArtPiece = ArtPiece.query(ArtPiece.key==artpiece.masterArtPiece).get() otherSlavePieces=ArtPiece.query(ArtPiece.masterArtPiece==masterArtPiece.key,ArtPiece.key<>artpiece.key) additionalPieces.append(masterArtPiece) for slavepiece in otherSlavePieces: additionalPieces.append(slavepiece) #generate the appropriate html to link to these other pieces if they exist additionalSizes = "" if len(additionalPieces) > 0: additionalSizes += "<h5>Also Available in Other Sizes:</h5><ul>" for additionalPiece in additionalPieces: additionalSizes += "<a href=\"/artpiece?itemNumber=" + additionalPiece.itemNumber + "\"><li>" + additionalPiece.name + " (" + additionalPiece.priceDisplay + ")</li></a>" additionalSizes += "</ul>" templateVars = { "title" : artpiece.name, "additionalSizes": additionalSizes, "artpiece": artpiece, "artist": artist, "photo": photo, "categories": categoriesString} self.response.write(template.render(templateVars))
def add(cls, id='', kootaj='', desc=''): if id != '': obj = Model.objects(Kootaj=id).first() else: obj = Model() if obj != None: obj.kootaj = kootaj obj.desc = desc obj.save()
def create(data: dict, user: str) -> dict: """ Create a new file. :param data: The given data. :param user: The user uuid. :return: The response """ device: Optional[Device] = wrapper.session.query(Device).filter_by( uuid=data['device_uuid']).first() if device is None: return invalid_device if not device.check_access(user): return permission_denied if 'filename' in data: filename: str = data['filename'] else: return no_file_name if 'content' in data: content: str = data['content'] else: return no_content file_count: int = wrapper.session.query(File).filter_by( device=device.uuid, filename=filename).count() if file_count > 0: return file_already_exists if len(filename) == 0: return empty_name_not_allowed if len(filename) > 64: return name_too_long if len(content) > CONTENT_LENGTH: return length_exceeded file: Optional[File] = File.create(device.uuid, filename, content) return file.serialize
def Upload_to_cloud(current_user, curent_preference, db, name_path): if os.path.exists("Buffer\\Preference_user_" + str(current_user.id) + "\\preference"): os.rename("Buffer\\Preference_user_" + str(current_user.id) + "\\preference", "Buffer\\Preference_user_" + str(current_user.id) + f"\\{curent_preference.name}") shutil.copytree(name_path, Constants.cloud_folder_path(current_user,curent_preference), dirs_exist_ok=True) all_files = Files.get_files_from_cloud(current_user, curent_preference) for path in all_files: if File.query.filter_by(file_name=path).first() is None: file = File(file_name=path) db.session.add(file) db.session.commit() preference_file = Preference_file(file_id=file.id, preference_id=curent_preference.preference_id) db.session.add(preference_file) print(f"NEW File {path}") db.session.commit() else: print(f"Update File {path}")
def all(cls, _filter=None, page=-1, perpage=15, sort='kootaj', order=1): try: obj = Model() obj.find(_filter=_filter, page=page, perpage=perpage, sort=sort, order=order) result = [] while not obj.eof: result.append({'kootaj': obj.kootaj, 'desc': obj.desc, '_id': obj.get_id(), }) obj.next() return result except Exception, err: return err.message
def post(self): categoryKeyString = self.request.get('editCategoryKey') categoryID = int(categoryKeyString) categoryName = self.request.get('editCategoryName') photoName = self.request.get('editCategoryPhotoName') #get the photo specified by the user photo = File.query(File.file_name==photoName.upper()).get() #get the category based on the key and update all fields category = Category.get_by_id(categoryID) category.categoryName=categoryName category.picture=photo.key category.uploaded_by=users.get_current_user() category.put() category.add_to_search_index() message = "Successfully updated category record: " + category.categoryName self.response.write(message)
def find_file(cls, name, hash, user): mongodbmanager.connect() return File.objects(name = name, hash = hash, session_id = user.session_id).first()
def add_new_remote_file(cls, file_name, file_id, file_size, part_size): file = File(file_id, file_name) file.set_file_and_part_size(file_size, part_size) cls.get_files().append(file)
def find_file_by_hash_and_sessionid(cls, hash, session_id): mongodbmanager.connect() return File.objects(hash = hash, session_id = session_id).first()
def find_files_by_hash(cls, hash): mongodbmanager.connect() return File.objects(hash = hash).all()
try: # If a commit has multiple parents, it's a merge commit. pygit2 appears to put the merged commit last, so we'll take that. TODO: find a commit with > 2 parents and make sure everything works out # TODO: This sometimes throws a GitError - "Object not found - no matching loose object" diff = commit.tree.diff_to_tree(commit.parents[-1].tree) patch = Patch(diff=diff.patch) files_changed = 0 for git_file_diff in diff: files_changed += 1 file_diff = FileDiff(git_file_diff.old_file_path, git_file_diff.new_file_path) file_diff.patch = patch current_file_path = '' for file_path_part in file_diff.new_file_path.split('/'): current_file_path += file_path_part new_file = File(current_file_path) new_file.file_diff = file_diff current_file_path += '/' session.add(new_file) for hunk in git_file_diff.hunks: hunk_lines_added = 0 hunk_lines_removed = 0 for line in hunk.lines: if line[0] == '+': hunk_lines_removed += 1 elif line[0] == '-': hunk_lines_added += 1 hunk = Hunk(hunk_lines_added, hunk_lines_removed) hunk.file_diff = file_diff
def count_files_by_hash(cls, hash): mongodbmanager.connect() return File.objects(hash = hash).count()
def telegramWebHook(): update = Update.de_json(request.get_json(force=True)) text = None if getattr(update.message, 'document'): gallery = Gallery().search(tgid = update.message.chat.id) if gallery: newfile = bot.getFile(update.message.document.file_id) file_name = update.message.document.file_id newfile.download(file_name) writed = False if os.path.exists(file_name): writed = write_file(file_name, read_file(file_name, storage = 'local', append_path = False), acl = 'public-read', mime_type = update.message.document.mime_type) thumbnail(file_name) os.remove(file_name) write_file('%s.json' % file_name, update.to_json()) if writed: file_id = File(gallery_eid = gallery.eid, file_id = update.message.document.file_id) file_id.save() sendLink = getattr(gallery, 'sendLink', None) if sendLink == 'True': text = 'File URL: %s' % url_for('image', file_id = file_id.eid, _external = True, disable_web_page_preview = True) else: text = 'Failed to download file' else: text = 'Gallery does not exist, please create first' pass if getattr(update.message, 'text'): args = update.message.text.split(' ', 2) if args[0] == '/register': text = 'Username:'******'Complete register: https://telegram.me/ACSGalleryBot?start=%s' % update.message.from_user.id else: text = 'User added to gallery' # set gallery permission at this point because i have chat id elif args[0] == '/start': if len(args) > 1 and int(args[1]) == int(update.message.chat.id): text = 'Username:'******'force_reply' : True }) else: text = update.to_json() elif getattr(update.message, 'reply_to_message'): if update.message.reply_to_message.text == 'Username:'******'Password:'******'force_reply' : True }) return 'ok' elif update.message.reply_to_message.text == 'Password:'******'User succesfuly registered' elif args[0] == '/create': if hasattr(update.message.chat, 'title'): gallery = Gallery().search(tgid = update.message.chat.id) if not gallery: gallery = Gallery(tgid = update.message.chat.id, title = update.message.chat.title).save() text = 'Gallery URL: %s' % url_for('gallery', id = gallery.eid, _external = True, _scheme = 'https') else: text = 'Bot only works in groups' elif args[0] == '/remove': gallery = Gallery().search(tgid = update.message.chat.id) if gallery: gallery.delete() text = 'Gallery deleted' else: text = 'Gallery is not registered' # TODO: Confirm elif args[0] == '/config': args.pop(0) gallery = Gallery.search(tgid = update.message.chat.id) if gallery: if len(args) == 0: text = g.config(update.message.chat.id) elif len(args) == 1: text = 'get one' text = g.config(update.message.chat.id, args[0]) else: text = g.config(update.message.chat.id, args[0], args[1]) else: text = 'Gallery is not registered' #else: # text = update.to_json() if text: bot.sendMessage(update.message.chat.id, text, disable_web_page_preview=True) return ""
def find_files_for_user(cls, user): mongodbmanager.connect() return File.objects(session_id = user.session_id)