Skip to content

Commit

Permalink
Merge pull request #134 from ecxia/bugfix/view_profiles_without_own
Browse files Browse the repository at this point in the history
let users view profiles without creating own
  • Loading branch information
ecxia authored Jul 5, 2020
2 parents 0b1edef + 0e747fb commit 04fa3bb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions buddy_mentorship/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core.validators import MinValueValidator, MaxValueValidator


class BuddyRequestmanager(models.Manager):
class BuddyRequestManager(models.Manager):
def find_by_users(
self, requestor: User, requestee: User, request_type: "RequestType"
):
Expand Down Expand Up @@ -38,7 +38,7 @@ class RequestType(models.IntegerChoices):
User, on_delete=models.CASCADE, related_name="requestor"
)
message = models.TextField()
objects = BuddyRequestmanager()
objects = BuddyRequestManager()

def __str__(self):
request_type_str = ["Request", "Offer"][int(self.request_type)]
Expand Down
24 changes: 22 additions & 2 deletions buddy_mentorship/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.urls import reverse
from django.utils import timezone

from .models import BuddyRequest, BuddyRequestmanager, Profile, Skill, Experience
from .models import BuddyRequest, BuddyRequestManager, Profile, Skill, Experience
from .views import can_request, send_request

from apps.users.models import User
Expand Down Expand Up @@ -117,6 +117,26 @@ def test_create_profile(self):
skillRecord2 = Skill.objects.get(skill=skill2)
self.assertEqual(skillRecord2.skill, "seaborn")

def test_profile_without_own(self):
user = User.objects.create_user(email="[email protected]")
profile_user = create_test_users(1, "profile_user", [])[0]
profile = Profile.objects.get(user=profile_user)
c = Client()
c.force_login(user)
response = c.get(f"/profile/{profile.id}",)
assert response.status_code == 200

def test_nonexistent_profile(self):
user = User.objects.create_user(email="[email protected]")
if len(Profile.objects.all()) == 0:
invalid_id = 1
else:
invalid_id = max([profile.id for profile in Profile.objects.all()]) + 100
c = Client()
c.force_login(user)
response = c.get(f"/profile/{invalid_id}",)
assert response.status_code == 404

def test_profile_view(self):
skill1 = Skill.objects.create(skill="python")
skill2 = Skill.objects.create(skill="django")
Expand All @@ -142,7 +162,7 @@ def test_profile_view(self):

c = Client()
c.force_login(user)
response = c.get(f"/profile/",)
response = c.get("/profile/",)
assert response.status_code == 200
assert response.context["can_request"] == False
assert response.context["can_offer"] == False
Expand Down
14 changes: 9 additions & 5 deletions buddy_mentorship/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.postgres.search import SearchQuery, SearchVector, SearchRank
from django.db.models import OuterRef, Subquery
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import ListView
from django.views.generic.edit import DeleteView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
Expand All @@ -27,12 +27,12 @@ def profile(request, profile_id=""):
profile_id = profile.id if profile else None
if profile_id is None:
return redirect("edit_profile")
profile = Profile.objects.get(id=profile_id)
profile = get_object_or_404(Profile, id=profile_id)
existing_request_to_user = None
existing_offer_to_user = None
existing_request_from_user = None
existing_offer_from_user = None
if user_profile != profile:
if user_profile != profile and user_profile != None:
existing_request_from_user = BuddyRequest.objects.find_by_users(
requestor=user_profile.user,
requestee=profile.user,
Expand Down Expand Up @@ -67,6 +67,7 @@ def profile(request, profile_id=""):
"request_type": BuddyRequest.RequestType,
"exp_types": Experience.Type,
}

return render(request, "buddy_mentorship/profile.html", context)


Expand Down Expand Up @@ -104,8 +105,11 @@ def send_request(request, uuid):

# needs to be updated as we expand profile model
def can_request(requestor, requestee):
requestor_profile = Profile.objects.get(user=requestor)
requestee_profile = Profile.objects.get(user=requestee)
requestor_profile = Profile.objects.filter(user=requestor).first()
requestee_profile = Profile.objects.filter(user=requestee).first()

if (not requestor_profile) or (not requestee_profile):
return False

requestor_experiences = Experience.objects.filter(profile=requestor_profile).all()
requestee_experiences = Experience.objects.filter(profile=requestee_profile).all()
Expand Down

0 comments on commit 04fa3bb

Please sign in to comment.