"""
Formatters for ressource datas
"""
class RessourceFormatterBase(object):
    """
    This is a dummy ressource formatter, its method "render" will simply return the 
    given instance, no format is applied here
    """
    def __init__(self, instance):
        self.instance = instance
    
    def render(self):
        return self.instance
    
class RessourceFormatterDefault(RessourceFormatterBase):
    """
    Default formatter, its goal is to unify various data types from social networks in 
    only one format to avoid to manage many various cases with all social networks
    
    This formatter is heavily linked to Foundation5 and private CSS that is not shipped 
    in the app, so you will have to implement it if needed
    """
    # CSS classes linked to size mode key name
    size_classes = {
        'small': 'grid-sizer2 h-sizer2',
        'medium': 'grid-sizer3 h-sizer1',
        'large': 'grid-sizer3 h-sizer2',
    }
    # CSS classes linked to social content type key name
    type_classes = {
        'edsa_facebook_fanpage': 'facebook',
        'edsa_twitter': 'twitter',
        'edsa_instagram': 'instagram',
        'edsa_article': 'article',
        'edsa_wordpress_rss': 'article',
        'edsa_pinterest': 'pinterest',
        'edsa_youtube': 'youtube',
        'edsa_youtube_search': 'youtube',
    }
    # CSS classes linked to social content type key name
    text_display_classes = {
        'default' : '',
        'top' : 'item-top',
        'bottom' : 'item-bottom',
    }
    
    def render(self):
        return {
            "id": self.instance.pk,
            "slug": self.instance.slug,
            "css_classes": self.get_css_classes(),
            "title": self.get_title(),
            "description": self.get_description(),
            "button": self.get_button(),
            "has_subblock": self.has_subblock(),
            "author": self.instance.author,
            "date": self.get_date(),
            "image": self.get_image(),
            "media": self.get_content_media(),
            "url": self.get_content_url(),
        }
    
    def has_subblock(self):
        return (self.instance.author or self.instance.ressource_date)
    
    def get_css_classes(self):
        css_classes = []
        if self.instance.view_size == 'default':
            css_classes.append(self.size_classes['medium'])
        else:
            css_classes.append(self.size_classes[self.instance.view_size])
        css_classes.append(self.get_type())
        css_classes.append(self.text_display_classes[self.instance.text_display])
        if self.get_content_url():
            css_classes.append("clickable")
        return " ".join(css_classes)
    
    def get_title(self):
        if self.get_type() in ('twitter', 'facebook', 'instagram'):
            return None
        return self.instance.name
    
    def get_description(self):
        return self.instance.description or self.instance.short_description
    
    def get_content_url(self):
        if self.instance.media_url and self.get_button() is None:
            return self.instance.media_url;
        return None
    
    def get_button(self):
        if self.instance.button_label and self.instance.media_url:
            return {
                'label': self.instance.button_label,
                'color': self.instance.button_color,
                'url': self.instance.media_url,
            }
        return None
    
    def get_type(self):
        return self.type_classes[self.instance.social_type]
    
    def get_author(self):
        if self.get_type() == 'youtube':
            return None
        return self.instance.author
    
    def get_content_media(self):
        if self.instance.media_url:
            return {
                'url': self.instance.media_url,
                'kind': self.instance.media_url_type,
            }
        return None
    
    def get_date(self):
        if self.get_type() == 'youtube':
            return None
        return self.instance.ressource_date
    
    def get_image(self):
        if self.instance.image:
            return self.instance.image.url
        return None

@@ -161,17 +161,16 @@
 turn the
- 
 %0A    giv
@@ -282,25 +282,24 @@
 instance%0A   
- 
 %0A    def ren
@@ -341,17 +341,16 @@
 ance%0A   
- 
 %0Aclass R
@@ -493,17 +493,16 @@
 works in
- 
 %0A    onl
@@ -576,25 +576,24 @@
 networks%0A   
- 
 %0A    This fo
@@ -668,17 +668,16 @@
  shipped
- 
 %0A    in 
@@ -1502,33 +1502,32 @@
 ttom',%0A    %7D%0A   
- 
 %0A    def render(
@@ -2063,24 +2063,124 @@
 tent_url(),%0A
+            %22open_window%22: self.get_open_window(),%0A            %22background%22: self.get_background(),%0A
         %7D%0A  
@@ -2176,25 +2176,24 @@
        %7D%0A   
- 
 %0A    def has
@@ -2278,25 +2278,24 @@
 ce_date)%0A   
- 
 %0A    def get
@@ -2778,25 +2778,24 @@
 classes)%0A   
- 
 %0A    def get
@@ -2933,25 +2933,24 @@
 nce.name%0A   
- 
 %0A    def get
@@ -3044,25 +3044,24 @@
 cription%0A   
- 
 %0A    def get
@@ -3205,33 +3205,32 @@
  return None%0A   
- 
 %0A    def get_but
@@ -3511,32 +3511,442 @@
  return None%0A   
+%0A    def get_background(self):%0A        if self.instance.background_color:%0A            return %7B%0A                'color': self.instance.background_color,%0A            %7D%0A        return None%0A%0A    def get_link(self):%0A        if self.instance.media_url:%0A            return %7B%0A                'url': self.instance.media_url,%0A                'open_window': self.instance.open_window,%0A            %7D%0A        return None%0A  
  %0A    def get_ty
@@ -4014,25 +4014,24 @@
 al_type%5D%0A   
- 
 %0A    def get
@@ -4145,25 +4145,24 @@
 e.author%0A   
- 
 %0A    def get
@@ -4375,25 +4375,24 @@
 urn None%0A   
- 
 %0A    def get
@@ -4516,17 +4516,16 @@
 date%0A   
- 
 %0A    def
@@ -4617,28 +4617,29 @@
 age.url%0A        return None%0A
+%0A

add background color and open_window in formatter