def add_algorithm(request):
    if request.user.is_authenticated:
        if request.user.is_superuser:
            if request.method == 'POST':
                form = AlgorithmsForm(request.POST, request.FILES)

                if form.is_valid():
                    name = form.cleaned_data['name']
                    description = form.cleaned_data['description']
                    created_by = request.user
                    new_algorithm = Algorithm(name=name, description=description, created_by=created_by,
                                                  in_use=True)
                    new_algorithm.save()
                    algo_db_wrapper = AlgorithmManager()
                    algo_db_wrapper.insert_algorithm(name=name, description=description, created_by=created_by,
                                                     file=request.FILES['file'],
                                                     replace_existing=True)

                    return redirect('algorithms_table')
                else:
                    return redirect('algorithms_table')

            else:
                form = AlgorithmsForm()
                return render(request, 'GUIManager/add_model_object.html', {'form': form, 'header': 'Add Algorithm'})
        else:
            return render(request, 'GUIManager/unauth.html')
    else:
        return redirect('login')
Example #2
0
def index(request):
    if request.user.is_authenticated:

        print(request.user.email)

        # Object containing all available algorithms in system
        # all_algorithms = model.Algorithm.objects.all()
        all_algorithms = AlgorithmManager().get_available_algorithms()
        catalog_count = len(DBH.get_user_catalog(request.user.id))

        if model.SystemRecommendations.objects.filter(isactive=True).exists():
            Systemalgo = model.SystemRecommendations.objects.get(isactive=True)
        else:
            Systemalgo = None
        response = render(
            request, 'GUIManager/home.html', {
                'algorithms': all_algorithms,
                'Systemalgo': Systemalgo,
                'catalog_count': catalog_count
            })
        # return render(request, 'GUIManager/home.html', {'first_name':
        # request.user.first_name})
        return response
    else:
        # wrap.get_papers('Biology')
        return redirect('login')
def delete_algorithm(request, algorithm_id):
    if request.user.is_authenticated:
        algorithm = Algorithm.objects.get(pk=algorithm_id)
        if request.user.username == algorithm.created_by.username:
            AlgorithmManager().delete_algorithm(algorithm.name)
            algorithm.name = "{}_{}".format(algorithm.name,timezone.localtime(timezone.now()))
            algorithm.in_use = False
            algorithm.save()
            return redirect('algorithms_table')

        else:
            return redirect('algorithms_table')
    else:
        return redirect('login')
def show_feedback_rating(request):
    if request.user.is_authenticated:
        ratings=DBH.get_feedback_ratings()
        response = render(request,
                          'GUIManager/evaluation_manager.html',
                          {'ratings': ratings,
                           'all_algorithms': AlgorithmManager().get_available_algorithms(),
                           'past_system_algo': model.SystemRecommendations.objects.all(),
                           'ratingmatrix': model.RatingMatrix.objects.count()}
                          )
        return response

    else:

        return redirect('login')
def update_system_recommendation(request):
    if request.user.is_authenticated:
        if request.method == "POST":
            h.set_system_recommendation(request.user,request.POST.get('algo_id'),
                                        request.POST.get('comment'))
            ratings = DBH.get_feedback_ratings()
            all_algorithms = AlgorithmManager().get_available_algorithms()
            response = render(request,
                              'GUIManager/evaluation_manager.html',
                              {'ratings': ratings,
                                'all_algorithms':all_algorithms,
                               'past_system_algo': model.SystemRecommendations.objects.all(),
                               'Success': "Your changes were updated succesfully"
                               }
                              )

            return response

    else:

        return redirect('login')
