예제 #1
0
def stream_response(request, response, content_length, is_stream=True):
    http_range = request.META.get('HTTP_RANGE')
    content_type = response['Content-Type'].split('/', 1)
    if not (http_range and http_range.startswith('bytes=')
            and http_range.count('-') == 1):
        return response
    if_range = request.META.get('HTTP_IF_RANGE')
    if if_range and if_range != response.get(
            'Last-Modified') and if_range != response.get('ETag'):
        return response
    f = response.file_to_stream

    st_size = content_length

    if is_stream:
        start, end = http_range.split('=')[1].split('-')
        if not start:  # requesting the last N bytes
            start = max(0, st_size - int(end))
            end = ''
        start, end = int(start or 0), int(end or st_size - 1)
        assert 0 <= start < st_size, (start, st_size)
        end = min(end, st_size - 1)
        f.seek(start)
        old_read = f.read
        f.read = lambda n: old_read(min(n, end + 1 - f.tell()))

        response.status_code = 206
        response['Content-Length'] = end + 1 - start
        response['Content-Range'] = 'bytes %d-%d/%d' % (start, end, st_size)

        response['Content-Disposition'] = f'inline; filename=".{content_type}"'
    return response
예제 #2
0
def spotify_callback(request, format=None):
    """ Get response from the above request. """

    code = request.GET.get('code')
    error = request.GET.get('error')

    response = post('https://acounts.spotify.com/api/token', data={
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': REDIRECT_URI,
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET
    }).json()

    access_token = response.get('access_token')
    token_type = response.get('token_type')
    refresh_token = response.get('refresh_token')
    expires_in = response.get('expires_in')
    error = response.get('error')

    # update or create user token
    if not request.session.exists(request.session.session_key):
        request.session.create()

    update_or_create_user_tokens(request.session.session_key, access_token, token_type, expires_in, refresh_token)

    return redirect('frontend:') 
예제 #3
0
def refresh_spotify_token(session_id):
    refresh_token = get_user_tokens(session_id).refresh_token
    response = post(TOKEN_URL, data={
        'grant_type' : 'refresh_token',
        'refresh_token': refresh_token,
        'client_id' : CLIENT_ID,
        'client_secret' : CLIENT_SECRET
    }).json()

    access_token = response.get("access_token")
    token_type = response.get('token_type')
    expires_in = response.get('expires_in')

    update_or_create_user_tokens(session_id, access_token, token_type, expires_in, refresh_token)
