# Path: ngnotifier/utils.py
# def hash_over(hostname, over):
#     try:
#         str = hostname + over['subject'] + over['xref'] + over['message-id']
#     except Exception as err:
#         print('Error occured during getting keys of over dictionary.')
#         print('Error: {}'.format(err))
#         return None
# 
#     str = get_encoded(str)
#     if str is None:
#         return None
# 
#     return hashlib.sha1(str).hexdigest()
# 
# def parse_nntp_date(str):
#     # Parse the date of a post
# 
#     # First try all registered possible formats
#     possible_formats = [
#         '%a, %d %b %Y %H:%M:%S',
#         '%a, %d %b %Y %H:%M:%S %z (%Z)',
#         '%a, %d %b %Y %H:%M:%S %Z',
#         '%a, %d %b %Y %H:%M:%S %z',
#         '%d %b %Y %H:%M:%S %z',
#         '%a, %d %b %Y %H:%M %z',
#         '%a, %d %b %y %H:%M:%S %Z',
#         '%d %b %Y %H:%M:%S %Z'
#     ]
#     for f in possible_formats:
#         try:
#             return dt.datetime.strptime(str, f)
#         except:
#             pass
# 
#     # If it failed, try another one by deleting incorrect TZ time
#     try:
#         str_tmp = re.sub('(\+[0-2][0-9]:[0-5][0-9])', '', str)
#         return dt.datetime.strptime(str_tmp, '%a, %d %b %Y %H:%M:%S %z (%Z)')
#     except:
#         # If it didn't worked either, just return current date!
#         print_fail(None, 'Failed parsing date! (' + str + ')')
#         return dt.datetime.now()
# 
# def get_decoded(str):
#     try:
#         str = str.decode('utf-8')
#     except:
#         try:
#             str = str.decode('iso-8859-15', 'surrogateescape')
#         except:
#             try:
#                 str = str.decode("windows-1252")
#             except Exception as err:
#                 # print('Error occured during decoding process.')
#                 # print('Error: {}'.format(err))
#                 return str
#     return str
# 
# def print_done():
#     print(
#         bcolors.OKGREEN, 'Added!', bcolors.ENDC
#     )
# 
# def print_exists():
#     print(
#         bcolors.OKGREEN, 'Already exists!', bcolors.ENDC
#     )
# 
# def print_fail(e, msg=None):
#     print(
#         bcolors.FAIL, 'Failed! ',
#         ((('(' + str(e) + ')') if e else '') if not msg else msg),
#         bcolors.ENDC
#     )
#     import traceback
#     traceback.print_stack()
# 
# def print_msg(type, msg, end=''):
#     if type == 'host':
#         print(bcolors.HEADER, 'Host: ', bcolors.ENDC, end=end, flush=False)
#     elif type == 'group':
#         print(bcolors.HEADER, '  Group: ', bcolors.ENDC, end=end, flush=False)
#     elif type == 'news':
#         print(bcolors.HEADER, '    News: ', bcolors.ENDC, end=end, flush=False)
#     print(msg, '... ', end=end, flush=False)
# 
# def get_father(references):
#     try:
#         return get_list_from_references(references)[-1]
#     except Exception:
#         return ''
# 
# def properly_decode_header(subject):
#     subject = get_encoded(subject).decode("utf8", "replace")
#     contents = make_header(decode_header(subject))
#     return str(contents)
# 
# def get_co(host, port, ssl, user, password, timeout):
#     try:
#         if ssl:
#             co = nntplib.NNTP_SSL(host, port, user, password, timeout=timeout)
#         else:
#             co = nntplib.NNTP(host, port, user, password, timeout=timeout)
#     except Exception as err:
#         raise ConnectionError('Could not connect to the server, please, check '
#                          'your connection ({}).'.format(err))
#     return co


from datetime import datetime
from itertools import chain
from _sha256 import sha224
from uuid import uuid4
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, transaction
from ngnotifier.utils import hash_over, parse_nntp_date, get_decoded,\
    print_done, print_exists, print_fail, print_msg, get_father,\
    properly_decode_header, get_co
from push_notifications.models import APNSDevice, GCMDevice
            if device.registration_id == registration_id:
                device.switch_active()
                break

    def add_ng_group(self, ng_group):
        ng_group.followers.add(self)
        ng_group.save()

    def del_ng_group(self, ng_group):
        ng_group.followers.remove(self)
        ng_group.save()

    def create_session(self, service, registration_id, device_name):
        try:
            if service == 'android':
                DeviceSession.objects.get(gcm_device__registration_id=registration_id).delete()
            else:
                DeviceSession.objects.get(apns_device__registration_id=registration_id).delete()
        except:
            pass
        session = DeviceSession()
        session.service = 'AN' if service == 'android' else 'IO'
        if service == 'android':
            gcm_device = GCMDevice(registration_id=registration_id, user=self, name=device_name)
            gcm_device.save()
            session.gcm_device = gcm_device
        else:
            apns_device = APNSDevice(registration_id=registration_id, user=self, name=device_name)
            apns_device.save()
            session.apns_device = apns_device

