# Path: ccvpn/common.py
# def get_client_ip(request):
#     header_name = settings.REAL_IP_HEADER_NAME
# 
#     if header_name:
#         header_name = header_name.replace('-', '_').upper()
#         value = request.META.get('HTTP_' + header_name)
#         if value:
#             return value.split(',', 1)[0]
# 
#     return request.META.get('REMOTE_ADDR')
# 
# Path: tickets/models.py
# class Ticket(models.Model):
#     class Meta:
#         ordering = ('-created',)
# 
#         permissions = (
#             ('view_any_ticket', _("Can view any ticket")),
#             ('reply_any_ticket', _("Can reply to any ticket")),
#             ('view_private_message', _("Can view private messages on tickets")),
#             ('post_private_message', _("Can post private messages on tickets")),
#         )
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
#                              on_delete=models.SET_NULL)
#     category = models.CharField(max_length=16, choices=CATEGORY_CHOICES)
#     subject = models.CharField(max_length=100)
#     created = models.DateTimeField(auto_now_add=True)
#     is_open = models.BooleanField(default=True)
#     closed = models.DateTimeField(blank=True, null=True)
# 
#     @property
#     def status_text(self):
#         if self.closed:
#             return _("Closed")
#         last_msg = self.message_set.last()
#         if last_msg and last_msg.user == self.user:
#             return _("Waiting for staff")
#         else:
#             return _("Open")
# 
#     def get_contacts(self):
#         contacts = TicketNotifyAddress.objects.filter(category=self.category)
#         return [c.address for c in contacts]
# 
#     def notify_new(self, first_message):
#         url = ROOT_URL + reverse('tickets:view', args=(self.id,))
#         subject = _("Ticket:") + " " + self.subject
#         ctx = dict(ticket=self, message=first_message, url=url)
#         notify(subject, 'tickets/mail_support_new.txt', self.get_contacts(), ctx)
# 
#     def notify_reply(self, message):
#         url = ROOT_URL + reverse('tickets:view', args=(self.id,))
#         subject = _("Ticket:") + " " + self.subject
#         ctx = dict(ticket=self, message=message, url=url)
#         if message.user == self.user:
#             notify(subject, 'tickets/mail_support_reply.txt', self.get_contacts(), ctx)
#         if self.user and self.user.email:
#             if message.staff_only and not self.user.has_perm('tickets.view_private_message'):
#                 return
#             notify(subject, 'tickets/mail_user_reply.txt', [self.user.email], ctx)
# 
#     def __str__(self):
#         return self.subject
# 
#     def get_absolute_url(self):
#         return reverse('tickets:view', args=(self.id,))
# 
# class TicketMessage(models.Model):
#     ticket = models.ForeignKey(Ticket, related_name='message_set',
#                                on_delete=models.CASCADE)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
#                              on_delete=models.SET_NULL)
#     remote_addr = models.GenericIPAddressField(blank=True, null=True)
#     created = models.DateTimeField(auto_now_add=True)
#     message = models.TextField()
#     staff_only = models.BooleanField(default=False)
# 
#     class Meta:
#         ordering = ('created',)
# 
# Path: tickets/forms.py
# class NewTicketForm(forms.Form):
#     category = forms.ChoiceField(label=_("Category"), choices=CATEGORY_CHOICES)
#     subject = forms.CharField(label=_("Subject"), min_length=1, max_length=100)
#     message = forms.CharField(label=_("Message"), widget=forms.Textarea)
# 
# class ReplyForm(forms.ModelForm):
#     class Meta:
#         model = TicketMessage
#         fields = ('message',)
# 
# class StaffReplyForm(forms.ModelForm):
#     class Meta:
#         model = TicketMessage
#         fields = ('message', 'staff_only')
# 
#     staff_only = forms.BooleanField(label=_("Private"), required=False)


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http.response import HttpResponseNotFound
from django.utils import timezone
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.utils.translation import ugettext as _
from ccvpn.common import get_client_ip
from .models import Ticket, TicketMessage
from .forms import NewTicketForm, ReplyForm, StaffReplyForm
        messages = ticket.message_set.all()
    else:
        messages = ticket.message_set.filter(staff_only=False)

    if request.method != 'POST':
        ctx = dict(
            staff_reply=request.user.has_perm('tickets.post_private_message'),
            ticket=ticket,
            ticket_messages=messages,
            form=ReplyForm(),
            title=_("Ticket:") + " " + ticket.subject,
        )
        ctx.update(common_context(request))
        return render(request, 'tickets/view.html', ctx)

    if not reply_any_ticket and ticket.user != request.user:
        return HttpResponseNotFound()

    if request.POST.get('close') or request.POST.get('button_close'):
        ticket.is_open = False
        ticket.closed = timezone.now()
        ticket.save()
        return redirect('tickets:view', id=ticket.id)

    if request.POST.get('reopen') or request.POST.get('button_reopen'):
        ticket.is_open = True
        ticket.save()
        return redirect('tickets:view', id=ticket.id)

    if request.user.has_perm('tickets.post_private_message'):

