@login_required(login_url='/login')
def project_view(request, id):
    user = Profile.objects.get(user=request.user)
    project = Project.objects.get(id=id)
    ratings = Rating.objects.filter(project=project).last()
    tech_tags = project.technologies.split(',')
    try:
        rates = Rating.objects.filter(user=user, project=project).first()
    except Rating.DoesNotExist:
        rates = None
    if (rates is None):
        rates_status = False
    else:
        rates_status = True
    form = RateForm()
    rating = None
    if (request.method == 'POST'):
        form = RateForm(request.POST)
        if form.is_valid():
            rate = form.save(commit=False)
            rate.user = user
            rate.project = project
            rate.save()
        try:
            rating = Rating.objects.filter(project_id=id)
        except Rating.DoesNotExist:
            rating = None
        design = form.cleaned_data['design']
        usability = form.cleaned_data['usability']
        content = form.cleaned_data['content']
        rate.average = (((design + usability) + content) / 2)
        rate.save()
        design_ratings = [d.design for d in rating]
        design_average = (sum(design_ratings) / len(design_ratings))
        usability_ratings = [us.usability for us in rating]
        usability_average = (sum(usability_ratings) / len(usability_ratings))
        content_ratings = [content.content for content in rating]
        content_average = (sum(content_ratings) / len(content_ratings))
        score = (((design_average + usability_average) + content_average) / 3)
        rate.design_average = round(design_average, 2)
        rate.usability_average = round(usability_average, 2)
        rate.content_average = round(content_average, 2)
        rate.score = round(score, 2)
        rate.save()
        return redirect('main:project_view', id=project.id)
    else:
        form = RateForm()
    ctx = {'project': project, 'ratings': ratings, 'form': form, 'tech_tags': tech_tags, 'rates_status': rates_status}
    return render(request, 'main/view_project.html', ctx)
@login_required(login_url='/login')
def project_view(request, id):
    user = Profile.objects.get(user=request.user)
    project = Project.objects.get(id=id)
    ratings = Rating.objects.filter(project=project).last()
    tech_tags = project.technologies.split(',')
    try:
        rates = Rating.objects.filter(user=user, project=project).first()
    except Rating.DoesNotExist:
        rates = None
    if (rates is None):
        rates_status = False
    else:
        rates_status = True
    form = RateForm()
    rating = None
    if (request.method == 'POST'):
        form = RateForm(request.POST)
        if form.is_valid():
            rate = form.save(commit=False)
            rate.user = user
            rate.project = project
            rate.save()
        try:
            rating = Rating.objects.filter(project_id=id)
        except Rating.DoesNotExist:
            rating = None
        design = form.cleaned_data['design']
        usability = form.cleaned_data['usability']
        content = form.cleaned_data['content']
        rate.average = (((design + usability) + content) / 2)
        rate.save()
        design_ratings = [d.design for d in rating]
        design_average = (sum(design_ratings) / len(design_ratings))
        usability_ratings = [us.usability for us in rating]
        usability_average = (sum(usability_ratings) / len(usability_ratings))
        content_ratings = [content.content for content in rating]
        content_average = (sum(content_ratings) / len(content_ratings))
        score = (((design_average + usability_average) + content_average) / 3)
        rate.design_average = round(design_average, 2)
        rate.usability_average = round(usability_average, 2)
        rate.content_average = round(content_average, 2)
        rate.score = round(score, 2)
        rate.save()
        return redirect('main:project_view', id=project.id)
    else:
        form = RateForm()
    ctx = {'project': project, 'ratings': ratings, 'form': form, 'tech_tags': tech_tags, 'rates_status': rates_status}
    return render(request, 'main/view_project.html', ctx)