# Path: profile/models.py
# class Profile(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     full_name = models.TextField()
#     notifications = models.ForeignKey('profile.Notifications', on_delete=models.CASCADE)
#     subscriptions = models.ManyToManyField('petitions.Petition', related_name='profile_subscriptions', blank=True)
#     petitions_created = models.ManyToManyField('petitions.Petition', related_name='profile_petitions_created', blank=True)
#     petitions_signed = models.ManyToManyField('petitions.Petition', related_name='profile_petitions_signed', blank=True)
#     display_name = models.CharField(max_length=3, blank=True)
#     # 0 - Does not have access, 1 - Has Access
#     has_access = models.PositiveSmallIntegerField(default=1)
# 
#     def __unicode__(self):
#         return self.user.username
# 
# Path: profile/models.py
# class GlobalAlert(models.Model):
#     active = models.BooleanField(default=True)
#     content = models.TextField()


import logging
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models import Q
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
from django.shortcuts import redirect, render
from django.views.decorators.http import require_POST
from .models import Profile
from profile.models import GlobalAlert
@login_required
def remove_superuser(request, user_id):
    if request.user.is_superuser:
        if user_id is not None:
            user = User.objects.get(id=int(user_id))
            user.is_superuser = False
            user.save()
            return HttpResponse(True)
    return HttpResponseForbidden(False)


@require_POST
@login_required
def remove_staff_member(request, user_id):
    if request.user.is_superuser:
        if user_id is not None:
            user = User.objects.get(id=int(user_id))
            user.is_staff = False
            user.save()
            return HttpResponse(True)
    return HttpResponseForbidden(False)


@require_POST
@login_required
def update_alert(request):
    if request.user.is_superuser:
        post = request.POST
        active = True if post.get('alert-active') == 'on' else False
        content = post.get('alert-content')

# Path: profile/models.py
# class Profile(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     full_name = models.TextField()
#     notifications = models.ForeignKey('profile.Notifications', on_delete=models.CASCADE)
#     subscriptions = models.ManyToManyField('petitions.Petition', related_name='profile_subscriptions', blank=True)
#     petitions_created = models.ManyToManyField('petitions.Petition', related_name='profile_petitions_created', blank=True)
#     petitions_signed = models.ManyToManyField('petitions.Petition', related_name='profile_petitions_signed', blank=True)
#     display_name = models.CharField(max_length=3, blank=True)
#     # 0 - Does not have access, 1 - Has Access
#     has_access = models.PositiveSmallIntegerField(default=1)
# 
#     def __unicode__(self):
#         return self.user.username
# 
# Path: profile/models.py
# class GlobalAlert(models.Model):
#     active = models.BooleanField(default=True)
#     content = models.TextField()


# Path: profile/views.py
import logging
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models import Q
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
from django.shortcuts import redirect, render
from django.views.decorators.http import require_POST
from .models import Profile
from profile.models import GlobalAlert

@login_required
def remove_superuser(request, user_id):
    if request.user.is_superuser:
        if user_id is not None:
            user = User.objects.get(id=int(user_id))
            user.is_superuser = False
            user.save()
            return HttpResponse(True)
    return HttpResponseForbidden(False)


@require_POST
@login_required
def remove_staff_member(request, user_id):
    if request.user.is_superuser:
        if user_id is not None:
            user = User.objects.get(id=int(user_id))
            user.is_staff = False
            user.save()
            return HttpResponse(True)
    return HttpResponseForbidden(False)


@require_POST
@login_required
def update_alert(request):
    if request.user.is_superuser:
        post = request.POST
        active = True if post.get('alert-active') == 'on' else False
        content = post.get('alert-content')

        alert, created = GlobalAlert.objects.get_or_create(id=1, defaults={'active': active, 'content': content})