# Path: ccvpn/common.py
# def get_client_ip(request):
#     header_name = settings.REAL_IP_HEADER_NAME
# 
#     if header_name:
#         header_name = header_name.replace('-', '_').upper()
#         value = request.META.get('HTTP_' + header_name)
#         if value:
#             return value.split(',', 1)[0]
# 
#     return request.META.get('REMOTE_ADDR')
# 
# Path: tickets/models.py
# class Ticket(models.Model):
#     class Meta:
#         ordering = ('-created',)
# 
#         permissions = (
#             ('view_any_ticket', _("Can view any ticket")),
#             ('reply_any_ticket', _("Can reply to any ticket")),
#             ('view_private_message', _("Can view private messages on tickets")),
#             ('post_private_message', _("Can post private messages on tickets")),
#         )
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
#                              on_delete=models.SET_NULL)
#     category = models.CharField(max_length=16, choices=CATEGORY_CHOICES)
#     subject = models.CharField(max_length=100)
#     created = models.DateTimeField(auto_now_add=True)
#     is_open = models.BooleanField(default=True)
#     closed = models.DateTimeField(blank=True, null=True)
# 
#     @property
#     def status_text(self):
#         if self.closed:
#             return _("Closed")
#         last_msg = self.message_set.last()
#         if last_msg and last_msg.user == self.user:
#             return _("Waiting for staff")
#         else:
#             return _("Open")
# 
#     def get_contacts(self):
#         contacts = TicketNotifyAddress.objects.filter(category=self.category)
#         return [c.address for c in contacts]
# 
#     def notify_new(self, first_message):
#         url = ROOT_URL + reverse('tickets:view', args=(self.id,))
#         subject = _("Ticket:") + " " + self.subject
#         ctx = dict(ticket=self, message=first_message, url=url)
#         notify(subject, 'tickets/mail_support_new.txt', self.get_contacts(), ctx)
# 
#     def notify_reply(self, message):
#         url = ROOT_URL + reverse('tickets:view', args=(self.id,))
#         subject = _("Ticket:") + " " + self.subject
#         ctx = dict(ticket=self, message=message, url=url)
#         if message.user == self.user:
#             notify(subject, 'tickets/mail_support_reply.txt', self.get_contacts(), ctx)
#         if self.user and self.user.email:
#             if message.staff_only and not self.user.has_perm('tickets.view_private_message'):
#                 return
#             notify(subject, 'tickets/mail_user_reply.txt', [self.user.email], ctx)
# 
#     def __str__(self):
#         return self.subject
# 
#     def get_absolute_url(self):
#         return reverse('tickets:view', args=(self.id,))
# 
# class TicketMessage(models.Model):
#     ticket = models.ForeignKey(Ticket, related_name='message_set',
#                                on_delete=models.CASCADE)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
#                              on_delete=models.SET_NULL)
#     remote_addr = models.GenericIPAddressField(blank=True, null=True)
#     created = models.DateTimeField(auto_now_add=True)
#     message = models.TextField()
#     staff_only = models.BooleanField(default=False)
# 
#     class Meta:
#         ordering = ('created',)
# 
# Path: tickets/forms.py
# class NewTicketForm(forms.Form):
#     category = forms.ChoiceField(label=_("Category"), choices=CATEGORY_CHOICES)
#     subject = forms.CharField(label=_("Subject"), min_length=1, max_length=100)
#     message = forms.CharField(label=_("Message"), widget=forms.Textarea)
# 
# class ReplyForm(forms.ModelForm):
#     class Meta:
#         model = TicketMessage
#         fields = ('message',)
# 
# class StaffReplyForm(forms.ModelForm):
#     class Meta:
#         model = TicketMessage
#         fields = ('message', 'staff_only')
# 
#     staff_only = forms.BooleanField(label=_("Private"), required=False)


# Path: tickets/views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http.response import HttpResponseNotFound
from django.utils import timezone
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.utils.translation import ugettext as _
from ccvpn.common import get_client_ip
from .models import Ticket, TicketMessage
from .forms import NewTicketForm, ReplyForm, StaffReplyForm

        messages = ticket.message_set.all()
    else:
        messages = ticket.message_set.filter(staff_only=False)

    if request.method != 'POST':
        ctx = dict(
            staff_reply=request.user.has_perm('tickets.post_private_message'),
            ticket=ticket,
            ticket_messages=messages,
            form=ReplyForm(),
            title=_("Ticket:") + " " + ticket.subject,
        )
        ctx.update(common_context(request))
        return render(request, 'tickets/view.html', ctx)

    if not reply_any_ticket and ticket.user != request.user:
        return HttpResponseNotFound()

    if request.POST.get('close') or request.POST.get('button_close'):
        ticket.is_open = False
        ticket.closed = timezone.now()
        ticket.save()
        return redirect('tickets:view', id=ticket.id)

    if request.POST.get('reopen') or request.POST.get('button_reopen'):
        ticket.is_open = True
        ticket.save()
        return redirect('tickets:view', id=ticket.id)

    if request.user.has_perm('tickets.post_private_message'):

        form = StaffReplyForm(request.POST)