# Path: core/models.py
# 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)
# 
# 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)
# 
# Path: core/forms.py
# class WebhookCreateForm(forms.ModelForm):
#     def __init__(self, *args, **kwargs):
#         super(WebhookCreateForm, self).__init__(*args, **kwargs)
#         helper = self.helper = FormHelper()
# 
#         layout = helper.layout = Layout()
#         layout.append(Field("name", placeholder="webhook for town-square"))
#         layout.append(
#             Field(
#                 "incoming_webhook_url",
#                 placeholder="https://mattermost.gitlab.com/hooks/b5g6pyoqsjy88fa6kzn7xi1rzy",
#             )
#         )
#         layout.append(FormActions(Submit("save", "Next")))
# 
#         helper.form_show_labels = False
#         helper.form_class = "form-horizontal"
#         helper.field_class = "col-lg-8"
#         helper.help_text_inline = True
# 
#     class Meta:
#         model = Webhook
#         fields = ["name", "incoming_webhook_url"]
#         help_texts = {
#             "name": "The description.",
#             "incoming_webhook_url": 'The generated url from <a href="https://docs.mattermost.com/developer/webhooks-incoming.html#setting-up-existing-integrations">Mattermost webhook</a>.',
#         }


from django.contrib.auth.decorators import login_required
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView
from core.models import Webhook, Board
from core.forms import WebhookCreateForm
# -*- coding: utf-8 -*-



class WebhookDetailView(DetailView):
    model = Webhook


class WebhookCreateView(SuccessMessageMixin, CreateView):
    model = Webhook
    form_class = WebhookCreateForm
    success_message = "%(name)s was created successfully"

    def form_valid(self, form):
        form.instance.board_id = self.kwargs.get("board_id")
        return super(WebhookCreateView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        board_id = self.kwargs.get("board_id")
        context = super(WebhookCreateView, self).get_context_data(**kwargs)

# Path: core/models.py
# 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)
# 
# 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)
# 
# Path: core/forms.py
# class WebhookCreateForm(forms.ModelForm):
#     def __init__(self, *args, **kwargs):
#         super(WebhookCreateForm, self).__init__(*args, **kwargs)
#         helper = self.helper = FormHelper()
# 
#         layout = helper.layout = Layout()
#         layout.append(Field("name", placeholder="webhook for town-square"))
#         layout.append(
#             Field(
#                 "incoming_webhook_url",
#                 placeholder="https://mattermost.gitlab.com/hooks/b5g6pyoqsjy88fa6kzn7xi1rzy",
#             )
#         )
#         layout.append(FormActions(Submit("save", "Next")))
# 
#         helper.form_show_labels = False
#         helper.form_class = "form-horizontal"
#         helper.field_class = "col-lg-8"
#         helper.help_text_inline = True
# 
#     class Meta:
#         model = Webhook
#         fields = ["name", "incoming_webhook_url"]
#         help_texts = {
#             "name": "The description.",
#             "incoming_webhook_url": 'The generated url from <a href="https://docs.mattermost.com/developer/webhooks-incoming.html#setting-up-existing-integrations">Mattermost webhook</a>.',
#         }


# Path: core/views/webhook.py
from django.contrib.auth.decorators import login_required
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView
from core.models import Webhook, Board
from core.forms import WebhookCreateForm

# -*- coding: utf-8 -*-



class WebhookDetailView(DetailView):
    model = Webhook


class WebhookCreateView(SuccessMessageMixin, CreateView):
    model = Webhook
    form_class = WebhookCreateForm
    success_message = "%(name)s was created successfully"

    def form_valid(self, form):
        form.instance.board_id = self.kwargs.get("board_id")
        return super(WebhookCreateView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        board_id = self.kwargs.get("board_id")
        context = super(WebhookCreateView, self).get_context_data(**kwargs)

        context["board"] = Board.objects.get(id=board_id)