Skip to content

Commit

Permalink
feat: decrease count on content unbookmark/unlike
Browse files Browse the repository at this point in the history
- exclude content interaction models from organisation middleware
  • Loading branch information
Muchogoc committed Feb 13, 2023
1 parent 3c4a2d3 commit c82a397
Show file tree
Hide file tree
Showing 20 changed files with 32 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml

- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.1.0
hooks:
- id: black
args: ["-l", "99"]

- repo: https://github.com/timothycrosley/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]
Expand Down
1 change: 0 additions & 1 deletion mycarehub/clients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class Client(AbstractBase):

name = CharField(_("Name of Client"), blank=True, max_length=255)
gender = CharField(choices=GenderChoices.choices, max_length=16, null=True, blank=True)
date_of_birth = DateField(null=True, blank=True)
Expand Down
1 change: 0 additions & 1 deletion mycarehub/common/filters/common_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Meta:


class UserFacilityAllotmentFilter(CommonFieldsFilterset):

search = filters.SearchFilter()

class Meta:
Expand Down
16 changes: 14 additions & 2 deletions mycarehub/common/filters/custom_filter_backends.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from rest_framework import filters

from mycarehub.content.models import ContentItemCategory
from mycarehub.content.models import (
ContentBookmark,
ContentItemCategory,
ContentLike,
ContentShare,
ContentView,
)


class OrganisationFilterBackend(filters.BaseFilterBackend):
"""Users are only allowed to view records in their organisation."""

def filter_queryset(self, request, queryset, view):
"""Filter all records that have an organisation field by user org."""
if queryset.model in [ContentItemCategory]:
if queryset.model in [
ContentItemCategory,
ContentBookmark,
ContentLike,
ContentShare,
ContentView,
]:
return queryset
return queryset.filter(organisation=request.user.organisation)
1 change: 0 additions & 1 deletion mycarehub/common/models/program_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Program(AbstractBase):

name = models.TextField(max_length=100, unique=True)
facilities = models.ManyToManyField(Facility, blank=True, related_name="programs")

Expand Down
1 change: 0 additions & 1 deletion mycarehub/common/serializers/common_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class Meta(BaseSerializer.Meta):


class UserFacilityAllotmentSerializer(BaseSerializer):

user_data = SimpleUserSerializer(source="user", read_only=True)
user_name = serializers.ReadOnlyField(source="user.__str__")
allotment_type_name = serializers.ReadOnlyField(source="get_allotment_type_display")
Expand Down
1 change: 0 additions & 1 deletion mycarehub/common/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def mock_baker(values):
)

class _Baker(baker.Baker):

fields = values

def _get_field_names(self):
Expand Down
1 change: 0 additions & 1 deletion mycarehub/common/views/common_views/drf_common_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def post(self, request, format=None):


class ProgramAPIView(APIView):

queryset = Program.objects.all()
serializer_class = ProgramRegistrationSerializer

Expand Down
1 change: 0 additions & 1 deletion mycarehub/content/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class CustomMedia(AbstractMedia):

organisation = models.ForeignKey(
Organisation,
on_delete=models.PROTECT,
Expand Down
1 change: 0 additions & 1 deletion mycarehub/content/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class ContentItemTagIndexPage(Page):
]

def get_context(self, request):

# Filter by tag
tag = request.GET.get("tag")
content_items = ContentItem.objects.filter(tags__name=tag)
Expand Down
5 changes: 0 additions & 5 deletions mycarehub/content/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@


class ContentItemCategorySerializer(BaseSerializer):

icon_url = serializers.SerializerMethodField()

def get_icon_url(self, category):
Expand All @@ -30,7 +29,6 @@ class Meta(BaseSerializer.Meta):

class ContentViewSerializer(BaseSerializer):
def create(self, validated_data):

with transaction.atomic():
content_view = ContentView.objects.create(**validated_data)

Expand All @@ -46,7 +44,6 @@ class Meta(BaseSerializer.Meta):

class ContentShareSerializer(BaseSerializer):
def create(self, validated_data):

with transaction.atomic():
content_share = ContentShare.objects.create(**validated_data)

Expand All @@ -62,7 +59,6 @@ class Meta(BaseSerializer.Meta):

class ContentLikeSerializer(BaseSerializer):
def create(self, validated_data):

