112 lines
3.4 KiB
Python
112 lines
3.4 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from datetime import datetime
|
|
from src.database import Base,db_session
|
|
from marshmallow import Schema, fields, post_load
|
|
|
|
import json
|
|
import flask
|
|
|
|
#from src.articles import Article
|
|
|
|
class OrganizationtypeCompSchema(Schema):
|
|
id =fields.Integer()
|
|
name=fields.String()
|
|
|
|
class OrganizationCompSchema(Schema):
|
|
id =fields.Integer()
|
|
name=fields.String()
|
|
organizationtype=fields.Nested(OrganizationtypeCompSchema)
|
|
|
|
class FullSectionSchema(Schema):
|
|
id=fields.Integer()
|
|
url =fields.String(allow_none=True )
|
|
crawlurl =fields.Integer(required=False,allow_none=True )
|
|
#published_date=fields.DateTime()
|
|
#date=fields.DateTime(allow_none=True)
|
|
name=fields.String(required=False,allow_none=True )
|
|
title=fields.String(dump_only=True)
|
|
foreign_name=fields.String()
|
|
group=fields.String(required=False,allow_none=True )
|
|
organization=fields.Nested(OrganizationCompSchema)
|
|
organization_id = fields.Integer()
|
|
updated_at = fields.DateTime()
|
|
created_at = fields.DateTime()
|
|
|
|
# @post_load
|
|
# def make_section(self, data):
|
|
# return Section.deserialize(data)
|
|
|
|
|
|
|
|
class Section(Base):
|
|
__tablename__ = 'sections'
|
|
# id = Column(Integer, primary_key=True)
|
|
url = Column(String(250))
|
|
crawlurl = Column(Integer)
|
|
foreign_name = Column(String(250),unique=True)
|
|
name=Column(String(250))
|
|
group = Column(String(250))
|
|
organization_id=Column(Integer, ForeignKey('organizations.id'))
|
|
organization=relationship("Organization")
|
|
articles=relationship("Article", back_populates="section")
|
|
__schema__=FullSectionSchema
|
|
__whiteattrs__=["name", "organization_id", "url"]
|
|
__jsonattrs__ = ["name", "organization_id", "foreign_name", "title", "url", "name", "id", "organization.name", "organization.organizationtype.name", "updated_at"]
|
|
# def __json__(self):
|
|
# return SectionSchema().dump(self)[0]
|
|
def __init__(self, url=None,fname=None):
|
|
self.url=url
|
|
self.foreign_name=fname
|
|
|
|
# def dict(self):
|
|
# SectionSchema.dump(self)[0]
|
|
|
|
def title(self):
|
|
t=self.name
|
|
if t == None or t.strip()=="":
|
|
t=self.foreign_name
|
|
return t
|
|
# @classmethod
|
|
# def deserialize(cls,data):
|
|
# a=Section()
|
|
# for c in Section.__table__.columns:
|
|
# if data.has_key(c.key):
|
|
# setattr(a, c.key,data[c.key])
|
|
# return a
|
|
|
|
@classmethod
|
|
def find_or_create(cls, fname):
|
|
s=Section.query.filter(Section.foreign_name==fname).first()
|
|
if s is None:
|
|
s=Section(fname)
|
|
db_session.add(s)
|
|
db_session.commit()
|
|
s.foreign_name=fname
|
|
db_session.add(s)
|
|
db_session.commit()
|
|
return s
|
|
|
|
|
|
|
|
class ArticleCompSchema(Schema):
|
|
id=fields.Integer()
|
|
# text=fields.String()
|
|
title=fields.String()
|
|
author=fields.String()
|
|
sourcetype =fields.String()
|
|
image =fields.String()
|
|
url =fields.String()
|
|
published_date=fields.DateTime()
|
|
date=fields.DateTime()
|
|
first_fetched=fields.DateTime()
|
|
section_id=fields.Integer()
|
|
|
|
#from src.articles.model import ArticleCompSchema
|
|
#class SectionSchema(Schema):
|
|
# id=fields.Integer()
|
|
# foreign_name=fields.String()
|
|
# name=fields.String()
|
|
# articles=fields.Nested(ArticleCompSchema,many=True)
|