Beispiel #1
0
class Organization(BasicCouchOperations):
    __predicate__ = "organization"
    _id = TextField()
    name = TextField()
    local = ListField(
        DictField(
            Mapping.build(lat=FloatField(),
                          lon=FloatField(),
                          address_str=TextField(),
                          city=TextField())))
    donation_types = ListField(TextField())
    schedule = TextField()
    doc_type = TextField(default="organization")

    all = ViewField(
        __predicate__, '''function(doc){
        if(doc.doc_type == "organization"){
            emit(doc._id, doc);
        }
    }''')

    city = ViewField(
        __predicate__, '''function(doc){
            if (doc.doc_type == "organization"){
                doc.local.forEach(function(local){
                    emit (local.city, doc);
                });
            }
         }''')

    @classmethod
    def sync(cls, db):
        cls.all.sync(db)
        cls.city.sync(db)
class City(Document):
    id = IntegerField()
    country = TextField()
    city = TextField()
    region = TextField()
    postalcode = TextField()
    metrocode = TextField()
    longitude = FloatField()
    latitude = FloatField()
    areacode = TextField()
Beispiel #3
0
class File(Document):
    # Fields of the documment File
    name = TextField()
    extension = TextField()
    size = IntegerField()
    # If the upload_date isn't given, upload_date will be the today date in seconds since the Epoch
    uploaded_date = FloatField(default=time)
    # If the expiring_date isn't given, expiring_date will be today date in a week in seconds since the Epoch
    expiring_date = FloatField(default=expiring)
    owner_id = TextField()
    shared = ListField(TextField())
    md5 = TextField()
Beispiel #4
0
 class UsageData(Document):
     """
     Document that represents the stored data.
     """
     url = TextField()
     ua_browser = TextField()
     ua_language = TextField()
     ua_platform = TextField()
     ua_version = TextField()
     blueprint = TextField()
     view_args = TextField()
     status = IntegerField()
     remote_addr = TextField()
     authorization = BooleanField()
     ip_info = TextField()
     path = TextField()
     speed = FloatField()
     datetime = DateTimeField(default=datetime.now)
     username = TextField()
     track_var = TextField()
     by_date = ViewField(
         'start-end', '''function(doc, req) {
         if (!doc._conflicts) {
             emit(doc.datetime, doc);
         }
     }''')
Beispiel #5
0
class EntityDocument(DocumentBase):
    """
    The couch entity document. It abstracts out the couch related functionality and inherits from the Document class
    of couchdb-python.
    A schema for the entity is enforced here.
    """
    aggregation_paths = DictField()
    geometry = DictField()
    centroid = ListField(FloatField())
    gr_id = TextField()
    short_code = TextField()
    data = DictField()

    def __init__(self,
                 id=None,
                 aggregation_paths=None,
                 geometry=None,
                 centroid=None,
                 gr_id=None,
                 short_code=None):
        DocumentBase.__init__(self, id=id, document_type='Entity')
        self.aggregation_paths = (aggregation_paths
                                  if aggregation_paths is not None else {})
        self._geometry = geometry
        self._centroid = centroid
        self._gr_id = gr_id
        self.short_code = short_code

    @property
    def entity_type(self):
        if attributes.TYPE_PATH in self.aggregation_paths:
            return self.aggregation_paths[attributes.TYPE_PATH]
        else:
            return None

    @entity_type.setter
    def entity_type(self, typ):
        self.aggregation_paths[attributes.TYPE_PATH] = typ

    @property
    def location(self):
        if attributes.GEO_PATH in self.aggregation_paths:
            return self.aggregation_paths[attributes.GEO_PATH]
        else:
            return None

    @location.setter
    def location(self, loc):
        self.aggregation_paths[attributes.GEO_PATH] = loc
Beispiel #6
0
class MediaDocument(DocumentBase):
    name = TextField()
    size = FloatField()
    questionnaire_id = TextField()
    is_preview = BooleanField()

    def __init__(self,
                 id=None,
                 attachment_name=None,
                 attachment_size=None,
                 questionnaire_id=None,
                 is_preview=False):
        DocumentBase.__init__(self, id=id, document_type='MediaDetails')
        self.name = attachment_name
        self.size = attachment_size
        self.questionnaire_id = questionnaire_id
        self.is_preview = is_preview
Beispiel #7
0
class Reading(DocumentBase):
    sensor_id = TextField()
    values = ListField(FloatField())