with transaction.atomic():
content_like = ContentLike.objects.create(**validated_data)

Expand All @@ -78,7 +74,6 @@ class Meta(BaseSerializer.Meta):

class ContentBookmarkSerializer(BaseSerializer):
def create(self, validated_data):

with transaction.atomic():
content_bookmark = ContentBookmark.objects.create(**validated_data)

Expand Down
16 changes: 15 additions & 1 deletion mycarehub/content/signals.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_save
from django.db.models import F
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from wagtail.models import Page, Site

from mycarehub.common.models import Program
from mycarehub.content.models import ContentBookmark, ContentItem, ContentLike
from mycarehub.home.models import HomePage

from .models import ContentItemIndexPage
Expand Down Expand Up @@ -58,3 +60,15 @@ def create_program_content_index_page(sender, instance, created, **kwargs):
content_item_index.program = instance

content_item_index.save()


@receiver(post_delete, sender=ContentLike)
def reduce_like_count(sender, instance, *args, **kwargs):
ContentItem.objects.filter(id=instance.content_item.id).update(like_count=F("like_count") - 1)


@receiver(post_delete, sender=ContentBookmark)
def reduce_bookmark_count(sender, instance, *args, **kwargs):
ContentItem.objects.filter(id=instance.content_item.id).update(
bookmark_count=F("bookmark_count") - 1
)
2 changes: 0 additions & 2 deletions mycarehub/content/tests/test_wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def test_set_content_item_program_after_page_create(
request_with_user,
content_item_index,
):

# get a hero image
hero = baker.make("content.CustomImage", _create_files=True)
# set up a content item
Expand Down Expand Up @@ -219,7 +218,6 @@ def test_show_organisation_media_only(request_with_user):


def test_hide_explorer_menu_item_from_non_superuser(request_with_user, admin_user):

hide_explorer_menu_item_from_non_superuser(
request=request_with_user, menu_items=[MenuItem("settings", "sample/")]
)
Expand Down
1 change: 0 additions & 1 deletion mycarehub/content/views/signed_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class SignedURLView(generic.View):
def post(self, request, *args, **kwargs):

# Checks the storage class being used
# Storage class should be Google Cloud Storage for signed url generation to proceed
if get_storage_class().__name__ != "MediaRootGoogleCloudStorage":
Expand Down
1 change: 0 additions & 1 deletion mycarehub/content/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def before_publish_page(request, page):
"""

if page.specific_class == ContentItem:

if page.item_type == "AUDIO_VIDEO" and page.featured_media.count() == 0:
msg = (
"an AUDIO_VIDEO content item must have at least one video "
Expand Down
1 change: 0 additions & 1 deletion mycarehub/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):

form = UserChangeForm
add_form = UserCreationForm
fieldsets = (
Expand Down
1 change: 0 additions & 1 deletion mycarehub/users/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class UserFactory(DjangoModelFactory):

username = Faker("user_name")
email = Faker("email")
name = Faker("name")
Expand Down
1 change: 0 additions & 1 deletion mycarehub/users/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class TestAdapters:
def test_is_open_for_signup(self):

req = MagicMock()
social_login = MagicMock()

Expand Down
3 changes: 0 additions & 3 deletions mycarehub/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class UserDetailView(LoginRequiredMixin, DetailView):

model = User
slug_field = "username"
slug_url_kwarg = "username"
Expand All @@ -21,7 +20,6 @@ class UserDetailView(LoginRequiredMixin, DetailView):


class UserUpdateView(LoginRequiredMixin, ApprovedMixin, SuccessMessageMixin, UpdateView):

model = User
fields = ["name"]
success_message = _("Information successfully updated")
Expand All @@ -37,7 +35,6 @@ def get_object(self):


class UserRedirectView(LoginRequiredMixin, ApprovedMixin, RedirectView):

permanent = False
permission_required = "users.can_view_dashboard"

Expand Down
1 change: 0 additions & 1 deletion mycarehub/utils/tests/test_signed_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


def test_generate_signed_upload_url():

name = generate_media_name(file_name="Test File.mp4")
link = generate_signed_upload_url(
bucket_name=settings.GS_BUCKET_NAME,
Expand Down

0 comments on commit c82a397

Please sign in to comment.