Example #6
0
def update_algorithm(request, algorithm_id):
    if request.user.is_authenticated:
        algorithm = Algorithm.objects.get(pk=algorithm_id)
        if request.user.username == algorithm.created_by.username:
            if request.user.is_superuser:
                if request.method == 'POST':
                    form = AlgorithmsForm(request.POST, request.FILES)

                    if form.is_valid():
                        algo_manager = AlgorithmManager()
                        name = form.cleaned_data['name']
                        description = form.cleaned_data['description']
                        created_by = request.user
                        new_name = "{}_{}".format(
                            algorithm.name, timezone.localtime(timezone.now()))
                        algo_manager.rename_algorithm(algorithm.name, new_name)
                        algorithm.name = new_name
                        algorithm.in_use = False
                        algorithm.save()
                        updated_algorithm = Algorithm(name=name,
                                                      description=description,
                                                      created_by=created_by,
                                                      in_use=True,
                                                      is_update_of=algorithm)
                        updated_algorithm.save()
                        algo_manager.insert_algorithm(
                            name=name,
                            description=description,
                            created_by=created_by,
                            file=request.FILES['file'],
                            replace_existing=True)
                        return redirect('algorithms_table')
                    else:
                        return redirect('algorithms_table')

                else:
                    form = AlgorithmsForm(instance=algorithm)
                    return render(request, 'GUIManager/add_model_object.html',
                                  {
                                      'form': form,
                                      'header': 'Add Algorithm'
                                  })
        else:
            return redirect('algorithms_table')
    else:
        return redirect('login')
Example #7
0
def getrecommendation(request):
    """
    Gets recommendations from the specified algorithm (step 0)
    and does these things:
    1. adds a query entry into Recommendation Table
    2. And inserts recommendations corresponding to previous query into
    RecommendationResult
    3. Process the result before sending to user
    :param request:
    :return:
    """
    if request.user.is_authenticated:
        if request.method == "POST":
            algoid = request.POST.get('algorithm')
            print(algoid, request.user.id)
            algo = model.Algorithm.objects.get(pk=algoid)
            usr = request.user
            # step 0: get recommendations from the requested algorithm
            # recommendations = h.get_recommendations(userid=usr.id,
            # algoid=algo.id)
            recommendations = AlgorithmManager().get_recommendations(
                user_id=usr.id, algorithm_id=algo.id)
            # try:
            # step 1: insert an entry into Recommendation table
            reclog = model.Recommendation(
                algorithm=algo,
                user=usr,
                input_rating_count=h.get_rating_matrix_size())
            reclog.save()

            # step 2: insert the algorithm results corresponding to previous
            # reclog id
            is_system_rec = request.POST.get('systemreco', None)
            h.add_recommendation(reclog, recommendations, is_system_rec)

            # step 3: Process the result before sending to user
            formatted_result_dict = h.process_user_recommendations(reclog)
            # except:
            #     return redirect('home')

            # pagination related code
            # initial page result is sent as 1
            page = 1
            # divide results into 10 entry per page
            paginator = Paginator(formatted_result_dict, 10)
            # update CTR
            h.update_ctr(reclog.id, page, for_pagination=True)
            try:
                recom = paginator.page(page)
            except PageNotAnInteger:
                recom = paginator.page(1)
            except EmptyPage:
                recom = paginator.page(paginator.num_pages)
            response = render(
                request,
                'GUIManager/displayrecommendation.html',
                {
                    'recommendations': recom,
                    'algo':
                    'ScipR System' if is_system_rec is not None else algo.name,
                    'recommendation': reclog.id,
                    # to display alert message in result page
                    'message': '1'
                })
            return response
        elif request.method == "GET":
            # Pagination related
            # get the recommendation list again
            try:
                recommendation = model.Recommendation.objects.get(
                    id=request.GET.get('rec_id'))
            except:
                return redirect('home')
            if model.SystemAlgorithmRecommendationLog.objects.filter(
                    recommendation=recommendation).exists():
                algo = 'ScipR System'
            else:
                algo = recommendation.algorithm.name
            page = request.GET.get('page', 1)
            # update CTR when a new page is shown
            h.update_ctr(request.GET.get('rec_id'), page, for_pagination=True)
            # divide results into 10 entry per page
            paginator = Paginator(
                h.process_user_recommendations(recommendation), 10)

            try:
                recom = paginator.page(page)
                # to prevent unauthorised attempts to view previous
                # recommendations based on rec_id
                if request.user.id != recommendation.user_id:
                    return redirect('home')
            except PageNotAnInteger:
                recom = paginator.page(1)
            except EmptyPage:
                recom = paginator.page(paginator.num_pages)
            response = render(
                request, 'GUIManager/displayrecommendation.html', {
                    'recommendations': recom,
                    'algo': algo,
                    'recommendation': request.GET.get('rec_id')
                })
            return response
            # return HttpResponseRedirect(reverse('display_recommendation'))

    else:

        return redirect('login')