Exemplo n.º 1
0
def renew_previsual(request):
    user = OUser.objects.get(email=request.user.email)
    target_repo = request.GET['target_repo']
    found = False
    repo = None
    for r in user.repos:
        if target_repo == r.url:
            found = True
            repo = r
            break
    if found:
        repo.previsual_page_available = True
        repo.save()
        autoncore.prepare_log(user.email)
        cloning_repo = '[email protected]:%s.git' % (
            target_repo
        )  # cloning_repo should look like '[email protected]:AutonUser/target.git'
        clone_repo(cloning_repo, user.email, dosleep=True)
        repo_dir = os.path.join(autoncore.home, user.email)
        previsual.start_previsual(repo_dir, target_repo)
        return HttpResponseRedirect('/profile')
    return render(
        request, 'msg.html', {
            'msg':
            'You should add the repo while you are logged in before the revisual renewal'
        })
Exemplo n.º 2
0
def renew_previsual(request):
    user = OUser.objects.get(email=request.user.email)
    target_repo = request.GET['target_repo']
    found = False
    repo = None
    for r in user.repos:
        if target_repo == r.url:
            found = True
            repo = r
            break
    if found:
        repo.state = 'Generating Previsualization'
        repo.notes = ''
        repo.previsual_page_available = True
        repo.save()
        autoncore.prepare_log(user.email)
        # cloning_repo should look like '[email protected]:AutonUser/target.git'
        cloning_repo = '[email protected]:%s.git' % target_repo
        sec = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for _ in range(4)
        ])
        folder_name = 'prevclone-' + sec
        clone_repo(cloning_repo, folder_name, dosleep=True)
        repo_dir = os.path.join(autoncore.home, folder_name)
        msg = previsual.start_previsual(repo_dir, target_repo)
        if msg == "":  # not errors
            repo.state = 'Ready'
            repo.save()
            return HttpResponseRedirect('/profile')
        else:
            repo.notes = msg
            repo.state = 'Ready'
            repo.save()
            return render(request, 'msg.html', {'msg': msg})
            #return render(request, 'profile.html', {'repos': user.repos, 'pnames': PublishName.objects.filter(user=user),
            #                                'error': msg})
    repo.state = 'Ready'
    repo.save()
    return render(
        request, 'msg.html', {
            'msg':
            'You should add the repo while you are logged in before the revisual renewal'
        })
Exemplo n.º 3
0
def renew_previsual(request):
    user = OUser.objects.get(email=request.user.email)
    target_repo = request.GET['target_repo']
    found = False
    repo = None
    for r in user.repos:
        if target_repo == r.url:
            found = True
            repo = r
            break
    if found:
        repo.previsual_page_available = True
        repo.save()
        autoncore.prepare_log(user.email)
        cloning_repo = '[email protected]:%s.git' % (
            target_repo)  # cloning_repo should look like '[email protected]:AutonUser/target.git'
        clone_repo(cloning_repo, user.email, dosleep=True)
        repo_dir = os.path.join(autoncore.home, user.email)
        previsual.start_previsual(repo_dir, target_repo)
        return HttpResponseRedirect('/profile')
    return render(request, 'msg.html',
                  {'msg': 'You should add the repo while you are logged in before the revisual renewal'})
