Ejemplo n.º 1
0
def project_upload(request):
    template = 'projects/project_upload.html'

    if not hasattr(request.user, 'googleproject'):
        if settings.FORCE_SINGLE_GOOGLE_ACCOUNT:
            proj = GoogleProject(user=request.user,
                                 project_name=settings.PROJECT_NAME,
                                 project_id=settings.PROJECT_ID,
                                 big_query_dataset=settings.BQ_PROJECT_ID)
            proj.save()

            buck = Bucket(user = request.user,
                          bucket_name=settings.GCLOUD_BUCKET)
            buck.save()
        else :
            template = 'projects/project_request.html'


    usage = get_storage_string(request.user.usage.usage_bytes)
    max_usage = get_storage_string(request.user.usage.usage_bytes_max)

    projects = request.user.project_set.all().filter(active=True)
    context = {
        'usage_monitoring_enabled' : settings.ENFORCE_USER_STORAGE_SIZE,
        'usage_size'  : request.user.usage.usage_bytes,
        'usage'  : usage,
        'max_usage' : max_usage,
        'max_usage_size' : request.user.usage.usage_bytes_max,
        'requested': False,
        'projects': projects
    }
    return render(request, template, context)
Ejemplo n.º 2
0
def import_files(request):
    template = 'projects/project_select.html'
    context = {}
    if request.method == "GET" :
        #check to validate the user has a google project set
        if not hasattr(request.user, 'googleproject'):
            if settings.FORCE_SINGLE_GOOGLE_ACCOUNT:
                proj = GoogleProject(user=request.user,
                                     project_name=settings.PROJECT_NAME,
                                     project_id=settings.PROJECT_ID,
                                     big_query_dataset=settings.BQ_PROJECT_ID)
                proj.save()

                buck = Bucket(user = request.user,
                              bucket_name=settings.GCLOUD_BUCKET)
                buck.save()


        appsession_uri       = request.GET['appsessionuri']
        authorization_code   = request.GET['authorization_code']

        redirect_url         = request.get_host() + reverse('accept_external_files')

        #request.is_secure should work here but Basespace as a bug where it only calls via http
        if "localhost" not in request.get_host() and "localhost" not in request.get_host():
            redirect_url = "https://" + redirect_url
        else :
            redirect_url = "http://" + redirect_url

        print >> sys.stderr, "url used for token : " + redirect_url;

        access_token = get_basespace_access_token(basespace_app_id, basespace_app_secret, redirect_url, authorization_code)
        print >> sys.stderr, "access token : " + str(access_token);
        if access_token :
            # b) fetch appsession info (JSON object), including list of files
            session_uri = basespace_api_uri + appsession_uri + '?access_token=' + access_token
            session_resp = urllib.urlopen(session_uri)
            session_dict = json.loads(session_resp.read())

            # c) gather the file metadata
            files = []
            bs_project = {}
            total_upload_bytes = 0
            if 'Response' in session_dict.keys():
                references = session_dict['Response']['References']
                for ref in references:
                    if ref['Type'] == 'Project':
                        bs_project = {'name'        : ref['Content']['Name'],
                                      'description' : ref['Content']['Description']}
                    elif ref['Type'] == 'File':
                        files.append({"name"    : str(ref['Content']['Name']),
                                      "size"    : get_storage_string(ref['Content']['Size']),
                                      "rawsize" : str(ref['Content']['Size']),
                                      "id"      : str(ref['Content']['Id']),
                                      "href"    : str(ref['HrefContent'])})
                        total_upload_bytes += int(ref['Content']['Size'])

            usage_size = get_storage_string(request.user.usage.usage_bytes)
            max_usage_size = get_storage_string(request.user.usage.usage_bytes_max)
            ownedProjects = request.user.project_set.all().filter(active=True)
            context = {'projects'           : ownedProjects,
                       'files'              : files,
                       'bs_project'         : bs_project,
                       'session_uri'        : appsession_uri,
                       'access_token'       : access_token,
                       'usage_size'         : usage_size,
                       'usage_byte'         : request.user.usage.usage_bytes,
                       'max_usage_bytes'    : request.user.usage.usage_bytes_max,
                       'max_usage_size'     : max_usage_size}

            return render(request, template, context)
        else :
            template = "basespace_error.html"
            return render(request, template, context)
    else :
        template = "403.html"
        return render(request, template, context)