from pprint import pformat
from django.conf import settings
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render, get_object_or_404
from analysis.models import Analysis, Instance
from forms import SearchForm
from flossmole import FSF, FC, GC, GH
from OpenSourceProject.models import OSProjectForge, OSProject, Forge
from ohloh import OhlohProxy
from utils import Forges
import OpenSourceProject
def test(request):
return HttpResponse(OpenSourceProject.flossmole_locale)
def instance_list_html(request, analysis_id):
'''
Part of the required interface
'''
analysis = Analysis.objects.get(pk=analysis_id)
context = {'analysis': analysis}
return render(request, 'OSProject/detail.html', context)
def search_html_form(request, analysis_id):
'''
Part of the required interface
'''
form = SearchForm(initial={'analysis_id': analysis_id}) # An unbound form
return render(request, 'OSProject/search.html', {'form': form})
def search_html(request):
'''
Part of the required interface
'''
form = SearchForm(request.POST)
if form.is_valid(): # All validation rules pass
target = int(str(form.cleaned_data['target']))
name = str(form.cleaned_data['name'])
analysis_id = str(form.cleaned_data['analysis_id'])
projects = []
if target == Forges.FC or target == Forges.ALL:
projects += FC.search(name)
if target == Forges.FSF or target == Forges.ALL:
projects += FSF.search(name)
if target == Forges.GC or target == Forges.ALL:
projects += GC.search(name)
if target == Forges.GH or target == Forges.ALL:
projects += GH.search(name)
if target == Forges.OHLOH or target == Forges.ALL:
projects += OhlohProxy.search(name)
return render(request, 'OSProject/search_results.html', {'projects': projects, 'analysis_id': analysis_id})
def OSProjectForge_delete(request, osprojectforge_id, analysis_id):
pf = get_object_or_404(OSProjectForge, pk=osprojectforge_id)
osp = pf.os_project
i = osp.instance
pf.delete()
if len(osp.osprojectforge_set.all()) == 0:
osp.delete()
i.delete()
return HttpResponseRedirect(reverse('analysis_detail', args=(analysis_id,)))
def OSProjectForge_add(request):
#TODO: check there's just one per forge
analysis_id = int(request.POST.get("analysis_id", ""))
id_forge = int(request.POST.get("id_forge", ""))
datasource_id = request.POST.get("datasource_id", "")
project_name = request.POST.get("project_name", "")
identifier_in_forge = request.POST.get("identifier_in_forge", "")
id_selected_instance = int(request.POST.get("id_selected_instance", ""))
#id_selected_instance is -1 if I have to create a new instance; otherwise it tells to which instance I should add it
if id_selected_instance > 0:
instance = Instance.objects.get(pk=id_selected_instance)
osp=OSProject.objects.get(instance = instance)
for ospf in osp.osprojectforge_set.all():
if ospf.forge.id == id_forge:
messages.add_message(request, messages.INFO, 'Hello world.')
print ('Hello world.')
return HttpResponseRedirect(reverse('analysis_detail', args=(analysis_id,)))
else:
analysis = get_object_or_404(Analysis, pk=analysis_id)
instance=Instance.objects.create(name=project_name, name_for_search="", analysis=analysis)
osp=OSProject.objects.create(name=project_name, instance=instance)
forge=Forge.objects.get(pk=id_forge)
pf=OSProjectForge(name=project_name, os_project=osp, forge=forge, identifier_in_forge=identifier_in_forge, datasource_id=datasource_id)
pf.save()
return HttpResponseRedirect(reverse('analysis_detail', args=(analysis_id,)))
def OSProjectForgeFromResultList_detail(request):
identifier_in_forge = request.POST.get("identifier_in_forge", "")
id_forge = int(request.POST.get("id_forge", ""))
pf = OSProjectForge(identifier_in_forge=identifier_in_forge, forge_id=id_forge)
osprojectforge_info = pf.getProjectInfo()
osprojectforge_info_nice_print = pformat(osprojectforge_info)
return render(request, 'OSProject/osprojectforge_detail.html', {'osprojectforge_info': osprojectforge_info, 'osprojectforge_info_nice_print': osprojectforge_info_nice_print})
def OSProjectForge_detail(request, osprojectforge_id):
pf = get_object_or_404(OSProjectForge, pk=osprojectforge_id)
osprojectforge_info = pf.getProjectInfo()
osprojectforge_info_nice_print = pformat(osprojectforge_info)
return render(request, 'OSProject/osprojectforge_detail.html', {'osprojectforge_info': osprojectforge_info, 'osprojectforge_info_nice_print': osprojectforge_info_nice_print})
# def actual_entity_detail(request, osproject_id):
# '''
# Part of the required interface
# '''
# osp = get_object_or_404(OSProject, pk=osproject_id)
# osproject_info = osp.getProjectInfo()
# return osproject_info
def OSProject_detail(request, osproject_id):
osp = get_object_or_404(OSProject, pk=osproject_id)
osproject_info = osp.getInstanceInfo()
osproject_info_nice_print = pformat(osproject_info)
return render(request, 'OSProject/osproject_detail.html', {'osprojectforge_info': osproject_info, 'osprojectforge_info_nice_print': osproject_info_nice_print})