Browse Source

Prepare for the removal of Member.team_id

pull/214/head
Pēteris Caune 6 years ago
parent
commit
fba8806e97
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 30 additions and 26 deletions
  1. +20
    -18
      hc/accounts/models.py
  2. +1
    -1
      hc/accounts/tests/test_profile.py
  3. +5
    -3
      hc/accounts/views.py
  4. +1
    -1
      hc/api/management/commands/sendalerts.py
  5. +3
    -3
      hc/front/views.py

+ 20
- 18
hc/accounts/models.py View File

@ -129,17 +129,19 @@ class Profile(models.Model):
self.api_key_readonly = urlsafe_b64encode(os.urandom(24)).decode()
self.save()
def checks_from_all_teams(self):
""" Return a queryset of checks from all teams we have access for. """
def checks_from_all_projects(self):
""" Return a queryset of checks from projects we have access to. """
team_ids = set(self.user.memberships.values_list("team_id", flat=True))
team_ids.add(self.id)
is_owner = models.Q(owner=self.user)
is_member = models.Q(member__user=self.user)
q = Project.objects.filter(is_owner | is_member)
project_ids = list(q.values_list("id", flat=True))
from hc.api.models import Check
return Check.objects.filter(user__profile__id__in=team_ids)
return Check.objects.filter(project_id__in=project_ids)
def send_report(self, nag=False):
checks = self.checks_from_all_teams()
checks = self.checks_from_all_projects()
# Has there been a ping in last 6 months?
result = checks.aggregate(models.Max("last_ping"))
@ -221,18 +223,6 @@ class Profile(models.Model):
self.save()
return True
def set_next_nag_date(self):
""" Set next_nag_date for all members of this team. """
is_owner = models.Q(id=self.id)
is_member = models.Q(user__memberships__team=self)
q = Profile.objects.filter(is_owner | is_member)
q = q.exclude(nag_period=NO_NAG)
# Exclude profiles with next_nag_date already set
q = q.filter(next_nag_date__isnull=True)
q.update(next_nag_date=timezone.now() + models.F("nag_period"))
def get_own_project(self):
project = self.user.project_set.first()
if project is None:
@ -261,6 +251,18 @@ class Project(models.Model):
num_used = Check.objects.filter(project__owner=self.owner).count()
return self.owner_profile.check_limit - num_used
def set_next_nag_date(self):
""" Set next_nag_date on profiles of all members of this project. """
is_owner = models.Q(user=self.owner)
is_member = models.Q(user__memberships__project=self)
q = Profile.objects.filter(is_owner | is_member)
q = q.exclude(nag_period=NO_NAG)
# Exclude profiles with next_nag_date already set
q = q.filter(next_nag_date__isnull=True)
q.update(next_nag_date=timezone.now() + models.F("nag_period"))
class Member(models.Model):
team = models.ForeignKey(Profile, models.CASCADE)


+ 1
- 1
hc/accounts/tests/test_profile.py View File

@ -132,7 +132,7 @@ class ProfileTestCase(BaseTestCase):
r = self.client.post("/accounts/profile/", form)
self.assertEqual(r.status_code, 200)
members = self.profile.member_set.all()
members = self.project.member_set.all()
self.assertEqual(members.count(), 2)
frank_found = False


+ 5
- 3
hc/accounts/views.py View File

@ -259,7 +259,7 @@ def profile(request):
farewell_user.profile.current_project = None
farewell_user.profile.save()
Member.objects.filter(team=profile,
Member.objects.filter(project=request.project,
user=farewell_user).delete()
ctx["team_member_removed"] = email
@ -433,6 +433,7 @@ def unsubscribe_reports(request, username):
def switch_team(request, target_username):
try:
target_team = Profile.objects.get(user__username=target_username)
target_project = target_team.get_own_project()
except Profile.DoesNotExist:
return HttpResponseForbidden()
@ -446,13 +447,14 @@ def switch_team(request, target_username):
# Users can switch to teams they are members of.
if not access_ok:
access_ok = request.user.memberships.filter(team=target_team).exists()
q = request.user.memberships.filter(project=target_project)
access_ok = q.exists()
if not access_ok:
return HttpResponseForbidden()
request.profile.current_team = target_team
request.profile.current_project = target_team.get_own_project()
request.profile.current_project = target_project
request.profile.save()
return redirect("hc-checks")


+ 1
- 1
hc/api/management/commands/sendalerts.py View File

@ -21,7 +21,7 @@ def notify(flip_id, stdout):
# Set dates for followup nags
if flip.new_status == "down":
check.project.owner_profile.set_next_nag_date()
check.project.set_next_nag_date()
# Send notifications
errors = flip.send_alerts()


+ 3
- 3
hc/front/views.py View File

@ -70,7 +70,7 @@ def _get_check_for_user(request, code):
if request.user.is_superuser:
q = Check.objects
else:
q = request.profile.checks_from_all_teams()
q = request.profile.checks_from_all_projects()
try:
return q.get(code=code)
@ -87,8 +87,8 @@ def _has_access(request, username):
if request.user.is_superuser:
return True
mq = request.user.memberships
return mq.filter(team__user__username=username).exists()
q = request.user.memberships
return q.filter(project__owner__username=username).exists()
@login_required


Loading…
Cancel
Save