File size: 2,705 Bytes
61af46f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Post(object):
    id:str
    fecha:str
    titulo:str
    contenido:str
    categorias:list[str]
    def __init__(self, id:str):
        self.__id = id
        self.fecha = str()
        self.titulo = str()
        self.contenido = str()
        self.categorias = ["Nuevos"]

    @property
    def id(self):
        return self.__id
    
    def to_json(self):
        _json = {"id": self.id,
                "fecha": self.fecha,
                "titulo": self.titulo,
                "contenido": self.contenido,
                "categorias": self.categorias}
        return _json

class Base_Blog_Data(object):
    nombre:str
    descripcion:str
    url:str
    posts:list[Post]
    categorias:list[str]
    def __init__(self, id:str):
        self.__id = id
        self.nombre = str()
        self.descripcion = str()
        self.url = str()
        self.posts = []
        self.categorias = ["Nuevos"]
    
    @property
    def id(self):
        return self.__id

    @staticmethod
    def filtrar_por_categorias(posts:list[Post], categorias:list[str]):
        posts_filtrados = []
        for post in posts:
            for cat in categorias:
                if cat in post.categorias and post not in posts_filtrados:
                    posts_filtrados.append(post)
        return posts_filtrados
    
    @staticmethod
    def filtrar_por_categoria(posts:list[Post], categoria:str):
        posts_filtrados = []
        for post in posts:
            if categoria in post.categorias:
                posts_filtrados.append(post)
        return posts_filtrados

    @staticmethod
    def filtrar_por_fecha(posts:list[Post], fecha_inicio:str, fecha_fin:str):
        posts_filtrados = []
        for post in posts:
            if fecha_inicio <= post.fecha <= fecha_fin:
                posts_filtrados.append(post)
        return posts_filtrados
    
    @staticmethod
    def filtrar_por_titulo(posts:list[Post], titulo:str):
        posts_filtrados = []
        for post in posts:
            if titulo in post.titulo:
                posts_filtrados.append(post)
        return posts_filtrados
    
    @staticmethod
    def filtrar_por_contenido(posts:list[Post], contenido:str):
        posts_filtrados = []
        for post in posts:
            if contenido in post.contenido:
                posts_filtrados.append(post)
        return posts_filtrados
    
    def to_json(self):
        _json = {"id": self.id,
                 "nombre": self.nombre,
                 "descripcion": self.descripcion,
                 "url": self.url,
                 "posts": [post.to_json() for post in self.posts],
                 "categorias": self.categorias}
        return _json