# Path: ngnotifier/utils.py
# def hash_over(hostname, over):
#     try:
#         str = hostname + over['subject'] + over['xref'] + over['message-id']
#     except Exception as err:
#         print('Error occured during getting keys of over dictionary.')
#         print('Error: {}'.format(err))
#         return None
# 
#     str = get_encoded(str)
#     if str is None:
#         return None
# 
#     return hashlib.sha1(str).hexdigest()
# 
# def parse_nntp_date(str):
#     # Parse the date of a post
# 
#     # First try all registered possible formats
#     possible_formats = [
#         '%a, %d %b %Y %H:%M:%S',
#         '%a, %d %b %Y %H:%M:%S %z (%Z)',
#         '%a, %d %b %Y %H:%M:%S %Z',
#         '%a, %d %b %Y %H:%M:%S %z',
#         '%d %b %Y %H:%M:%S %z',
#         '%a, %d %b %Y %H:%M %z',
#         '%a, %d %b %y %H:%M:%S %Z',
#         '%d %b %Y %H:%M:%S %Z'
#     ]
#     for f in possible_formats:
#         try:
#             return dt.datetime.strptime(str, f)
#         except:
#             pass
# 
#     # If it failed, try another one by deleting incorrect TZ time
#     try:
#         str_tmp = re.sub('(\+[0-2][0-9]:[0-5][0-9])', '', str)
#         return dt.datetime.strptime(str_tmp, '%a, %d %b %Y %H:%M:%S %z (%Z)')
#     except:
#         # If it didn't worked either, just return current date!
#         print_fail(None, 'Failed parsing date! (' + str + ')')
#         return dt.datetime.now()
# 
# def get_decoded(str):
#     try:
#         str = str.decode('utf-8')
#     except:
#         try:
#             str = str.decode('iso-8859-15', 'surrogateescape')
#         except:
#             try:
#                 str = str.decode("windows-1252")
#             except Exception as err:
#                 # print('Error occured during decoding process.')
#                 # print('Error: {}'.format(err))
#                 return str
#     return str
# 
# def print_done():
#     print(
#         bcolors.OKGREEN, 'Added!', bcolors.ENDC
#     )
# 
# def print_exists():
#     print(
#         bcolors.OKGREEN, 'Already exists!', bcolors.ENDC
#     )
# 
# def print_fail(e, msg=None):
#     print(
#         bcolors.FAIL, 'Failed! ',
#         ((('(' + str(e) + ')') if e else '') if not msg else msg),
#         bcolors.ENDC
#     )
#     import traceback
#     traceback.print_stack()
# 
# def print_msg(type, msg, end=''):
#     if type == 'host':
#         print(bcolors.HEADER, 'Host: ', bcolors.ENDC, end=end, flush=False)
#     elif type == 'group':
#         print(bcolors.HEADER, '  Group: ', bcolors.ENDC, end=end, flush=False)
#     elif type == 'news':
#         print(bcolors.HEADER, '    News: ', bcolors.ENDC, end=end, flush=False)
#     print(msg, '... ', end=end, flush=False)
# 
# def get_father(references):
#     try:
#         return get_list_from_references(references)[-1]
#     except Exception:
#         return ''
# 
# def properly_decode_header(subject):
#     subject = get_encoded(subject).decode("utf8", "replace")
#     contents = make_header(decode_header(subject))
#     return str(contents)
# 
# def get_co(host, port, ssl, user, password, timeout):
#     try:
#         if ssl:
#             co = nntplib.NNTP_SSL(host, port, user, password, timeout=timeout)
#         else:
#             co = nntplib.NNTP(host, port, user, password, timeout=timeout)
#     except Exception as err:
#         raise ConnectionError('Could not connect to the server, please, check '
#                          'your connection ({}).'.format(err))
#     return co


# Path: ngnotifier/models.py
from datetime import datetime
from itertools import chain
from _sha256 import sha224
from uuid import uuid4
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, transaction
from ngnotifier.utils import hash_over, parse_nntp_date, get_decoded,\
    print_done, print_exists, print_fail, print_msg, get_father,\
    properly_decode_header, get_co
from push_notifications.models import APNSDevice, GCMDevice

            if device.registration_id == registration_id:
                device.switch_active()
                break

    def add_ng_group(self, ng_group):
        ng_group.followers.add(self)
        ng_group.save()

    def del_ng_group(self, ng_group):
        ng_group.followers.remove(self)
        ng_group.save()

    def create_session(self, service, registration_id, device_name):
        try:
            if service == 'android':
                DeviceSession.objects.get(gcm_device__registration_id=registration_id).delete()
            else:
                DeviceSession.objects.get(apns_device__registration_id=registration_id).delete()
        except:
            pass
        session = DeviceSession()
        session.service = 'AN' if service == 'android' else 'IO'
        if service == 'android':
            gcm_device = GCMDevice(registration_id=registration_id, user=self, name=device_name)
            gcm_device.save()
            session.gcm_device = gcm_device
        else:
            apns_device = APNSDevice(registration_id=registration_id, user=self, name=device_name)
            apns_device.save()
            session.apns_device = apns_device

        session.save()