Exemplo n.º 4
0
def profile(request):
    print '************* profile ************'
    print str(datetime.today())
    if 'fake' in request.GET and request.user.email == '*****@*****.**':
        print 'faking the user: '******'fake']
        user = OUser.objects.get(email=request.GET['fake'])
    else:
        print 'not faking'
        user = request.user
    # ouser = OUser.objects.get(email=request.user.email)
    error_msg = ''
    if 'repo' in request.GET and 'name' not in request.GET:  # asking for ontologies in a repo
        repo = request.GET['repo']
        print 'repo :<%s>' % (repo)
        print 'got the repo'
        try:
            print 'trying to validate repo'
            hackatt = True
            for repooo in user.repos:
                if repooo.url == repo:
                    hackatt = False
                    break
            if hackatt:  # trying to access a repo that does not belong to the use currently logged in
                return render(request, 'msg.html', {
                    'msg':
                    'This repo is not added, please do so in the main page'
                })
            print 'try to get abs folder'
            if type(autoncore.g) == type(None):
                print 'access token is: ' + request.session['access_token']
                update_g(request.session['access_token'])
            try:
                ontologies = parse_online_repo_for_ontologies(repo)
                print 'ontologies: ' + str(len(ontologies))
                arepo = Repo.objects.get(url=repo)
                pnames = PublishName.objects.filter(user=user, repo=arepo)
                for o in ontologies:
                    print '--------\n%s\n' % o
                    o['published'] = False
                    o['pname'] = ''
                    for pn in pnames:
                        if pn.ontology == o['ontology']:
                            o['published'] = True
                            o['pname'] = pn.name
                            break
                    for d in o:
                        print '   ' + d + ': ' + str(o[d])
                print 'testing redirect'
                print 'will return the Json'
                # html = render(request, 'profile_sliders.html', {'ontologies': ontologies}).content
                # jresponse = JsonResponse({'ontologies': ontologies, 'sliderhtml': html})
                jresponse = JsonResponse({'ontologies': ontologies})
                jresponse.__setitem__('Content-Length', len(jresponse.content))
                sys.stdout.flush()
                sys.stderr.flush()
                return jresponse
            except Exception as e:
                print "exception in getting the ontologies for the repo: " + str(
                    repo)
                print "exception:  " + str(e)
                arepo = Repo.objects.get(url=repo)
                arepo.state = 'Invalid repository'
                arepo.save()
                ontologies = []
                jresponse = JsonResponse({'ontologies': ontologies})
                jresponse.__setitem__('Content-Length', len(jresponse.content))
                sys.stdout.flush()
                sys.stderr.flush()
                return jresponse
        except Exception as e:
            print 'exception: ' + str(e)
    elif 'name' in request.GET:  # publish with a new name
        print request.GET
        name = request.GET['name']
        target_repo = request.GET['repo']
        ontology_rel_path = request.GET['ontology']
        # user = request.user
        found = False
        for r in user.repos:
            if target_repo == r.url:
                found = True
                repo = r
                break
        if found:  # if the repo belongs to the user

            if len(PublishName.objects.filter(name=name)) > 1:
                error_msg = 'a duplicate published names, please contact us ASAP to fix it'

            elif len(PublishName.objects.filter(name=name)) == 0 or (
                    PublishName.objects.get(name=name).user == user
                    and PublishName.objects.get(name=name).repo == repo
                    and PublishName.objects.get(name=name).ontology
                    == ontology_rel_path):
                if (len(PublishName.objects.filter(name=name)) == 0 and len(
                        PublishName.objects.filter(user=user,
                                                   ontology=ontology_rel_path,
                                                   repo=repo)) > 0):
                    error_msg += 'can not reserve multiple names for the same ontology'
                else:
                    autoncore.prepare_log(user.email)
                    # cloning_repo should look like '[email protected]:user/reponame.git'
                    cloning_repo = '[email protected]:%s.git' % target_repo
                    sec = ''.join([
                        random.choice(string.ascii_letters + string.digits)
                        for _ in range(4)
                    ])
                    folder_name = 'pub-' + sec
                    clone_repo(cloning_repo, folder_name, dosleep=True)
                    repo_dir = os.path.join(autoncore.home, folder_name)
                    doc_dir = os.path.join(repo_dir, 'OnToology',
                                           ontology_rel_path[1:],
                                           'documentation')
                    print 'repo_dir: %s' % repo_dir
                    print 'doc_dir: %s' % doc_dir
                    htaccess_f = os.path.join(doc_dir, '.htaccess')
                    if not os.path.exists(htaccess_f):
                        print 'htaccess is not found'
                        #error_msg += 'make sure your ontology has documentation and htaccess'
                        error_msg += 'We couldn\'t reserve your w3id. Please make sure that your ontology has ' \
                                     'documentation and htacess. For that, click on "Generate documentation, diagrams' \
                                     ' and evaluation" on the menu, and once the process is completed, accept the ' \
                                     'pull request on you GitHub repository'
                    else:
                        print 'found htaccesss'
                        f = open(htaccess_f, 'r')
                        file_content = f.read()
                        f.close()
                        f = open(htaccess_f, 'w')
                        for line in file_content.split('\n'):
                            if line[:11] == 'RewriteBase':
                                f.write('RewriteBase /publish/%s \n' % name)
                            else:
                                f.write(line + '\n')
                        f.close()
                        # comm = 'rm -Rf /home/ubuntu/publish/%s' % name
                        comm = 'rm -Rf ' + os.path.join(publish_dir, name)
                        print(comm)
                        call(comm, shell=True)
                        # comm = 'mv %s /home/ubuntu/publish/%s' % (doc_dir, name)
                        comm = 'mv %s %s' % (doc_dir,
                                             os.path.join(publish_dir, name))
                        print comm
                        call(comm, shell=True)
                        if len(PublishName.objects.filter(name=name)) == 0:
                            p = PublishName(name=name,
                                            user=user,
                                            repo=repo,
                                            ontology=ontology_rel_path)
                            p.save()
            else:
                if PublishName.objects.get(name=name).user == user:
                    print 'same user'
                if PublishName.objects.get(name=name).repo == repo:
                    print 'same repo'
                if PublishName.objects.get(
                        name=name).ontology == ontology_rel_path:
                    print 'same ontology'
                error_msg += ' Name already taken'
        else:  # not found
            error_msg += 'You should add this repo to OnToology first'

    elif 'delete-name' in request.GET:
        name = request.GET['delete-name']
        p = PublishName.objects.filter(name=name)
        if len(p) == 0:
            error_msg += 'This name is not reserved'
        elif p[0].user.id == user.id:
            pp = p[0]
            pp.delete()
            pp.save()
            # comm = 'rm -Rf /home/ubuntu/publish/%s' % name
            comm = 'rm -Rf ' + os.path.join(publish_dir, name)
            call(comm, shell=True)
        else:
            error_msg += 'You are trying to delete a name that does not belong to you'
    print 'testing redirect'
    repos = user.repos
    for r in repos:
        try:
            if len(r.url.split('/')) != 2:
                user.update(pull__repos=r)
                r.delete()
                user.save()
                continue
            r.user = r.url.split('/')[0]
            r.rrepo = r.url.split('/')[1]
        except:
            user.update(pull__repos=r)
            user.save()
    request.GET = []
    # if error_msg == '':
    #     return HttpResponseRedirect(reverse('profile'))
    sys.stdout.flush()
    sys.stderr.flush()
    # due to github rate-limiting
    # last_updated = Github().get_repo('OnToology/OnToology').get_issues(state='closed')[0].closed_at
    # return render(request, 'profile.html', {'repos': repos, 'pnames': PublishName.objects.filter(user=user),
    #                                         'error': error_msg, 'last_updated': last_updated})
    return render(
        request, 'profile.html', {
            'repos': repos,
            'pnames': PublishName.objects.filter(user=user),
            'error': error_msg
        })