# Path: status/models.py
# class Incident(BaseModel):
#     """ Creates an incident.  Incidents are displayed at the top of the page until closed. """
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     name = models.CharField(max_length=255)
#     hidden = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.user, self.name)
# 
#     def get_absolute_url(self):
#         return reverse('status:incident_detail', args=[self.pk, ])
# 
#     def get_first_update(self):
#         try:
#             first = self.incidentupdate_set.first()
#         except IncidentUpdate.DoesNotExist:
#             first = None
#         return first
# 
#     def get_latest_update(self):
#         try:
#             latest = self.incidentupdate_set.latest()
#         except IncidentUpdate.DoesNotExist:
#             latest = None
#         return latest
# 
#     class Meta:
#         get_latest_by = 'created'
#         verbose_name = 'Incident'
#         verbose_name_plural = 'Incidents'
#         ordering = ['-created', ]
# 
# class IncidentUpdate(BaseModel):
#     """ Updates about an incident. """
#     incident = models.ForeignKey(Incident)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     status = models.ForeignKey(Status)
#     description = models.TextField()
# 
#     def __unicode__(self):
#         return "%s - %s: %s" % (self.user, self.status, self.description)
# 
#     def get_absolute_url(self):
#         return reverse('status:incident_detail', args=[self.incident.pk, ])
# 
#     class Meta:
#         get_latest_by = 'created'
#         verbose_name = 'Incident Update'
#         verbose_name_plural = 'Incident Updates'
#         ordering = ['created', ]
# 
#     def save(self, *args, **kwargs):
#         """ Update the parent incident update time too. """
#         self.incident.updated = timezone.now()
#         self.incident.save()
#         super(IncidentUpdate, self).save(*args, **kwargs)
# 
# Path: status/forms.py
# class IncidentCreateForm(forms.ModelForm):
#     class Meta:
#         model = Incident
#         fields = ['name']
# 
# class IncidentUpdateCreateForm(forms.ModelForm):
#     class Meta:
#         model = IncidentUpdate
#         fields = ['status', 'description']


from datetime import date, timedelta
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from django.template import RequestContext, Template
from django.template.loader import get_template
from django.utils.decorators import method_decorator
from django.views.generic import (
    MonthArchiveView, YearArchiveView, CreateView, DeleteView, DetailView, ListView, TemplateView
)
from stronghold.decorators import public
from status.models import Incident, IncidentUpdate
from status.forms import IncidentCreateForm, IncidentUpdateCreateForm
import slack
import slack.chat
import logging
                else:
                    description = f.description
                try:
                    message = "<https://%s%s|%s> (%s): %s" % (
                        get_current_site(request),
                        reverse('status:incident_detail', args=[i.pk, ]),
                        i.name,
                        f.status.name,
                        description
                    )
                    send_to_slack(message, username=settings.SLACK_USERNAME, channel=settings.SLACK_CHANNEL)
                except Exception as e:
                    logger.warn('Unable to send to slack: %s' % (e))

            return HttpResponseRedirect('/')
    else:
        form = IncidentCreateForm()
        form2 = IncidentUpdateCreateForm()

    request_context = RequestContext(request)
    request_context.push({'form': form, 'form2': form2})
    t = get_template('status/incident_create_form.html')
    rendered_template = t.render(request_context.flatten(), request)
    return HttpResponse(rendered_template)
    #return get_template('status/incident_create_form.html').render(request_context.flatten(), request)

    #return render(request, template_name='status/incident_create_form.html', context=request_context)


class DashboardView(ListView):

# Path: status/models.py
# class Incident(BaseModel):
#     """ Creates an incident.  Incidents are displayed at the top of the page until closed. """
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     name = models.CharField(max_length=255)
#     hidden = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return "%s - %s" % (self.user, self.name)
# 
#     def get_absolute_url(self):
#         return reverse('status:incident_detail', args=[self.pk, ])
# 
#     def get_first_update(self):
#         try:
#             first = self.incidentupdate_set.first()
#         except IncidentUpdate.DoesNotExist:
#             first = None
#         return first
# 
#     def get_latest_update(self):
#         try:
#             latest = self.incidentupdate_set.latest()
#         except IncidentUpdate.DoesNotExist:
#             latest = None
#         return latest
# 
#     class Meta:
#         get_latest_by = 'created'
#         verbose_name = 'Incident'
#         verbose_name_plural = 'Incidents'
#         ordering = ['-created', ]
# 
# class IncidentUpdate(BaseModel):
#     """ Updates about an incident. """
#     incident = models.ForeignKey(Incident)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     status = models.ForeignKey(Status)
#     description = models.TextField()
# 
#     def __unicode__(self):
#         return "%s - %s: %s" % (self.user, self.status, self.description)
# 
#     def get_absolute_url(self):
#         return reverse('status:incident_detail', args=[self.incident.pk, ])
# 
#     class Meta:
#         get_latest_by = 'created'
#         verbose_name = 'Incident Update'
#         verbose_name_plural = 'Incident Updates'
#         ordering = ['created', ]
# 
#     def save(self, *args, **kwargs):
#         """ Update the parent incident update time too. """
#         self.incident.updated = timezone.now()
#         self.incident.save()
#         super(IncidentUpdate, self).save(*args, **kwargs)
# 
# Path: status/forms.py
# class IncidentCreateForm(forms.ModelForm):
#     class Meta:
#         model = Incident
#         fields = ['name']
# 
# class IncidentUpdateCreateForm(forms.ModelForm):
#     class Meta:
#         model = IncidentUpdate
#         fields = ['status', 'description']


# Path: status/views.py
from datetime import date, timedelta
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from django.template import RequestContext, Template
from django.template.loader import get_template
from django.utils.decorators import method_decorator
from django.views.generic import (
    MonthArchiveView, YearArchiveView, CreateView, DeleteView, DetailView, ListView, TemplateView
)
from stronghold.decorators import public
from status.models import Incident, IncidentUpdate
from status.forms import IncidentCreateForm, IncidentUpdateCreateForm
import slack
import slack.chat
import logging

                else:
                    description = f.description
                try:
                    message = "<https://%s%s|%s> (%s): %s" % (
                        get_current_site(request),
                        reverse('status:incident_detail', args=[i.pk, ]),
                        i.name,
                        f.status.name,
                        description
                    )
                    send_to_slack(message, username=settings.SLACK_USERNAME, channel=settings.SLACK_CHANNEL)
                except Exception as e:
                    logger.warn('Unable to send to slack: %s' % (e))

            return HttpResponseRedirect('/')
    else:
        form = IncidentCreateForm()
        form2 = IncidentUpdateCreateForm()

    request_context = RequestContext(request)
    request_context.push({'form': form, 'form2': form2})
    t = get_template('status/incident_create_form.html')
    rendered_template = t.render(request_context.flatten(), request)
    return HttpResponse(rendered_template)
    #return get_template('status/incident_create_form.html').render(request_context.flatten(), request)

    #return render(request, template_name='status/incident_create_form.html', context=request_context)


class DashboardView(ListView):

    model = Incident