# Path: ngnotifier/models.py
# class Log(models.Model):
#     TYPES = (
#         ('NU', 'New user'),
#         ('NN', 'New news'),
#         ('NG', 'New group'),
#         ('N', 'Notification'),
#         ('P', 'Post'),
#         ('UN', 'Update news'),
#         ('UG', 'Update groups'),
#         ('US', 'Update stats')
#     )
#     type = models.CharField(choices=TYPES, max_length=2, blank=False, default=None)
#     date = models.DateTimeField(auto_now_add=True)
#     user = models.ForeignKey(User, null=True, default=None)
#     news = models.ForeignKey(NGNews, null=True, default=None)
#     group = models.ForeignKey(NGGroup, null=True, default=None)
#     description = models.TextField(null=True)
# 
# class NGNews(models.Model):
#     groups = models.ManyToManyField('NGGroup')
#     hash = models.TextField(unique=True)
#     subject = models.TextField()
#     contents = models.TextField()
#     email_from = models.TextField()
#     message_id = models.TextField(unique=True)
#     date = models.DateTimeField()
#     lines = models.TextField()
#     xref = models.TextField()
#     references = models.TextField()
#     father = models.TextField()
#     has_children = models.NullBooleanField(default=None)
#     bytes = models.TextField()
#     nb_answers = models.IntegerField(default=0)
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             super().save(*args, **kwargs)
#             new_news_log = Log()
#             new_news_log.type = 'NN'
#             new_news_log.news = self
#             new_news_log.group = self.groups.first()
#             new_news_log.save()
#         else:
#             super().save(*args, **kwargs)
# 
#     def get_host(self):
#         for group in self.groups.first():
#             return group.host.host
# 
#     def get_children(self):
#         return NGNews.objects.filter(father=self.message_id)
# 
#     def get_groups(self):
#         return [c.name for c in self.groups.all()]
# 
#     def add_group(self, group):
#         self.groups.add(group)
#         self.save()
# 
#     # Be careful to call this only once!
#     def add_answer_count(self):
#         if self.father != '':
#             father = NGNews.objects.get(message_id=self.father)
#             father.add_answer_count()
#         self.nb_answers += 1
#         self.save()


import smtplib
import json
from email.mime.text import MIMEText
from ngnotifier.models import Log, NGNews
from push_notifications.models import GCMDevice, APNSDevice
from pushbullet import PushBullet
from ngnotifier.settings import mail_conf as mail, FROM_ADDR, BOT_MSG
from pushbullet.errors import InvalidKeyError
from ngnotifier.settings import PUSH_NOTIFICATIONS_SETTINGS
from apns2.client import APNsClient
from apns2.payload import Payload
                'badge': self.badge,
                'sound': self.sound,
                'news_id': self.news_id
            }
        }


def send_pushs(followers, ng_group, ng_news):
    father = ng_news
    while father.father != '':
        try:
            father = NGNews.objects.get(message_id=father.father)
        except NGNews.DoesNotExists:
            break
    data = {
        'event_type': 'NEW_NEWS',
        'host': ng_group.host.host,
        'newsgroup_id': ng_group.id,
        'newsgroup': ng_group.name,
        'news_id': ng_news.id,
        'news_uid': ng_news.message_id,
        'subject': ng_news.subject,
        'author': ng_news.email_from,
        'creation_date': ng_news.date.strftime("%Y-%m-%dT%H:%M:%S%z")
    }
    
    # Google Android devices
    android_devices = GCMDevice.objects.filter(user__in=followers, active=1)
    
    for android_device in android_devices:

# Path: ngnotifier/models.py
# class Log(models.Model):
#     TYPES = (
#         ('NU', 'New user'),
#         ('NN', 'New news'),
#         ('NG', 'New group'),
#         ('N', 'Notification'),
#         ('P', 'Post'),
#         ('UN', 'Update news'),
#         ('UG', 'Update groups'),
#         ('US', 'Update stats')
#     )
#     type = models.CharField(choices=TYPES, max_length=2, blank=False, default=None)
#     date = models.DateTimeField(auto_now_add=True)
#     user = models.ForeignKey(User, null=True, default=None)
#     news = models.ForeignKey(NGNews, null=True, default=None)
#     group = models.ForeignKey(NGGroup, null=True, default=None)
#     description = models.TextField(null=True)
# 
# class NGNews(models.Model):
#     groups = models.ManyToManyField('NGGroup')
#     hash = models.TextField(unique=True)
#     subject = models.TextField()
#     contents = models.TextField()
#     email_from = models.TextField()
#     message_id = models.TextField(unique=True)
#     date = models.DateTimeField()
#     lines = models.TextField()
#     xref = models.TextField()
#     references = models.TextField()
#     father = models.TextField()
#     has_children = models.NullBooleanField(default=None)
#     bytes = models.TextField()
#     nb_answers = models.IntegerField(default=0)
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             super().save(*args, **kwargs)
#             new_news_log = Log()
#             new_news_log.type = 'NN'
#             new_news_log.news = self
#             new_news_log.group = self.groups.first()
#             new_news_log.save()
#         else:
#             super().save(*args, **kwargs)
# 
#     def get_host(self):
#         for group in self.groups.first():
#             return group.host.host
# 
#     def get_children(self):
#         return NGNews.objects.filter(father=self.message_id)
# 
#     def get_groups(self):
#         return [c.name for c in self.groups.all()]
# 
#     def add_group(self, group):
#         self.groups.add(group)
#         self.save()
# 
#     # Be careful to call this only once!
#     def add_answer_count(self):
#         if self.father != '':
#             father = NGNews.objects.get(message_id=self.father)
#             father.add_answer_count()
#         self.nb_answers += 1
#         self.save()


# Path: ngnotifier/notifs.py
import smtplib
import json
from email.mime.text import MIMEText
from ngnotifier.models import Log, NGNews
from push_notifications.models import GCMDevice, APNSDevice
from pushbullet import PushBullet
from ngnotifier.settings import mail_conf as mail, FROM_ADDR, BOT_MSG
from pushbullet.errors import InvalidKeyError
from ngnotifier.settings import PUSH_NOTIFICATIONS_SETTINGS
from apns2.client import APNsClient
from apns2.payload import Payload

                'badge': self.badge,
                'sound': self.sound,
                'news_id': self.news_id
            }
        }


def send_pushs(followers, ng_group, ng_news):
    father = ng_news
    while father.father != '':
        try:
            father = NGNews.objects.get(message_id=father.father)
        except NGNews.DoesNotExists:
            break
    data = {
        'event_type': 'NEW_NEWS',
        'host': ng_group.host.host,
        'newsgroup_id': ng_group.id,
        'newsgroup': ng_group.name,
        'news_id': ng_news.id,
        'news_uid': ng_news.message_id,
        'subject': ng_news.subject,
        'author': ng_news.email_from,
        'creation_date': ng_news.date.strftime("%Y-%m-%dT%H:%M:%S%z")
    }
    
    # Google Android devices
    android_devices = GCMDevice.objects.filter(user__in=followers, active=1)
    
    for android_device in android_devices:

        log = Log()