def save_file_locally(publication_id): publication = Publication.objects.get(id=publication_id) try: if not publication.local_file: request = requests.get(publication.remote_file, stream=True) if request.status_code != requests.codes.ok: return base_name, ext = os.path.splitext( publication.remote_file.split("/")[-1]) file_name = f'{base_name}{ext}' lf = tempfile.NamedTemporaryFile() for block in request.iter_content(1024 * 8): if not block: break lf.write(block) publication.local_file.save(file_name, files.File(lf)) # fixme: move to separate task pages_dir = tempfile.TemporaryDirectory() lf = publication.local_file for i, file in enumerate(convert_pdf_to_png(lf.path, pages_dir.name)): page = Page.objects.create(number=i + 1, publication=publication) with open(file, "rb") as f: page.image.save( f'publication_{publication.id}_page_{i + 1}.jpg', files.File(f)) page.save() except Exception: # yup, too broad ¯\_(ツ)_/¯ publication.download_status = Publication.DOWNLOAD_FAILED else: publication.download_status = Publication.DOWNLOAD_DONE publication.save()
def _make_content(self, params): path = '' if params.get('from_path') and params.get('from_file'): raise ValueError( "At most one argument from 'from_file' and 'from_path' should " "be non-empty when calling factory.django.FileField." ) if params.get('from_path'): path = params['from_path'] f = open(path, 'rb') content = django_files.File(f, name=path) elif params.get('from_file'): f = params['from_file'] content = django_files.File(f) path = content.name else: data = self._make_data(params) content = django_files.base.ContentFile(data) if path: default_filename = os.path.basename(path) else: default_filename = self.DEFAULT_FILENAME filename = params.get('filename', default_filename) return filename, content
def test_duplicate_name_in_different_locations(self): storage1 = DatabaseStorage( location=os.path.join(DIR, "media/location1")) if os.path.isdir(storage1.location): shutil.rmtree(storage1.location) storage2 = DatabaseStorage( location=os.path.join(DIR, "media/location2")) if os.path.isdir(storage2.location): shutil.rmtree(storage2.location) def get_name(instance, filename): return "dummy.txt" class Location1Thing(models.Model): upload = models.FileField(storage=storage1, upload_to=get_name, max_length=500) class Meta: managed = False db_table = Thing()._meta.db_table class Location2Thing(models.Model): upload = models.FileField(storage=storage2, upload_to=get_name, max_length=500) class Meta: managed = False db_table = Thing()._meta.db_table tmpdir = tempfile.mkdtemp( dir=os.path.join(settings.PROJECT_DIR, "media")) data1 = b"11111111" open(os.path.join(tmpdir, "dummy.txt"), "wb").write(data1) upload = files.File(open(os.path.join(tmpdir, "dummy.txt"), "rb")) t1 = Location1Thing(upload=upload) t1.save() self.assertTrue(t1.upload.storage.exists(t1.upload.name)) os.remove(t1.upload.path) self.assertTrue(t1.upload.storage.exists(t1.upload.name)) self.assertRaises(NotImplementedError, lambda t1: t1.upload.path, t1) data2 = b"22222222" open(os.path.join(tmpdir, "dummy.txt"), "wb").write(data2) t2 = Location2Thing.objects.create( upload=files.File(open(os.path.join(tmpdir, "dummy.txt"), "rb"))) os.remove(t2.upload.path) self.assertTrue(t2.upload.storage.exists(t2.upload.name)) self.assertRaises(NotImplementedError, lambda t2: t2.upload.path, t2) self.assertEqual(File.objects.count(), 2) self.assertEqual( Location2Thing.objects.get(pk=t2.pk).upload.file.read(), data2) self.assertEqual( Location1Thing.objects.get(pk=t1.pk).upload.file.read(), data1) shutil.rmtree(tmpdir)
def get(self, request, format=None): try: groupId = request.GET.get("group_id", None) group = Groups.objects.get(id=groupId) data = get_group_photos(1, group.flickr_id) pages = data['photos']['pages'] for photo in data['photos']['photo']: photo = Photos.objects.create(flickr_id=photo['id']) sizes = get_photo_size(photo.flickr_id) url = sizes['sizes']['size'][-1]['source'] result = requests.get(url, stream=True) if result.status_code == requests.codes.ok: lf = tempfile.NamedTemporaryFile() file_name = url.split('/')[-1] for block in result.iter_content(1024 * 8): if not block: break lf.write(block) photo.image.save(file_name, files.File(lf)) GroupPhotos.objects.create(group=group, photo=photo) if pages > 1: for i in range(2, pages + 1): data = get_group_photos(i, group.flickr_id) for photo in data['photos']['photo']: photo = Photos.objects.create(flickr_id=photo['id']) sizes = get_photo_size(photo['id']) url = data['sizes']['size'][-1]['source'] result = requests.get(url, stream=True) if result.status_code == requests.codes.ok: lf = tempfile.NamedTemporaryFile() file_name = url.split('/')[-1] for block in result.iter_content(1024 * 8): if not block: break lf.write(block) photo.image.save(file_name, files.File(lf)) print(photo) GroupPhotos.objects.create(group=group, photo=photo) response = {'status': status.HTTP_200_OK, 'message': 'Group added'} except ValueError as err: response = { 'status': status.HTTP_400_BAD_REQUEST, 'error_message': str(err) } return Response(response, status=response['status'])
def test_get_available_name(self): storage = DatabaseStorage( location=os.path.join(DIR, "media/custom_location")) if os.path.isdir(storage.location): shutil.rmtree(storage.location) class CustomLocationThing(models.Model): upload = models.FileField(storage=storage, max_length=500) class Meta: managed = False db_table = Thing()._meta.db_table # Create a file in a subdirectory of the custom location and save it test_fn = os.path.join("subdirectory", "test.txt") test_fqfn = os.path.join(storage.location, "subdirectory", "test.txt") with NamedTemporaryFile() as t: t.write(b"hello there") o = CustomLocationThing() o.upload = files.File(t, name=test_fn) o.save() # Verify we can read the contents of thing. o = CustomLocationThing.objects.first() self.assertEqual(o.upload.read(), b"hello there") self.assertEqual(File.objects.count(), 1) self.assertEqual( File.objects.first().name, os.path.join("custom_location", "subdirectory", "test.txt"), ) # Delete file from local filesystem self.assertEqual(os.path.isfile(test_fqfn), True) os.remove(test_fqfn) self.assertEqual(os.path.isfile(test_fqfn), False) # Create another (different) file in the same subdirectory of the custom location and save it with NamedTemporaryFile() as t: t.write(b"goodbye") o2 = o = CustomLocationThing() o.upload = files.File(t, name=test_fn) o.save() # Verify we can read the contents of thing. o = CustomLocationThing.objects.get(pk=o2.pk) self.assertEqual(o.upload.read(), b"goodbye") self.assertEqual(File.objects.count(), 2) # 'custom_location/subdirectory/test.txt' is already taken, so the second file # should have a different name self.assertNotEqual( o.upload.name, os.path.join("custom_location", "subdirectory", "test.txt")) self.assertTrue( o.upload.name.startswith( os.path.join("custom_location", "subdirectory", "test_")))
def create_unmapped_exam_answers(exam, folder, name_prefix, num_pages_per_exam, num_students): question_parts = models.QuestionPart.objects.filter(exam=exam) for cur_student in range(num_students): exam_answer = models.Submission(course_user=None, exam=exam, page_count=num_pages_per_exam) temp_pdf_name = '%s/%s%d.pdf' % (folder, name_prefix, cur_student) temp_pdf = file(temp_pdf_name, 'rb') exam_answer.pdf.save('new', files.File(temp_pdf)) exam_answer.save() os.remove(temp_pdf_name) for cur_page in range(num_pages_per_exam): exam_answer_page = models.SubmissionPage( exam_answer=exam_answer, page_number=cur_page + 1) cur_page_num = cur_student * num_pages_per_exam + cur_page temp_jpeg_name = '%s/%s%d.jpeg' % (folder, name_prefix, cur_page_num) temp_jpeg = file(temp_jpeg_name, 'rb') exam_answer_page.page_jpeg.save('new', files.File(temp_jpeg)) # Doing this because this script is a worst case scenario and we're probably # short on time so don't want another upload. exam_answer_page.page_jpeg_large = exam_answer_page.page_jpeg exam_answer_page.save() temp_jpeg.close() for question_part in question_parts: answer_pages = '' for page in question_part.pages.split(','): page = int(page) answer_pages = answer_pages + str(2 * page - 1) + ',' # Remove the trailing comma (,) from the end of answer_pages answer_pages = answer_pages[:-1] response = models.Response(question_part=question_part, exam_answer=exam_answer, pages=answer_pages) response.save() for cur_page in range(num_pages_per_exam): cur_page_num = cur_student * num_pages_per_exam + cur_page temp_jpeg_name = '%s/%s%d.jpeg' % (folder, name_prefix, cur_page_num)
def save_user_profile(backend, user, response, *args, **kwargs): if backend.name == "google-oauth2": # print(response.keys()) # print(response.items()) if 'gender' in response.keys(): if response['gender'] == 'male': user.shopuserprofile.gender = ShopUserProfile.MALE else: user.shopuserprofile.gender = ShopUserProfile.FEMALE if 'tagline' in response.keys(): user.shopuserprofile.tagline = response['tagline'] if 'aboutMe' in response.keys(): user.shopuserprofile.aboutMe = response['aboutMe'] if 'picture' in response.keys(): url = response['picture'] resp = requests.get(url) if resp.status_code != requests.codes.ok: print('error') fp = BytesIO() fp.write(resp.content) file_name = url.split("/")[-1] user.avatar.save(file_name, files.File(fp)) # user.avatar = response['picture'] if 'ageRange' in response.keys(): minAge = response['ageRange']['min'] if int(minAge) < 18: user.delete() raise AuthForbidden('social_core.backends.google.GoogleOAuth2') user.save() elif backend.name == "vk-oauth2": # print(response.keys()) # print(response.items()) if 'photo' in response.keys(): url = response['photo'] resp = requests.get(url) print(f'{resp} | {resp.content}') if resp.status_code != requests.codes.ok: print('error') fp = BytesIO() fp.write(resp.content) file_name = url.split("/")[-1] user.avatar.save(file_name, files.File(fp))
def handle(self, *args, **options): with open('animes.json', 'r') as _input: animes = json.load(_input) animes = [anime for anime in animes if anime['score']] for i, anime in enumerate( sorted(animes, key=lambda a: a['score'], reverse=True)): if Interest.objects.filter(name=anime['name'], interest_type='A').exists(): print('skip') continue new_anime = Interest() new_anime.name = anime['name'] new_anime.interest_type = 'A' if i <= 1000: while True: resp = s.get(anime['thumbnail']) if resp.status_code == requests.codes.ok: time.sleep(.2) break else: print("blya") time.sleep(10) fp = BytesIO() fp.write(resp.content) file_name = anime['name'] + '_thumb.jpg' new_anime.thumbnail.save(file_name, files.File(fp)) else: new_anime.save() print(new_anime.name) print(len(animes))
def generate(self, sequence, obj, create, params): """Fill in the field.""" params.setdefault('__sequence', sequence) params = base.DictFactory.simple_generate(create, **params) filename, content = self._make_content(params) return django_files.File(content.file, filename)
def post(self, request, *args, **kwargs): if request.data.get('url', False): url = request.data.get('url') request = requests.get(url, stream=True) file_name = 'image' + randomString() + '.' + url.split('.')[-1] lf = tempfile.NamedTemporaryFile() for block in request.iter_content(1024 * 8): if not block: break lf.write(block) image = QueryImage() image.url = url image.file.save(file_name, files.File(lf)) query_image_serializer = QueryImageSerializer(image) return Response(query_image_serializer.data, status=status.HTTP_201_CREATED) else: query_image_serializer = QueryImageSerializer(data=request.data) if query_image_serializer.is_valid(): query_image_serializer.save() return Response(query_image_serializer.data, status=status.HTTP_201_CREATED) else: return Response(query_image_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def handle(self, *args, **options): url = options['url'] response = requests.get(url) response.raise_for_status() decoded_response = response.json() if 'error' in decoded_response: raise requests.exceptions.HTTPError(decoded_response['error']) place, create = Place.objects.get_or_create( title=decoded_response['title'], defaults={ "short_description": decoded_response['description_short'], "long_description": decoded_response['description_long'], "lng": decoded_response['coordinates']['lng'], "lat": decoded_response['coordinates']['lat'] }) image_urls = decoded_response['imgs'] for number, image_url in enumerate(image_urls, 1): response = requests.get(image_url) response.raise_for_status() image_name = image_url.split('/')[-1] placeimage, create = PlaceImage.objects.get_or_create( place=place, number=number) byte_response = BytesIO(response.content) placeimage.image.save(image_name, files.File(byte_response), save=True)
def handle(self, *args, **options): Contact.objects.all().delete() for i in range(50): contact_data = requests.get('https://randomuser.me/api/').json() contact_data = contact_data['results'][0] name = contact_data['name']['first'] + ' ' + contact_data['name']['last'] age = contact_data['dob']['age'] contact = Contact(name=name, age=age, image=None) contact.save() image_url = contact_data['picture']['large'] image_response = requests.get(image_url) image_data = BytesIO() image_data.write(image_response.content) file_name = image_url.split('/')[-1] contact.image.save(file_name, files.File(image_data)) contact.save() print(str(round(i/50*100,2))+'%') print(name)
def make_testdata(code): url = "https://chart.yahoo.co.jp/?code={}.T&tm=6m&type=c&log=off&size=m&over=m25,m75&add=m,r,vm&comp=".format( code) r = requests.get(url) if r.status_code == 200: ee = EntryExit() # date ee.date_entry = date.today() # stock info = stock_overview(code) if Stocks.objects.filter(code=code).exists(): stock = Stocks.objects.get(code=code) else: stock = Stocks.objects.create(name=info['name'], code=info['code']) ee.stock = stock # num ee.num_entry = 100 # price ee.price_entry = info['price'] ee.price_set_profit = round(info['price'] * 1.1, 1) ee.price_loss_cut = round(info['price'] * 0.9, 1) # file filename = "{}_{}.png".format(date.today(), code) fp = BytesIO() fp.write(r.content) ee.chart_entry.save(filename, files.File(fp)) return ee else: print("Failed") return False
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) url = request.data.get('image') if url: # Steam the image from the url request_image = requests.get(url, stream=True) # Create a temporary file lf = tempfile.NamedTemporaryFile() # Read the streamed image in sections for block in request_image.iter_content(1024 * 8): # If no more file then stop if not block: break # Write image block to temporary file lf.write(block) # Get the recently created recipe and add the image recipe = Recipe.objects.get(pk=serializer.data['id']) recipe.photo.save(str(uuid.uuid4()), files.File(lf)) recipe.save() return Response(serializer.data)
def create_category_service(form, file_value, track_info): """create new category using CategoryForm it will resize the image but it's not base-64 Arguments: form {POST Method} -- to define form method file_value {FILES Method} -- to get image file track_info {DICT} -- dictionary contains of user id and ip to update status_of_user Returns: BOOLEAN/STRING -- returns True if form is valid else returns forms errors """ cat_form = CategoryForm(form) if cat_form.is_valid(): try: image_b64 = file_value.get('image') th_img, ext = resize_pics(image_b64) cat_form.instance.image.save(f'category.{ext}', files.File(th_img), save=False) except: pass cat_form.save() status = f"دسته {cat_form.instance.title} را ساخت" status_of_user(track_info, status, 'import-create-cat') return True else: logger.error(str(cat_form.errors)) return cat_form.errors
def download_file_from_url(url): # Stream the image from the url try: request = requests.get(url, stream=True) except requests.exceptions.RequestException as e: return if request.status_code != requests.codes.ok: return # Create a temporary file lf = tempfile.NamedTemporaryFile() # Read the streamed image in sections for block in request.iter_content(1024 * 8): # If no more file then stop if not block: break # Write image block to temporary file lf.write(block) return files.File(lf)
def handle(self, *args, **options): for superevent_id in self.superevent_ids: response = requests.get(f'https://gcn.gsfc.nasa.gov/other/{superevent_id}.gcn3') circulars = re.split(r'\/{10,}', response.text) for circular in circulars: date = None file_name = None file_content = io.BytesIO() for line in circular.splitlines(): entry = line.split(':', 1) if len(entry) > 1: if entry[0] == 'NUMBER': alert_id = entry[1].strip() response = requests.get(f'https://gcn.gsfc.nasa.gov/gcn3/{alert_id}.gcn3', stream=True) file_content.write(response.content) file_name = f'{alert_id}.gcn3' elif entry[0] == 'DATE' and 'MAG:' not in entry[1]: date = parse(entry[1], parserinfo=parserinfo(yearfirst=True)) if date and file_name and file_content: alert = ScrapedAlert.objects.create( alert_type='lvc_circular', timestamp=date, ) alert.alert_data.save(file_name, files.File(file_content)) alert.save()
def create_new_videos(videos_info): videos = [] for video_info in videos_info: video = Video(yt_id=video_info['id'], yt_title=video_info['title'], published_at_yt=video_info['published_at']) video.title = clean_video_title(video_info['title']) video.description = clean_video_description(video_info['description']) video.save() videos.append(video) request = requests.get(video_info['thumbnail_url'], stream=True) if request.status_code == requests.codes.ok: tempimg = tempfile.NamedTemporaryFile() for block in request.iter_content(1024 * 8): if not block: break tempimg.write(block) video.thumbnail = files.File(tempimg) video.save() # make thumbnail be "cut" in the right ratio for the video thumbnails ratio = 275.0 / 154.0 t_width = int( min(video.thumbnail.width, video.thumbnail.height * ratio)) t_height = int( min(video.thumbnail.width / ratio, video.thumbnail.height)) #center the cut y_origin = video.thumbnail.height / 2 - t_height / 2 y_end = y_origin + t_height t_ratio = '0,' + str(y_origin) + str(t_width) + ',' + str(y_end) video.thumbnail_ratio = t_ratio video.save() if 'tags' in video_info: for video_tag in video_info['tags']: tag = Tag.objects.create(video=video, name=video_tag) return videos
def ContentUpdator(request): global c if c == 1: category="general" c+=1 elif c==2: category="science" c+=1 elif c==3: category="sports" c+=1 elif c==4: category="health" c+=1 elif c==5: category="technology" c+=1 elif c==6: category="entertainment" c+=1 elif c==7: category="business" c=1 api_key=os.environ.get('NEWS_API') url="https://newsapi.org/v2/top-headlines?country=in&category={}&pagesize=50&apiKey={}".format(category,api_key) p=requests.get(url) n=p.json() for m in n['articles']: try: headline=m['title'] p=re.findall(r'-\s[\w\s]*',headline) p=p[-1] headline=re.compile(p).sub('',headline) if(News.objects.filter(headline=headline).exists()): pass else: author=m['author'] des=str(m['content']) p=re.compile('\[\+[\d\w\s]*\]') des=p.sub('',des) url=m['url'] url_img=m['urlToImage'] topic=Category.objects.get(title=category) u=News.objects.create(headline=headline,description=des,url=url,url_image=url_img,author=author,) u.categories.add(topic) regex = re.compile(r'^(?:http|ftp)s?://'r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'r'localhost|'r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'r'(?::\d+)?'r'(?:/?|[/?]\S+)$', re.IGNORECASE) if url_img is None: print('Url is empty') else: if url_img is not None: img_result=requests.get(url_img) fp = BytesIO() fp.write(img_result.content) file_name =str(random.random()*1000)+'.jpg' u.picture.save(file_name, files.File(fp)) u.save() except AttributeError or OperationalError or NewConnectionError: pass data={'status':'success'} return JsonResponse(data)
def download_profile_image(user=None, *args, **kwargs): is_azuread_oauth2 = kwargs['backend'] and kwargs[ 'backend'].name == "azuread-oauth2" and True or False if user and not user.profile.profile_image_uploaded_at and is_azuread_oauth2: try: username = user.username req = urllib2.Request( 'https://graph.microsoft.com/v1.0/me/photo/$value') req.add_header( 'Authorization', 'Bearer {0}'.format(kwargs['response']['access_token'])) resp = urllib2.urlopen(req) content = resp.read() fp = BytesIO() fp.write(content) image_file = files.File(fp) profile_image_names = get_profile_image_names(user.username) create_profile_images(image_file, profile_image_names) user.profile.profile_image_uploaded_at = now() user.save() log.info(LOG_MESSAGE_CREATE, { 'image_names': profile_image_names.values(), 'user_id': user.id }) except Exception as e: log.exception( 'Error when downloading user image from Microsoft API', exc_info=True)
def handle(self, *args, **options): for each in Launcher.objects.all(): if 'cloudinary' in str(each.image_url) or 'imgur' in str( each.image_url): each.image_url = '' if each.image_url == '': result = requests.get("http://launchlibrary.net/1.3/rocket/" + str(each.id)) if len(result.json()['rockets']) > 0: webrocket = result.json()['rockets'][0] if 'placeholder' not in webrocket['imageURL']: request = requests.get(webrocket['imageURL'], stream=True) filename = webrocket['imageURL'].split('/')[-1] filename, file_extension = os.path.splitext(filename) clean_name = urllib.quote( urllib.quote(each.name.encode('utf8')), '') clean_name = "%s_nation_%s" % (clean_name.lower( ), datetime.datetime.now().strftime("%Y%m%d%H%M%S")) name = "%s%s" % (str(clean_name), file_extension) lf = tempfile.NamedTemporaryFile() for block in request.iter_content(1024 * 8): if not block: break lf.write(block) imageFile = Launcher.objects.get(id=each.id).image_url imageFile.save(name, files.File(lf)) print(each.name)
def edit_category_service(form, file_value, track_info, category): """edit current category object with Form Model of Categories Arguments: form {POST Method} -- to define CategoryForm method file_value {FILES Method} -- to get image for category track_info {DICT} -- dictionary contains of user id and ip to update status_of_user Returns: BOOLEAN/STRING -- returns True if form is valid else returns forms errors """ cat_form = CategoryForm(form, instance=category) if cat_form.is_valid(): try: image_b64 = file_value.get('image') th_img, ext = resize_pics(image_b64) cat_form.instance.image.save(f'category.{ext}', files.File(th_img), save=False) except: pass cat_form.save() status = f"مشخصات دسته {cat_form.instance.title} را تغییر داد" status_of_user(track_info, status, 'import-edit-cat') return True else: logger.error(str(cat_form.errors)) return cat_form.errors
def get_files(self, target, filenames): """ Identify existing unique files by checksum. Can be used for both images and attachments """ if len(filenames) == 0: return [], [] hashmap = {} for filename in filenames: with open(filename, 'rb') as fp: f = files.File(fp) checksum = generate_checksum(f) hashmap[checksum] = filename hashes = list(hashmap.keys()) r = self.get(target, params={'checksums': hashes}) found_files = r.json() found_names = list(map(lambda x: hashmap[x['checksum']], found_files)) missing_files = [] for filename in filenames: if not filename in found_names: missing_files.append(filename) print(target) print(found_files) print(missing_files) print('-' * 80) return found_files, missing_files
def update_startup_service(form, file_value, user, track_info, form_start): """if request method is POST and the startup is already exists it will allow to update it Arguments: form {POST Method} -- to get some fileds for category and so on file_value {FILES} -- get the imported files (image) user {OBJECT} -- requested user track_info {DICT} -- dictionary contains of user id and ip to update status_of_user form_start {FORM} -- get the model form that has instances of current startup Returns: BOOLEAN -- returns True if form is valid """ if form_start.is_valid(): lists = get_list(form['start_date1']) date = JalaliDate(lists[0], lists[1], lists[2]).to_gregorian() form_start.instance.start_date = date form_start.instance.site = form.get('site') if form.get( 'site').startswith('http') else str('http://') + str( form.get('site')) try: image_b64 = form.get('avatar_base64') th_img, ext = resize_pics(image_b64) form_start.instance.image.save(f'startup.{ext}', files.File(th_img), save=False) except: pass if form_start.instance.status == 'failed_document': form_start.instance.status = 'editing' form_start.save() the_c = StartupComments.objects.get(startup=form_start.instance) the_c.doc_stat = None the_c.doc_stat_status = None the_c.save() if form_start.instance.status == 'failed': form_start.instance.status = 'editing' form_start.save() the_c = StartupComments.objects.get(startup=form_start.instance) the_c.startup_stat = None the_c.startup_stat_status = None the_c.save() form_start.instance.category.clear() for cat in form.getlist('category'): form_start.instance.category.add(cat) form_start.save() user_profile = UserProfile.objects.get(user=user) user_profile.startup = form_start.instance user_profile.save() if user_profile.user.step == 'startup': user_profile.user.step = 'team' user_profile.user.save() status = f"استارت اپ {form_start.instance.title} را تغییر داد" status_of_user(track_info, status, 'registering-startup-step') return True else: logger.error(str(form_start.errors))
def download_image(self): if self.image: # do not download image if already downloaded return resp = requests.get(self.image_url) if "/empty_n.jpg" in self.image_url: # remove default image of databazeknih if self.image and os.path.exists(self.image.path): os.remove(self.image.path) self.book.delete() return if resp.status_code == 404: logger.info( f"BookCover.download_image PK {self.pk}: URL {self.image_url} returned {resp.status_code}. Removing BookCover." ) self.delete() return if resp.status_code != requests.codes.ok: raise Exception( f"Image cannot be downloaded {self.image_url}, {resp.status_code}, {resp}" ) fp = BytesIO() fp.write(resp.content) file_name = self.image_url.split("/")[-1] self.image.save(file_name, files.File(fp)) # save BookCover to make image hashes self.save()
def get_remote_image(self): """get_remote_image. Download image using remote url """ try: if self.remote_url: resp = requests.get(self.remote_url) if resp.status_code != status.HTTP_200_OK: raise requests.exceptions.RequestException bytes_io = BytesIO() bytes_io.write(resp.content) file_name = f"{self.part.name}-{self.remote_url.split('/')[-1]}" logger.info("Saving as name %s", file_name) self.image.save(file_name, files.File(bytes_io)) bytes_io.close() self.save() except requests.exceptions.RequestException as request_err: # Probably wrong url raise request_err except Exception as unexpected_error: logger.exception("unexpected error") raise unexpected_error
def get_image_url(image_url): response = requests.get(image_url, stream=True) # Was the request OK? if response.status_code != requests.codes.ok: # Nope, error handling, skip file etc etc etc return None, None # Get the filename from the url, used for saving later file_name = image_url.split('/')[-1] # Create a temporary file lf = tempfile.NamedTemporaryFile() # Read the streamed image in sections for block in response.iter_content(1024 * 8): # If no more file then stop if not block: break # Write image block to temporary file lf.write(block) return file_name, files.File(lf)
def save(self): user = self.user team_id = self.cleaned_data['team_id'] team_password = self.cleaned_data['team_password'] team = Team.objects.get(id=int(team_id), password=team_password) user.team = team user.is_student = self.cleaned_data['is_student'] user.display_name = self.cleaned_data['display_name'] if self.cleaned_data['is_import_github_icon']: resp = requests.get("https://github.com/%s.png" % user.username) if resp.status_code != requests.codes.ok: raise RuntimeError('icon fetch failed') file_name = "{}_{}.png".format(user.id, str(uuid.uuid4())) fp = BytesIO() fp.write(resp.content) user.icon.save(file_name, files.File(fp)) else: user.icon = self.cleaned_data['user_icon'] user.save() return user
def download_xmpp_net_badges(): if not settings.OBSERVATORY_URL: log.debug( 'Not downloading badges because OBSERVATORY_URL setting is not set.' ) return url = urljoin(settings.OBSERVATORY_URL, 'badge.php') for host in settings.XMPP_HOSTS: filename = 'badge_%s.svg' % host.replace('.', '_') try: image = Image.objects.get(name=filename) except Image.DoesNotExist: image = Image(name=filename) try: response = requests.get(url, params={'domain': host}, stream=True) except Exception as e: log.exception(e) continue if response.status_code != requests.codes.ok: log.error('Could not download badge for %s.', host) continue with tempfile.NamedTemporaryFile() as stream: for block in response.iter_content(1024 * 8): if not block: break stream.write(block) image.image.save(filename, files.File(stream))
def save(self): user = self.user # パスワードとして使う文字群から指定文字数ランダムに選択してチームパスワードとする password = ''.join(random.choice(settings.PASSWORD_LETTERS) for i in range(settings.PASSWORD_LENGTH)) team = Team.objects.create(name=self.cleaned_data['name'], participate_at=self.cleaned_data['participate_at'], password=password, owner=user) user.team = team user.is_student = self.cleaned_data['is_student'] user.display_name = self.cleaned_data['display_name'] user.email = self.cleaned_data['email'] if self.cleaned_data['is_import_github_icon']: resp = requests.get("https://github.com/%s.png" % user.username) if resp.status_code != requests.codes.ok: raise RuntimeError('icon fetch failed') file_name = "{}_{}.png".format(user.id, str(uuid.uuid4())) fp = BytesIO() fp.write(resp.content) user.icon.save(file_name, files.File(fp)) else: user.icon = self.cleaned_data['user_icon'] user.save() return user