# Path: harnas/contest/forms.py
# class NewsForm(forms.ModelForm):
#     class Meta:
#         model = News
#         fields = ['title', 'description']
# 
# Path: harnas/contest/models.py
# class Contest(models.Model):
#     name = models.CharField(max_length=250, unique=True, null=False)
#     slug = models.SlugField()
#     description = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     modified_at = models.DateTimeField(auto_now=True)
#     creator = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_contest', 'Can view'),
#             ('participate_in_contest', 'Can participate'),
#             ('manage_contest', 'Can manage contest'))
# 
#     def __str__(self):
#         return self.name
# 
# class News(models.Model):
#     title = models.CharField(max_length=250)
#     description = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     modified_at = models.DateTimeField(auto_now=True)
#     author = models.ForeignKey(User)
#     contest = models.ForeignKey(Contest)
# 
#     class Meta:
#         verbose_name_plural = "news"
# 
#     def __str__(self):
#         return self.title


from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import require_http_methods, require_safe
from harnas.contest.forms import NewsForm
from harnas.contest.models import Contest, News


@require_safe
def delete(request, contest_id, news_id):
    if not request.user.has_perm('manage_contest',
                                 Contest.objects.get(pk=contest_id)):
        raise PermissionDenied
    else:
        try:
            news = News.objects.get(pk=news_id)
            news.delete()
            messages.add_message(request,
                                 messages.SUCCESS,
                                 "News deleted successfully.")
        except ObjectDoesNotExist:
            raise PermissionDenied
        return HttpResponseRedirect(
            reverse('contest_details',
                    args=[contest_id]) + '?current_tab=news')


@require_http_methods(['POST', 'GET'])
def edit(request, contest_id, news_id):
    if not request.user.has_perm('manage_contest',
                                 Contest.objects.get(pk=contest_id)):
        raise PermissionDenied
    else:
        try:
            news = News.objects.get(pk=news_id)
            if request.method == 'POST':

# Path: harnas/contest/forms.py
# class NewsForm(forms.ModelForm):
#     class Meta:
#         model = News
#         fields = ['title', 'description']
# 
# Path: harnas/contest/models.py
# class Contest(models.Model):
#     name = models.CharField(max_length=250, unique=True, null=False)
#     slug = models.SlugField()
#     description = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     modified_at = models.DateTimeField(auto_now=True)
#     creator = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_contest', 'Can view'),
#             ('participate_in_contest', 'Can participate'),
#             ('manage_contest', 'Can manage contest'))
# 
#     def __str__(self):
#         return self.name
# 
# class News(models.Model):
#     title = models.CharField(max_length=250)
#     description = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     modified_at = models.DateTimeField(auto_now=True)
#     author = models.ForeignKey(User)
#     contest = models.ForeignKey(Contest)
# 
#     class Meta:
#         verbose_name_plural = "news"
# 
#     def __str__(self):
#         return self.title


# Path: harnas/contest/views/news.py
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import require_http_methods, require_safe
from harnas.contest.forms import NewsForm
from harnas.contest.models import Contest, News



@require_safe
def delete(request, contest_id, news_id):
    if not request.user.has_perm('manage_contest',
                                 Contest.objects.get(pk=contest_id)):
        raise PermissionDenied
    else:
        try:
            news = News.objects.get(pk=news_id)
            news.delete()
            messages.add_message(request,
                                 messages.SUCCESS,
                                 "News deleted successfully.")
        except ObjectDoesNotExist:
            raise PermissionDenied
        return HttpResponseRedirect(
            reverse('contest_details',
                    args=[contest_id]) + '?current_tab=news')


@require_http_methods(['POST', 'GET'])
def edit(request, contest_id, news_id):
    if not request.user.has_perm('manage_contest',
                                 Contest.objects.get(pk=contest_id)):
        raise PermissionDenied
    else:
        try:
            news = News.objects.get(pk=news_id)
            if request.method == 'POST':

                form = NewsForm(request.POST, instance=news)