Skip to content

Commit

Permalink
Add basic admin and model __str__ methods (#2)
Browse files Browse the repository at this point in the history
* Add basic admin and model __str__ methods

* Bump version number
  • Loading branch information
lukeburden authored Feb 20, 2019
1 parent 5908b0a commit abef054
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
description=description,
long_description=long_description,
long_description_content_type="text/markdown",
version="1.0.4",
version="1.0.5",
license="MIT",
url=url,
packages=find_packages(exclude=["tests", "testproj"]),
Expand Down
75 changes: 75 additions & 0 deletions zengo/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from django.contrib import admin

from . import models


class ZendeskUserAdmin(admin.ModelAdmin):

list_display = [
"zendesk_id",
"name",
"email",
"active",
"role",
"user",
"created_at",
]
list_filter = ["active", "role", "created_at"]
raw_id_fields = ["user"]
search_fields = ["zendesk_id", "name", "email"]

def get_queryset(self, request):
return models.ZendeskUser.objects.all().select_related("user")


class TicketAdmin(admin.ModelAdmin):

list_display = [
"zendesk_id",
"requester",
"subject",
"status",
"created_at",
"updated_at",
]
list_filter = ["status", "created_at", "updated_at"]
# for Django <2.1
raw_id_fields = ["requester"]
# for Django >=2.1
autocomplete_fields = ["requester"]
search_fields = ["ticket__subject", "ticket__zendesk_id"]

def get_queryset(self, request):
return models.Ticket.objects.all().select_related("requester")


class CommentAdmin(admin.ModelAdmin):
list_display = ["zendesk_id", "ticket_id", "author", "body", "public", "created_at"]
list_filter = ["public", "created_at"]
# for Django <2.1
raw_id_fields = ["ticket", "author"]
# for Django >=2.1
autocomplete_fields = ["ticket", "author"]

def get_queryset(self, request):
return models.Comment.objects.all().select_related("ticket", "author")


class EventAdmin(admin.ModelAdmin):
list_display = ["remote_ticket_id", "processing_ok", "created_at", "updated_at"]
list_filter = ["created_at", "updated_at"]
# for Django <2.1
raw_id_fields = ["ticket"]
# for Django >=2.1
autocomplete_fields = ["ticket"]

def processing_ok(self, instance):
return False if instance.error else True

processing_ok.boolean = True


admin.site.register(models.ZendeskUser, ZendeskUserAdmin)
admin.site.register(models.Ticket, TicketAdmin)
admin.site.register(models.Comment, CommentAdmin)
admin.site.register(models.Event, EventAdmin)
25 changes: 25 additions & 0 deletions zengo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from django.contrib.auth import get_user_model
from django.db import models
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible

from konst import Constant, Constants
from konst.models.fields import ConstantChoiceCharField


@python_2_unicode_compatible
class ZendeskUser(models.Model):
"""
Link between a user in Zendesk and the local system.
Expand Down Expand Up @@ -45,6 +47,11 @@ class ZendeskUser(models.Model):
class Meta:
app_label = "zengo"

def __str__(self):
return "{} - {} (id={} zendesk_id={})".format(
self.name, self.email, self.id, self.zendesk_id
)

@property
def photo_url(self):
if self.photos_json:
Expand All @@ -53,6 +60,7 @@ def photo_url(self):
return j.get("content_url")


@python_2_unicode_compatible
class Ticket(models.Model):

id = models.BigAutoField(primary_key=True)
Expand All @@ -79,7 +87,13 @@ class Ticket(models.Model):
class Meta:
app_label = "zengo"

def __str__(self):
return "{} - {} (id={} zendesk_id={})".format(
self.subject, self.status, self.id, self.zendesk_id
)


@python_2_unicode_compatible
class Comment(models.Model):

id = models.BigAutoField(primary_key=True)
Expand All @@ -95,7 +109,13 @@ class Comment(models.Model):
class Meta:
app_label = "zengo"

def __str__(self):
return "{} - {} (id={} zendesk_id={})".format(
self.author, self.public, self.id, self.zendesk_id
)


@python_2_unicode_compatible
class Event(models.Model):
"""
Persist details around a single occurrence of Zendesk hitting the webhook view.
Expand All @@ -122,6 +142,11 @@ class Event(models.Model):
class Meta:
app_label = "zengo"

def __str__(self):
return "{} (id={} remote_ticket_id={})".format(
"Errored" if self.error else "Processed", self.id, self.remote_ticket_id
)

@property
def json(self):
return json.loads(self.raw_data)

0 comments on commit abef054

Please sign in to comment.