예제 #4
0
 def test_list_cards(self):
     client.login(username=self.user.email, password='******')
     
     response = client.get('/profile/cards.json')
     self.assertEqual(response.status_code, 200, response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', response.content)
     self.assertGreater(len(response.json()), 0)
예제 #5
0
 def test_get_customer(self):
     client.login(username=self.user.email, password='******')
     self.user.create_customer()
     response = client.get('/profile/customer.json')
     self.assertEqual(response.get('Content-Type'), 'application/json', msg=response.content)
     self.assertEqual(response.status_code, 200, msg=response.content)
     self.assertIn('id', response.json())
예제 #6
0
def refresh_spotify_token(session_id):
    refresh_token = get_user_tokens(session_id).refresh_token

    response = post('https://accounts.spotify.com/api/token',
                    data={
                        'grant_type': 'refresh_token',
                        'refresh_token': refresh_token,
                        'client_id': CLIENT_ID,
                        'client_secret': CLIENT_SECRET
                    }).json()

    access_token = response.get('access_token')
    token_type = response.get('token_type')
    expires_in = response.get('expires_in')
    #refresh_token = response.get('refresh_token')

    update_or_create_user_tokens(session_id, access_token, token_type,
                                 expires_in, refresh_token)
예제 #7
0
 def test_to_cancel_cash_out(self, *args, **kwargs):
     client.login(username=self.professional.user.email, password='******')
     cash_out = CashOut.create_withdraw(self.professional)
     self.assert_(cash_out.was_withdrawn)
     data = {'uuid': str(cash_out.uuid)}
     response = client.delete(f'/profile/cash_out.json', data=data, content_type='application/json')
     self.assertEqual(response.status_code, 200, response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', response.content)
     self.assertIn('transfer', response.json())
     self.assertIn('was_withdrawn', response.json(), response.json())
     self.assertEqual    (False, response.json()['was_withdrawn'], response.json())
예제 #8
0
 def test_create_card(self):
     client.login(username=self.user.email, password='******')
     self.user.create_customer()
     data = {
         "card_expiration_date": "1122",
         "card_number": "4018720572598048",
         "card_cvv": "123",
         "card_holder_name": "Cersei Lannister"
     }
     response = client.post('/profile/cards.json', data=data, content_type='application/json')
     self.assertEqual(response.status_code, 200, msg=response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', response.content)
     self.assertIn('id', response.json())
예제 #9
0
 def test_create_customer(self):
     client.login(username=self.user.email, password='******')
     data = {
         'cpf': '829.354.190-30',
         'zip_code': '57680-970',
         'neighborhood': 'Centro',
         'street': 'Rua Vereador Artedorio Pinto Dâmaso',
         'street_number': 30
     }
     response = client.post('/profile/customer.json', data=data, content_type='application/json')
     self.assertEqual(response.status_code, 200, msg=response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', msg=response.content)
     self.assertIn('id', response.json())
예제 #10
0
 def test_create_recipient(self):
     client.login(username=self.professional.user.email, password='******')
     recipient = {
         'agency': '0932',
         'agency_dv': '5',
         'bank_code': '341',
         'account': '58054',
         'account_dv': '1',
         'legal_name': 'HOUSE TARGARYEN'
     }
     response = client.post('/profile/recipient.json', data=recipient, content_type='application/json')
     self.assertEqual(response.status_code, 200, response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', response.content)
     self.assertIn('id', response.json())
예제 #11
0
    def stopNotebookJob(self, notebookId: str):
        """
        Stop all paragraphs from a notebook
        """
        isNotebookRunning = True
        while isNotebookRunning:
            requests.delete(
                f"{self.ZEPPELIN_ADDR}/{NOTEBOOKS_ENDPOINT}/job/{notebookId}")
            response = self.getNotebookDetailsWithRetry(notebookId)
            isNotebookRunning = response.get("info",
                                             {}).get("isRunning", False)
            time.sleep(3)

        logger.info(f"Stop notebook {notebookId}")
        return self.__parseResponse(response)
예제 #12
0
def match_image(client, collection_id, image):
    try:
        response = client.search_faces_by_image(CollectionId=collection_id,
                                                Image={'Bytes': image},
                                                MaxFaces=1,
                                                FaceMatchThreshold=90)
    except Exception as e:
        print(e)
        response = None
    matched_faces = None
    face_id = None
    if response:
        matched_faces = response.get('FaceMatches', [])
    if matched_faces:
        face_id = matched_faces[0].get('Face').get('ExternalImageId', None)
    return face_id
예제 #13
0
 def test_pay_job(self):
     self.client.login(request=HttpRequest(),
                       username=self.user.email,
                       password='******')
     data = {'card_index': 0}
     response = self.client.post(f'/jobs/{self.proposal.job.uuid}/pay.json',
                                 data=data,
                                 content_type='application/json')
     self.assertEqual(response.status_code, 200, response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json',
                      response.content)
     self.assertIn('uuid', response.json())
     self.assertIn('transaction', response.json())
     self.assertIn('id', response.json()['transaction'])
     job: Job = Job.objects.get(uuid=str(self.proposal.job.uuid))
     self.assertTrue(job.paid)
예제 #14
0
 def test_to_withdraw(self, mock):
     client.login(username=self.professional.user.email, password='******')
     response = client.post(f'/profile/cash_out.json')
     self.assertEqual(response.status_code, 200, response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', response.content)
     self.assertIn('transfer', response.json())
예제 #15
0
 def test_get_recipient(self):
     client.login(username=self.professional.user.email, password='******')
     response = client.get('/profile/recipient.json')
     self.assertEqual(response.status_code, 200, response.content)
     self.assertEqual(response.get('Content-Type'), 'application/json', response.content)
     self.assertIn('id', response.json())