Beispiel #8
0
class Page(Document):
    type = TextField(default="page")
    url = TextField()
    raw = TextField()
    content = TextField()
    links = ListField(TextField())
    rank = FloatField(default=0)
    last_checked = DateTimeField(default=datetime.now)

    def is_valid(self):
        return (datetime.now() - self.last_checked).days < 7

    def update(self):
        print("updating page")
        
        parse = urlparse(self.url)
        robotstxt = RobotsTxt.get_by_domain(parse.scheme, parse.netloc)
        #if not robotstxt.is_allowed(self.url):
        #    return False

        while cache.get(parse.netloc) is not None:
            time.sleep(1)

        cache.set(parse.netloc, True, 10)

        print("getting: {}".format(self.url))
        resp = requests.get(self.url, headers={'User-Agent':
                                               settings.USER_AGENT})

        ctype = resp.headers['content-type']
        if not ctype.startswith("text/html"):
            print("unsupported content-type: {}".format(ctype))
            return

        print("setting Page.content...")
        self.content = resp.text
        self.raw = resp.text

        self.last_checked = datetime.now()
        self.store(settings.db)

    @staticmethod
    def count():
        r = settings.db.view("page/by_url", limit=0)
        return r.total_rows

    @staticmethod
    def get_top_by_rank(limit=10):
        r = settings.db.view("page/by_rank", limit=limit)
        docs = []
        for row in r.rows:
            docs.append(Page.load(settings.db, row.value))
        return docs

    @staticmethod
    def get_by_url(url, update=True):
        r = settings.db.view("page/by_url", key=url)
        if len(r.rows) == 1:
            doc = Page.load(settings.db, r.rows[0].value)
            if doc.is_valid():
                return doc
        elif not update:
            return None
        else:
            doc = Page(url=url)
        print("Page.get_by_url: doc.update() ...")
        doc.update()

        return doc

    @staticmethod
    def get_id_by_url(url, update=True):
        r = settings.db.view("page/by_url", key=url)
        if len(r) == 1:
            return r.rows[0].value
        else:
            doc = Page.get_by_url(url, update=update)
            if doc is not None:
                return doc.id
            else:
                return None

    @staticmethod
    def get_links_to_url(url):
        return [row.value for row in settings.db.view("page/links_to_url", key=url).rows]
Beispiel #9
0
class Record(Document):
    host = TextField()
    messurement = FloatField()
    time = DateTimeField(default=datetime.now())
Beispiel #10
0
class ContactDocument(DocumentBase):
    """
    The couch entity document. It abstracts out the couch related functionality and inherits from the Document class
    of couchdb-python.
    A schema for the entity is enforced here.
    """
    aggregation_paths = DictField()
    geometry = DictField()
    centroid = ListField(FloatField())
    gr_id = TextField()
    short_code = TextField()
    data = DictField()
    groups = ListField(TextField())
    custom_groups = ListField(TextField())

    def __init__(self,
                 id=None,
                 aggregation_paths=None,
                 geometry=None,
                 centroid=None,
                 gr_id=None,
                 short_code=None):
        DocumentBase.__init__(self, id=id, document_type='Contact')
        self.aggregation_paths = (aggregation_paths
                                  if aggregation_paths is not None else {})
        self._geometry = geometry
        self._centroid = centroid
        self._gr_id = gr_id
        self.short_code = short_code

    @property
    def entity_type(self):
        if attributes.TYPE_PATH in self.aggregation_paths:
            return self.aggregation_paths[attributes.TYPE_PATH]
        else:
            return None

    @property
    def email(self):
        if self.data.get('email'):
            return self.data['email']['value']
        return None

    @property
    def name(self):
        if self.data.get('name'):
            return self.data.get('name')['value']
        return None

    @entity_type.setter
    def entity_type(self, typ):
        self.aggregation_paths[attributes.TYPE_PATH] = typ

    @property
    def location(self):
        if attributes.GEO_PATH in self.aggregation_paths:
            return self.aggregation_paths[attributes.GEO_PATH]
        else:
            return None

    @location.setter
    def location(self, loc):
        self.aggregation_paths[attributes.GEO_PATH] = loc

    def add_group(self, group_name):
        self.groups.append(group_name)

    def add_custom_group(self, group_name):
        if not 'custom_groups' in self._data:
            self._data['custom_groups'] = []

        if group_name not in self.custom_groups:
            self.custom_groups.append(group_name)

    def remove_custom_group(self, group_name):
        if group_name in self.custom_groups:
            self.custom_groups.remove(group_name)
