# Path: core/models.py
# class Board(models.Model):
# 
#     name = models.CharField(max_length=100)
#     webhook_activate = models.BooleanField(default=False)
#     trello_board_id = models.CharField(max_length=100)
#     trello_token = models.CharField(max_length=100)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ["name"]
#         verbose_name_plural = "boards"
# 
#     def __str__(self):
#         return self.name
# 
#     def __unicode__(self):
#         return "{}".format(self.name)
# 
# class Webhook(models.Model):
# 
#     name = models.CharField(max_length=50)
#     incoming_webhook_url = models.CharField(max_length=300, unique=True)
# 
#     icon_url = models.CharField(
#         max_length=250,
#         default="http://maffrigby.com/wp-content/uploads/2015/05/trello-icon.png",
#     )
#     username = models.CharField(max_length=30, default="Matterllo")
# 
#     board = models.ManyToManyField(Board)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ["name"]
#         verbose_name_plural = "webhooks"
# 
#     def __str__(self):
#         return "{} :: {}".format(self.name, self.incoming_webhook_url)
# 
#     def __unicode__(self):
#         return "{} :: {}".format(self.name, self.incoming_webhook_url)


from os import getenv
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.utils.text import slugify
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.utils.decorators import method_decorator
from trello import TrelloClient
from core.models import Board, Webhook
                    trello_board_id=board.id,
                    trello_token=token,
                )
                host = getenv("MATTERLLO_HOST") or request.get_host()
                url = "{}://{}/callback/{}/".format(
                    request.scheme, host, b.id
                )
                result = trello_client.create_hook(url, board.id)
                print(
                    "create trello hook :: callback={} :: board={} :: result={}".format(
                        url, slug_board, result
                    )
                )
            return super(BoardView, self).get(request)
        except Exception as e:
            print("unable to display board :: {}".format(e))
            return super(BoardView, self).get(request)

    def get_context_data(self, **kwargs):
        """Fishy way to ensure trello_client is configured."""
        try:
            context = super(BoardView, self).get_context_data(**kwargs)
            token = self.request.GET.get("token")
            if token is None:
                context["board_list"] = None
            trello_client = TrelloClient(
                api_key=settings.TRELLO_APIKEY, token=token
            )
            trello_client.list_boards()
            user = self.request.user

# Path: core/models.py
# class Board(models.Model):
# 
#     name = models.CharField(max_length=100)
#     webhook_activate = models.BooleanField(default=False)
#     trello_board_id = models.CharField(max_length=100)
#     trello_token = models.CharField(max_length=100)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ["name"]
#         verbose_name_plural = "boards"
# 
#     def __str__(self):
#         return self.name
# 
#     def __unicode__(self):
#         return "{}".format(self.name)
# 
# class Webhook(models.Model):
# 
#     name = models.CharField(max_length=50)
#     incoming_webhook_url = models.CharField(max_length=300, unique=True)
# 
#     icon_url = models.CharField(
#         max_length=250,
#         default="http://maffrigby.com/wp-content/uploads/2015/05/trello-icon.png",
#     )
#     username = models.CharField(max_length=30, default="Matterllo")
# 
#     board = models.ManyToManyField(Board)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ["name"]
#         verbose_name_plural = "webhooks"
# 
#     def __str__(self):
#         return "{} :: {}".format(self.name, self.incoming_webhook_url)
# 
#     def __unicode__(self):
#         return "{} :: {}".format(self.name, self.incoming_webhook_url)


# Path: core/views/board.py
from os import getenv
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.utils.text import slugify
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.utils.decorators import method_decorator
from trello import TrelloClient
from core.models import Board, Webhook

                    trello_board_id=board.id,
                    trello_token=token,
                )
                host = getenv("MATTERLLO_HOST") or request.get_host()
                url = "{}://{}/callback/{}/".format(
                    request.scheme, host, b.id
                )
                result = trello_client.create_hook(url, board.id)
                print(
                    "create trello hook :: callback={} :: board={} :: result={}".format(
                        url, slug_board, result
                    )
                )
            return super(BoardView, self).get(request)
        except Exception as e:
            print("unable to display board :: {}".format(e))
            return super(BoardView, self).get(request)

    def get_context_data(self, **kwargs):
        """Fishy way to ensure trello_client is configured."""
        try:
            context = super(BoardView, self).get_context_data(**kwargs)
            token = self.request.GET.get("token")
            if token is None:
                context["board_list"] = None
            trello_client = TrelloClient(
                api_key=settings.TRELLO_APIKEY, token=token
            )
            trello_client.list_boards()
            user = self.request.user

            listboard = Board.objects.filter(trello_token=token)