# 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
# 
# Path: cronos/teilar/models.py
# 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


from cronos.common.exceptions import CronosError
from cronos.common.log import log_extra_data
from cronos.posts.models import Authors
from cronos.teilar.models import Websites
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import IntegrityError
import logging
            #    u'Μονάδα Διασφάλισης Ποιότητας ΤΕΙ Λάρισας',
            #    u'http://modip.teilar.gr',
            #    u'info-modip.teilar.gr',
            #],
            #u'%s/%s' % (settings.RSS_PATH, 'career.rss'): [
            #    u'TODO: career',
            #    u'http://www.career.teilar.gr',
            #    u'career@teilar.gr',
            #],
        }
        return websites

    def add_website_to_db(self, url, attributes):
        '''
        Add the website to the DB
        '''
        name = attributes[0]
        rss = attributes[1]
        email = attributes[2]
        website = Websites(
            url = url,
            name = name,
            rss = rss,
            email = email,
        )
        try:
            website.save()
            logger_syslog.info(u'Επιτυχής προσθήκη', extra = log_extra_data(url))
            author = Authors(content_object = website)
            author.save()

# 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
# 
# Path: cronos/teilar/models.py
# 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


# Path: cronos/teilar/management/commands/get_websites.py
from cronos.common.exceptions import CronosError
from cronos.common.log import log_extra_data
from cronos.posts.models import Authors
from cronos.teilar.models import Websites
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import IntegrityError
import logging

            #    u'Μονάδα Διασφάλισης Ποιότητας ΤΕΙ Λάρισας',
            #    u'http://modip.teilar.gr',
            #    u'info-modip.teilar.gr',
            #],
            #u'%s/%s' % (settings.RSS_PATH, 'career.rss'): [
            #    u'TODO: career',
            #    u'http://www.career.teilar.gr',
            #    u'career@teilar.gr',
            #],
        }
        return websites

    def add_website_to_db(self, url, attributes):
        '''
        Add the website to the DB
        '''
        name = attributes[0]
        rss = attributes[1]
        email = attributes[2]
        website = Websites(
            url = url,
            name = name,
            rss = rss,
            email = email,
        )
        try:
            website.save()
            logger_syslog.info(u'Επιτυχής προσθήκη', extra = log_extra_data(url))
            author = Authors(content_object = website)
            author.save()

            logger_syslog.info(u'Επιτυχής προσθήκη', extra = log_extra_data(url))