# Path: cronos/common/exceptions.py
# class CronosError(Exception):
#     '''
#     Custom Exception class for general errors
#     '''
#     def __init__(self, value):
#         self.value = value
#     def __str__(self):
#         return unicode(self.value).encode('utf-8')
# 
# Path: cronos/common/log.py
# def log_extra_data(additional = None):
#     '''
#     Extra data needed by the custom formatter
#     * If the additional argument is a string or unicode, then its value is printed
#     in the log.
#     * If the additiona argument is the request, then from the request it
#     prints the client IP and username if applicable.
#     '''
#     log_extra_data = {
#         'client_ip': '',
#         'id_name': '',
#         'instance_name': settings.INSTANCE_NAME,
#     }
#     if not additional:
#         return log_extra_data
#     if type(additional) == str or type(additional) == unicode:
#         log_extra_data['id_name'] = additional
#     else:
#         request = additional
#         if request.META:
#             log_extra_data['client_ip'] = request.META.get('REMOTE_ADDR', 'None')
#         try:
#             if request.user.is_authenticated():
#                 '''
#                 Handle logged in users
#                 '''
#                 log_extra_data['id_name'] = request.user.name
#         except AttributeError:
#                 pass
#     return log_extra_data
# 
# Path: cronos/posts/models.py
# class Authors(models.Model):
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
#     content_object = generic.GenericForeignKey('content_type', 'object_id')
# 
#     def __unicode__(self):
#         return self.content_object.name
# 
# class Posts(models.Model):
#     title = models.CharField(max_length = 500)
#     pubdate = models.DateTimeField()
#     creator = models.ForeignKey(Authors)
#     unique = models.CharField(max_length = 255, unique = True)
#     url = models.URLField()
#     summary = models.TextField(null = True)
#     enclosure = models.CharField("Attachment URL", max_length = 255, null = True)
# 
#     def __unicode__(self):
#         return self.title
# 
# Path: cronos/teilar/models.py
# class Departments(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 255)
#     email = models.EmailField(null = True)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Teachers(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 255)
#     email = models.EmailField(null = True)
#     department = models.ForeignKey(Departments)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Websites(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 255)
#     rss = models.URLField()
#     email = models.EmailField(null = True)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class EclassLessons(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 500)
#     teacher = models.CharField(max_length = 500)
#     faculty = models.ForeignKey(EclassFaculties)
#     ltype = models.CharField(max_length = 100)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name


from cronos.common.exceptions import CronosError
from cronos.common.log import log_extra_data
from cronos.posts.models import Authors, Posts
from cronos.teilar.models import Departments, Teachers, Websites, EclassLessons
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import MultipleObjectsReturned
from django.core.management.base import BaseCommand
from django.db.utils import IntegrityError
from django.utils import timezone
from time import mktime
import datetime
import MySQLdb
import feedparser
import logging
import warnings
        '''
        Add the Departments from the DB in the list of RSS sites
        EDIT: They don't offer good RSS, I am recreating it, it is included in Websites table
        '''
        #departments = Departments.objects.filter(is_active = True)
        #for department in departments:
        #    authors['http://teilar.gr/tmimata/rss_tmima_news_xml.php?tid=%i' % department.url.split('=')[1] = department.url
        return authors

    def add_post_to_db(self, post, unique_url):
        '''
        Add the post to the DB
        '''
        if post[6]:
            '''
            Try to get the author of the post
            If there is post[6] (which means that
            there is dc:creator) then the author should be
            retrieved from the Teachers or Departments table
            '''
            if post[1].split('/')[3].startswith('person'):
                '''
                Since there are teachers with the same name,
                we need to get the author from the URL
                '''
                post[1] = post[1].replace('teilar', 'www.teilar').replace('_announce', '')
                author = Teachers.objects.get(url = post[1])
            else:
                author = Departments.objects.get(name = post[6])
        else:

# Path: cronos/common/exceptions.py
# class CronosError(Exception):
#     '''
#     Custom Exception class for general errors
#     '''
#     def __init__(self, value):
#         self.value = value
#     def __str__(self):
#         return unicode(self.value).encode('utf-8')
# 
# Path: cronos/common/log.py
# def log_extra_data(additional = None):
#     '''
#     Extra data needed by the custom formatter
#     * If the additional argument is a string or unicode, then its value is printed
#     in the log.
#     * If the additiona argument is the request, then from the request it
#     prints the client IP and username if applicable.
#     '''
#     log_extra_data = {
#         'client_ip': '',
#         'id_name': '',
#         'instance_name': settings.INSTANCE_NAME,
#     }
#     if not additional:
#         return log_extra_data
#     if type(additional) == str or type(additional) == unicode:
#         log_extra_data['id_name'] = additional
#     else:
#         request = additional
#         if request.META:
#             log_extra_data['client_ip'] = request.META.get('REMOTE_ADDR', 'None')
#         try:
#             if request.user.is_authenticated():
#                 '''
#                 Handle logged in users
#                 '''
#                 log_extra_data['id_name'] = request.user.name
#         except AttributeError:
#                 pass
#     return log_extra_data
# 
# Path: cronos/posts/models.py
# class Authors(models.Model):
#     content_type = models.ForeignKey(ContentType)
#     object_id = models.PositiveIntegerField()
#     content_object = generic.GenericForeignKey('content_type', 'object_id')
# 
#     def __unicode__(self):
#         return self.content_object.name
# 
# class Posts(models.Model):
#     title = models.CharField(max_length = 500)
#     pubdate = models.DateTimeField()
#     creator = models.ForeignKey(Authors)
#     unique = models.CharField(max_length = 255, unique = True)
#     url = models.URLField()
#     summary = models.TextField(null = True)
#     enclosure = models.CharField("Attachment URL", max_length = 255, null = True)
# 
#     def __unicode__(self):
#         return self.title
# 
# Path: cronos/teilar/models.py
# class Departments(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 255)
#     email = models.EmailField(null = True)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Teachers(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 255)
#     email = models.EmailField(null = True)
#     department = models.ForeignKey(Departments)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Websites(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 255)
#     rss = models.URLField()
#     email = models.EmailField(null = True)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class EclassLessons(models.Model):
#     url = models.URLField(unique = True)
#     name = models.CharField(max_length = 500)
#     teacher = models.CharField(max_length = 500)
#     faculty = models.ForeignKey(EclassFaculties)
#     ltype = models.CharField(max_length = 100)
#     is_active = models.BooleanField(default = True)
# 
#     def __unicode__(self):
#         return self.name


# Path: cronos/teilar/management/commands/get_rss_feeds.py
from cronos.common.exceptions import CronosError
from cronos.common.log import log_extra_data
from cronos.posts.models import Authors, Posts
from cronos.teilar.models import Departments, Teachers, Websites, EclassLessons
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import MultipleObjectsReturned
from django.core.management.base import BaseCommand
from django.db.utils import IntegrityError
from django.utils import timezone
from time import mktime
import datetime
import MySQLdb
import feedparser
import logging
import warnings

        '''
        Add the Departments from the DB in the list of RSS sites
        EDIT: They don't offer good RSS, I am recreating it, it is included in Websites table
        '''
        #departments = Departments.objects.filter(is_active = True)
        #for department in departments:
        #    authors['http://teilar.gr/tmimata/rss_tmima_news_xml.php?tid=%i' % department.url.split('=')[1] = department.url
        return authors

    def add_post_to_db(self, post, unique_url):
        '''
        Add the post to the DB
        '''
        if post[6]:
            '''
            Try to get the author of the post
            If there is post[6] (which means that
            there is dc:creator) then the author should be
            retrieved from the Teachers or Departments table
            '''
            if post[1].split('/')[3].startswith('person'):
                '''
                Since there are teachers with the same name,
                we need to get the author from the URL
                '''
                post[1] = post[1].replace('teilar', 'www.teilar').replace('_announce', '')
                author = Teachers.objects.get(url = post[1])
            else:
                author = Departments.objects.get(name = post[6])
        else:

            for model in [EclassLessons, Websites]: