def setUp(self): # Give user john 0 warnings (default) u = User.objects.create(username="******", email="*****@*****.**", colour="#00FF00", upload_key=get_upload_key()) u.set_password("MyP@$$w0rd!") u.save() # Give user jane 3 warnings u = User.objects.create(username="******", email="*****@*****.**", colour="#FFFF00", upload_key=get_upload_key(), warnings=3) u.set_password("MyP@$$w0rd!") u.save() # Have user jane upload 2 files, 1 public, 1 private. filepath = os.getcwd() + '/populate_files/hello.txt' mf = open(filepath, 'rb') f = ContentFile(mf.read()) f.size = os.path.getsize(filepath) f.name = "test_public.txt" File.objects.create(user=u, file_content=f, generated_filename=get_id_gen(), is_private=False) f.name = "test_private.txt" File.objects.create(user=u, file_content=f, generated_filename=get_id_gen(), is_private=True) filepath = os.getcwd() + '/populate_files/ER.png' mf = open(filepath, 'rb') f = ContentFile(mf.read()) f.size = os.path.getsize(filepath) f.name = "ER.png" File.objects.create( user=u, file_content=f, file_thumbnail=f, generated_filename=get_id_gen(), )
def upload_file(user, filepath, private=False): try: myfile = open(filepath, 'rb') f = ContentFile(myfile.read()) f.name = filepath.split("/")[1] f.size = os.path.getsize(filepath) ext = filepath.split(".")[-1] supported_thumbnail = ext.lower() in [ "gif", "png", "jpg", "webm", "mp4", "jpeg" ] f = File.objects.get_or_create( user=user, ip='127.0.0.1', file_content=f, file_thumbnail=f if supported_thumbnail else None, generated_filename=get_id_gen(), is_private=private)[0] f.save() if private: print( '\t* New PRIVATE file uploaded by ' + user.username + ':', f.original_filename) else: print('\t* New file uploaded by ' + user.username + ':', f.original_filename) except Exception: print("\t* Could not upload file: " + filepath)
def test_file_with_size_greater_than_max_should_not_validate(self): file = ContentFile('some content') file.size = MAX_FILE_SIZE + 1 try: validate_max_size(file) self.fail('ValidationError should be raised') except ValidationError: pass
def test_clean(self): file = ContentFile(b'b' * 1024) file.name = 'abc.jpg' file.size = 1024 wf = WebFile(file=file) wf.full_clean() wf.save() self.assertEqual(wf.mimetype, 'image/jpeg') self.assertEqual(wf.size, 1024)
def test_view(self): file = ContentFile(b'b' * 1024) file.name = 'abc.jpg' file.size = 1024 wf = WebFile(file=file) wf.full_clean() wf.save() urls = [ reverse('webfs:file_by_id', args=(wf.id, )), reverse('webfs:file_by_name', args=(wf.file, )) ] for url in urls: response = self.client.get(url) self.assertEqual(response['Content-Type'], 'image/jpeg') self.assertTrue(response.streaming) self.assertEqual(next(response.streaming_content)[:4], b'bbbb')
def setUp(self): u = User.objects.create(username="******", email="*****@*****.**") u.set_password("testPassword123") u.save() u = User.objects.create(username="******", email="*****@*****.**") u.set_password("testPassword123") u.save() filepath = os.getcwd() + '/populate_files/hello.txt' mf = open(filepath, 'rb') f = ContentFile(mf.read()) f.size = os.path.getsize(filepath) f.name = "test_public.txt" File.objects.create( user=u, file_content=f, generated_filename=get_id_gen(), ) ErrorVideo.objects.create( title="YEE", url="https://www.youtube.com/watch?v=q6EoRBvdVPQ")
def populate(): nightclubs = { "Name": ["Cathouse", "Firewater", "Sub Club", "The Garage", "Reflex"], "Music": ["Rock", "EDM", "Dance", "EDM", "80s"], "Price": ["£3", "£4.50", "$2.60", "$3", "£2.50"], "Desc": [ "filler text one", "filler text two", "filler text three", "filler text four", "filler text five" ], "Location": [ "Argyle Street", "Cambell Lane", "Random Alley", "Forgotten Avenue", "Sauchiehall Street" ], "Score": [3, 4, 5, 3, 4], "Image": [] } users = { "User": [ "seshlad420", "partygirl74", "SarahMathews12", "Carl Marks", "StevenAaron45" ], "Email": [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**" ], "Password": [ "password123", "passcode234", "mypassword1", "throwaway666", "outofideas" ], "Image": [], } #"one.jpg", "two.jpg", "three.jpg", "four.jpg", "five.jpg" #"user1.jpg", "user2.jpg", "user3.jpg", "user4.jpg", "user5.jpg" directory1 = r'\WAD2 Project\sesh_master_app\Club_Images' for i in "one.jpg", "two.jpg", "three.jpg", "four.jpg", "five.jpg": filepath = directory1 + "\\" + i myf = open(filepath, 'rb') f = ContentFile(myf.read()) f.name = filepath.split("/")[-1] f.size = os.path.getsize(filepath) nightclubs["Image"].append(f) directory2 = r'\WAD2 Project\sesh_master_app\\User_Images' for j in "user1.jpg", "user2.jpg", "user3.jpg", "user4.jpg", "user5.jpg": filepath = directory2 + "\\" + j myf = open(filepath, 'rb') f = ContentFile(myf.read()) f.name = filepath.split("/")[-1] f.size = os.path.getsize(filepath) users["Image"].append(f) for i in range(5): Name = nightclubs["Name"][i] Music = nightclubs["Music"][i] Price = nightclubs["Price"][i] Desc = nightclubs["Desc"][i] Location = nightclubs["Location"][i] Score = nightclubs["Score"][i] Image = nightclubs["Image"][i] add_club(Name, Music, Price, Desc, Location, Score, Image) for j in range(5): User = users["User"][j] Email = users["Email"][j] Password = users["Password"][j] Image = users["Image"][j] add_user(User, Email, Password, Image) print("Database Successfully Populated.")
def test_file_with_max_size_should_validate(self): file = ContentFile('some content') file.size = MAX_FILE_SIZE validate_max_size(file)