Beispiel #11
0
class Ride(Document):
    doc_type = TextField(default='ride')
    driver = TextField()
    hitchhiker = TextField()
    vehicle = TextField()
    fare = FloatField()
    distance = FloatField()
    origin = TextField()
    destination = TextField()
    driver_origin = TextField()
    driver_destination = TextField()


    def save(self):
        db = DataBase.db()
        self.store(db)

    @classmethod
    def by_user(cls,email):
        db = DataBase.db()
        rides = cls.view(
                        db,
                        '_design/ride/_view/ride-by-driver',
                        key=email,
                        include_docs=True
                        )
        if rides:
            result = []
            for c in rides:
                result.append(c)
            if len(result) > 0:
                return result[0]
            else:
                return None
        else:
            return None
    
    @classmethod
    def by_hitchhiker(cls,email):
        db = DataBase.db()
        rides = cls.view(
                        db,
                        '_design/ride/_view/ride-by-hitchhiker',
                        key=email,
                        include_docs=True
                        )
        if rides:
            result = []
            for c in rides:
                result.append(c)
            if len(result) > 0:
                return result[0]
            else:
                return None
        else:
            return None
    
    @classmethod
    def hitchhiker_history(cls, email):
        db = DataBase.db()
        rides = cls.view(
                        db,
                        '_design/ride/_view/hitch-previous-ride',
                        key=email,
                        include_docs=True
                        )

        if rides:
            result = []

            for i in rides:
                i['origin'] = GoogleApi.get_name(i['origin'])
                i['destination'] = GoogleApi.get_name(i['destination'])
                result.append(i)



            return result

        else:
            return None

    @classmethod
    def driver_history(cls, email):
        db = DataBase.db()
        rides = cls.view(
                        db,
                        '_design/ride/_view/driver-previous-ride',
                        key=email,
                        include_docs=True
                        )
        print rides
        if rides:
            result = []

            for i in rides:
                i['origin'] = GoogleApi.get_name(i['origin'])
                i['destination'] = GoogleApi.get_name(i['destination'])
                result.append(i)

            return result

        else:
            return None

    def calculate_distance(self):
        db = DataBase.db()
        self.distance = GoogleApi.distance(self.origin, self.destination)
        self.store(db)

    def calculate_fare(self):
        db = DataBase.db()
        self.fare =  float(self.distance/ 1000) * 4
        self.store(db)

    def stop(self):
        db = DataBase.db()
        self.doc_type = 'previous_ride'
        self.calculate_distance()
        self.calculate_fare()

        summary = {}
        summary['fare'] = self.fare
        summary['distance'] = self.distance
        summary['car_owner'] = self.driver
        summary['hitchhiker'] = self.hitchhiker
        self.store(db)
        return summary
Beispiel #12
0
class Page(Document):
    type = TextField(default="page")

    url = TextField()

    content = TextField()

    links = ListField(TextField())

    rank = FloatField(default=0)

    last_checked = DateTimeField(default=datetime.now)

    def is_valid(self):
        return (datetime.now() - self.last_checked).days < 7

    def update(self):
        parse = urlparse(self.url)

        robotstxt = RobotsTxt.get_by_domain(parse.scheme, parse.netloc)
        if not robotstxt.is_allowed(self.url):
            return False

        while cache.get(parse.netloc) is not None:
            time.sleep(1)
        cache.set(parse.netloc, True, 10)

        print "getting", self.url
        req = Request(self.url, None, {"User-Agent": settings.USER_AGENT})

        resp = urlopen(req)
        if not resp.info()["Content-Type"].startswith("text/html"):
            return
        self.content = resp.read().decode("utf8")
        self.last_checked = datetime.now()

        self.store(settings.db)

    @staticmethod
    def count():
        r = settings.db.view("page/by_url", limit=0)
        return r.total_rows

    @staticmethod
    def get_top_by_rank(limit=10):
        r = settings.db.view("page/by_rank", limit=limit)
        docs = []
        for row in r.rows:
            docs.append(Page.load(settings.db, row.value))
        return docs

    @staticmethod
    def get_by_url(url, update=True):
        r = settings.db.view("page/by_url", key=url)
        if len(r.rows) == 1:
            doc = Page.load(settings.db, r.rows[0].value)
            if doc.is_valid():
                return doc
        elif not update:
            return None
        else:
            doc = Page(url=url)

        doc.update()

        return doc

    @staticmethod
    def get_id_by_url(url, update=True):
        r = settings.db.view("page/by_url", key=url)
        if len(r) == 1:
            return r.rows[0].value
        else:
            doc = Page.get_by_url(url, update=update)
            if doc is not None:
                return doc.id
            else:
                return None

    @staticmethod
    def get_links_to_url(url):
        return [
            row.value
            for row in settings.db.view("page/links_to_url", key=url).rows
        ]