Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit on modified activities class #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
3 changes: 3 additions & 0 deletions kerckhoff/activities/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions kerckhoff/activities/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ActivitiesConfig(AppConfig):
name = 'activities'
Empty file.
44 changes: 44 additions & 0 deletions kerckhoff/activities/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from enum import Enum

from django.contrib.postgres.fields import JSONField, ArrayField
from django.dispatch import receiver
from packages.models import PackageVersion

from .signals import package_updated, package_created

class ActivityType(Enum):
PACKAGE_UPDATED: 'package_updated'
PACKAGE_CREATED: 'package_created'

# Notes
# **kwargs allows you to handle named arguments that you have not defined
# in advance

# the create method "creates" and "saves" the object in a single step

# enum AcitivityType class created for cleaner code

class Activities(models.Model):
# a dict that logs user actions
user = models.ForeignKey(User)
action = JSONField(blank=True, default=dict, null=True)

# post_save is a signal that triggers this func log
@receiver(post_save, sender=PackageVersion)
def log(sender, instance: PackageVersion, created, **kwargs):
if created:
Activities.objects.create(user=instance.creator, action={
"event_type": ActivityType.PACKAGE_CREATED,
"data": {
"package_id": instance.package,
"commit_message": instance.version_description
}
})
else:
Activities.objects.create(user=instance.creator, action={
"event_type": ActivityType.PACKAGE_UPDATED,
"data": {
"package_id": instance.package,
"commit_message": instance.version_description
}
})
4 changes: 4 additions & 0 deletions kerckhoff/activities/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import django.dispatch

package_updated = django.dispatch.Signal(providing_args=["user, package"])
package_created = django.dispatch.Signal(providing_args=["user, pacakge"])
3 changes: 3 additions & 0 deletions kerckhoff/activities/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions kerckhoff/activities/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.