Skip to content

Mautic logging #1

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

Merged
merged 2 commits into from
Jul 5, 2022
Merged
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
3 changes: 2 additions & 1 deletion app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
'adminsortable2',
'debug_toolbar',
'passport',
'quadraticlands'
'quadraticlands',
'mautic_logging',
]

MIDDLEWARE = [
Expand Down
15 changes: 13 additions & 2 deletions app/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
wall_post_email,
)
from marketing.models import Keyword
from mautic_logging.models import MauticLog
from oauth2_provider.decorators import protected_resource
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError
from perftools.models import JSONStore, StaticJsonEnv
Expand Down Expand Up @@ -7538,9 +7539,19 @@ def mautic_proxy_backend(method="GET", endpoint='', payload=None, params=None):
if payload:
body_unicode = payload.decode('utf-8')
payload = json.loads(body_unicode)
response = getattr(requests, method.lower())(url=url, headers=headers, params=params, data=json.dumps(payload)).json()
http_response = getattr(requests, method.lower())(url=url, headers=headers, params=params, data=json.dumps(payload))
else:
response = getattr(requests, method.lower())(url=url, headers=headers, params=params).json()
http_response = getattr(requests, method.lower())(url=url, headers=headers, params=params)

response = http_response.json()


# Temporary logging of Mautic interaction in order to prepare for a move over from Mautic to Hubspot.
try:
log = MauticLog(method=method, endpoint=endpoint, payload=payload, params=params, status_code=http_response.status_code)
log.save()
except Exception:
pass

return response

Expand Down
Empty file added app/mautic_logging/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions app/mautic_logging/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin

from .models import MauticLog


class MauticLogAdmin(admin.ModelAdmin):
ordering = ['-id']
list_display = ['created_on', 'endpoint', 'status_code', 'method', 'params', 'payload']
search_fields = ['created_on', 'endpoint', 'status_code', 'method', 'params', 'payload']

admin.site.register(MauticLog, MauticLogAdmin)
5 changes: 5 additions & 0 deletions app/mautic_logging/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class MauticLoggingConfig(AppConfig):
name = 'mautic_logging'
32 changes: 32 additions & 0 deletions app/mautic_logging/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.2.24 on 2022-07-05 06:10

import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import economy.models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='MauticLog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(db_index=True, default=economy.models.get_time)),
('modified_on', models.DateTimeField(default=economy.models.get_time)),
('status_code', models.IntegerField()),
('method', models.CharField(max_length=5)),
('endpoint', models.TextField()),
('payload', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict, null=True)),
('params', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict, null=True)),
],
options={
'abstract': False,
},
),
]
Empty file.
16 changes: 16 additions & 0 deletions app/mautic_logging/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib.postgres.fields import JSONField
from django.db import models

from economy.models import SuperModel


class MauticLog(SuperModel):
"""Define the MauticLog model. Used to store requests to the mautic_proxy for further analysis"""
status_code = models.IntegerField()
method = models.CharField(max_length=5)
endpoint = models.TextField()
payload = JSONField(null=True, default=dict, blank=True)
params = JSONField(null=True, default=dict, blank=True)

def __str__(self):
return f"[{self.status_code}] {self.endpoint}"
3 changes: 3 additions & 0 deletions app/mautic_logging/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.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can test the model with some mock responses, or rather update tests related to the mautic_proxy

3 changes: 3 additions & 0 deletions app/mautic_logging/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.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this file