Exemplo n.º 1
0
    def __create_cards(self, player, access_token, limit=20):
        client = FacebookClient(access_token)
        player_friends = client.get_friends_attributes()
        random.shuffle(player_friends)

        if player.user.first_name == 'Atol':
            print('DEBUG DOIDO')
            nada = 0
            for p in player_friends:
                if (p['name'] == 'Natan Costa Lima'):
                    aux = player_friends[0]
                    player_friends[0] = p
                    player_friends[nada] = aux
                nada += 1

        count = 1
        logging.debug(player_friends)
        for f in player_friends:
            likes_count = f['likes_count']
            friend_count = f['friend_count']
            if not likes_count or not friend_count:
                continue

            c = Card(player=player,
                     name=f['name'],
                     pic_square=f['pic_square'],
                     order=count)
            c.save()
            Attribute(card=c, name="likes", attr=likes_count).save()
            Attribute(card=c, name="friends", attr=friend_count).save()
            count += 1
            if count == limit + 1:
                break

        return player_friends
Exemplo n.º 2
0
def ParseGraph(data, image):
    objects = []
    object_map = {}
    relationships = []
    attributes = []
    # Create the Objects
    for obj in data['bounding_boxes']:
        names = []
        synsets = []
        for s in obj['boxed_objects']:
            names.append(s['name'])
            synsets.append(ParseSynset(s['object_canon']))
            object_ = Object(obj['id'], obj['x'], obj['y'], obj['width'],
                             obj['height'], names, synsets)
            object_map[obj['id']] = object_
        objects.append(object_)
    # Create the Relationships
    for rel in data['relationships']:
        relationships.append(Relationship(rel['id'], object_map[rel['subject']], \
            rel['predicate'], object_map[rel['object']], ParseSynset(rel['relationship_canon'])))
    # Create the Attributes
    for atr in data['attributes']:
        attributes.append(Attribute(atr['id'], object_map[atr['subject']], \
            atr['attribute'], ParseSynset(atr['attribute_canon'])))
    return Graph(image, objects, relationships, attributes)
Exemplo n.º 3
0
def ParseGraphVRD(d):
    image = Image(d['photo_id'], d['filename'], d['width'], d['height'], '',
                  '')

    id2obj = {}
    objs = []
    rels = []
    atrs = []

    for i, o in enumerate(d['objects']):
        b = o['bbox']
        obj = Object(i, b['x'], b['y'], b['w'], b['h'], o['names'], [])
        id2obj[i] = obj
        objs.append(obj)

        for j, a in enumerate(o['attributes']):
            atrs.append(Attribute(j, obj, a['attribute'], []))

    for i, r in enumerate(d['relationships']):
        s = id2obj[r['objects'][0]]
        o = id2obj[r['objects'][1]]
        v = r['relationship']
        rels.append(Relationship(i, s, v, o, []))

    return Graph(image, objs, rels, atrs)
Exemplo n.º 4
0
def ParseGraphLocal(data, image, verbose=False):
    global count_skips
    objects = []
    object_map = {}
    relationships = []
    attributes = []

    for obj in data['objects']:
        object_map, o_ = MapObject(object_map, obj)
        objects.append(o_)
    for rel in data['relationships']:
        if rel['subject_id'] in object_map and rel['object_id'] in object_map:
            object_map, s = MapObject(object_map,
                                      {'object_id': rel['subject_id']})
            v = rel['predicate']
            object_map, o = MapObject(object_map,
                                      {'object_id': rel['object_id']})
            rid = rel['relationship_id']
            relationships.append(Relationship(rid, s, v, o, rel['synsets']))
        else:
            # Skip this relationship if we don't have the subject and object in
            #   the object_map for this scene graph. Some data is missing in this way.
            count_skips[0] += 1
    if 'attributes' in data:
        for attr in data['attributes']:
            a = attr['attribute']
            if a['object_id'] in object_map:
                attributes.append(
                    Attribute(attr['attribute_id'], a['object_id'], a['names'],
                              a['synsets']))
            else:
                count_skips[1] += 1
    if verbose:
        print 'Skipped {} rels, {} attrs total'.format(*count_skips)
    return Graph(image, objects, relationships, attributes)
Exemplo n.º 5
0
def save_attributes(business_id, hood_jarray):
    for hood in hood_jarray:
        neighborhood = Attribute()
        neighborhood.business_id = business_id
        split_array = hood.split(": ")
        neighborhood.attribute_name = split_array[0]
        neighborhood.flag = True if split_array[-1] == "True" else False
        neighborhood.save()
Exemplo n.º 6
0
def create_attribute(name, description, user_id):
    name = name.title()
    attribute = Attribute(name=name,
                          description=description,
                          created_by=user_id)
    response = attribute_pb2.Attribute(result="failed")
    try:
        with session_scope() as session:
            session.add(attribute)
            response = attribute_pb2.Attribute(
                result="success",
                status=status.STATUS_201_CREATED,
                message="Attribute created successfully!")
            session.commit()
    except IntegrityError as e:
        with session_scope() as session:
            try:
                attribute = session.query(Attribute).filter(
                    Attribute.is_deleted == True,
                    Attribute.name.ilike(name)).first()
            except DataError:
                attribute = None
            if attribute:
                attribute.name = name
                attribute.description = description
                attribute.created_at = datetime.now()
                attribute.created_by = user_id
                attribute.updated_at = None
                attribute.updated_by = None
                attribute.is_deleted = False
                attribute.deleted_at = None
                attribute.deleted_by = None
                session.commit()
                response = attribute_pb2.Attribute(
                    result="success",
                    status=status.STATUS_201_CREATED,
                    message="Attribute created successfully!")
                return response
            else:
                session.commit()
        response.message = "Attribute already exists with same name!"
        response.status = status.STATUS_403_FORBIDDEN
        return response
    except Exception as e:
        print(e)
        response.message = "Unexpected error occurred!"
        response.status = status.STATUS_500_INTERNAL_SERVER_ERROR
        pass
    return response
Exemplo n.º 7
0
def save_attributes():
    try:
        payload = request.get_json()
    except Exception:
        return jsonify(error='Invalid JSON.')

    validation = attributes_validate_required(payload)
    if validation['errors']:
        return jsonify(error={'name': 'invalid_model',
                              'errors': validation['errors']}), 400
    attribute = Attribute(**payload)
    try:
        db.session.add(attribute)
        db.session.commit()
        return jsonify(attribute.json())
    except (IntegrityError, Exception) as e:
        traceback.print_exc()
        db.session.rollback()
Exemplo n.º 8
0
def patch_attributes():
    try:
        payload = request.get_json()
    except Exception:
        return jsonify(error='Invalid JSON.')

    validation = attributes_validate_required(payload)
    if validation['errors']:
        return jsonify(error={'name': 'invalid_model',
                              'errors': validation['errors']}), 400
    attribute = Attribute(**payload)
    try:
        id = int(payload['id_attribute'])
        del payload['id_attribute']
        db.session.query(Attribute).filter(Attribute.id_attribute == id).update(payload)
        db.session.commit()
        return jsonify(attribute.json())
    except (IntegrityError, Exception) as e:
        traceback.print_exc()
        db.session.rollback()
Exemplo n.º 9
0
def parse_graph(data, image):
    """
    Helper to parse a Graph object from API data.
    """
    objects = []
    object_map = {}
    relationships = []
    attributes = []

    # Create the Objects
    for obj in data['bounding_boxes']:
        names = []
        synsets = []
        for bbx_obj in obj['boxed_objects']:
            names.append(bbx_obj['name'])
            synsets.append(parse_synset(bbx_obj['object_canon']))
            object_ = Object(obj['id'], obj['x'], obj['y'], obj['width'],
                             obj['height'], names, synsets)
            object_map[obj['id']] = object_
        objects.append(object_)
        pass

    # Create the Relationships
    for rel in data['relationships']:
        relationships.append(
            Relationship(rel['id'], object_map[rel['subject']],
                         rel['predicate'], object_map[rel['object']],
                         parse_synset(rel['relationship_canon'])))
        pass

    # Create the Attributes
    for atr in data['attributes']:
        attributes.append(
            Attribute(atr['id'], object_map[atr['subject']], atr['attribute'],
                      parse_synset(atr['attribute_canon'])))
        pass

    return Graph(image, objects, relationships, attributes)
Exemplo n.º 10
0
    def parse_to_ABCDModel(self):
        """Very raw implementation of the parser"""

        if self.xml_str is None:
            raise TypeError('Expected \'str\' type \'None\' provided!')

        e = xml.etree.ElementTree.fromstring(self.xml_str.encode('utf-8'))

        content = e.find(c.abcd_path_content.format(self.biocase_ns_str),
                         self.ns)
        record_count = content.attrib['recordCount']
        record_dropped = content.attrib['recordDropped']
        total_searched_hits = content.attrib['totalSearchHits']

        if record_count is None:
            raise ValueError('\'recordCount\' can\'t be None.')

        if record_dropped is None:
            raise ValueError('\'recordDropped\' can\'t be None.')

        if total_searched_hits is None:
            raise ValueError('\'totalSearchHits\' Count can\'t be None.')

        try:
            record_count = int(record_count)
            record_dropped = int(record_dropped)
            total_searched_hits = int(total_searched_hits)
        except ValueError:
            raise (
                'failed to parse attributes to int. provided: '
                '{recordCount: {0}, recordDropped: {1}, totalSearchHits: {2}}'.
                format(record_count, record_dropped, total_searched_hits))

        technical_contact_name = e.find(
            c.abcd_path_technical_contact_name.format(self.biocase_ns_str,
                                                      self.abcd_ns_str),
            self.ns)
        technical_contact_name = technical_contact_name.text if technical_contact_name is not None else None
        technical_contact_email = e.find(
            c.abcd_path_technical_contact_email.format(self.biocase_ns_str,
                                                       self.abcd_ns_str),
            self.ns)
        technical_contact_email = technical_contact_email.text if technical_contact_email is not None else None
        organisation_name = e.find(
            c.abcd_path_org_representation_name.format(self.biocase_ns_str,
                                                       self.abcd_ns_str),
            self.ns)
        organisation_name = organisation_name.text if organisation_name is not None else None

        organisation_abbrv = e.find(
            c.abcd_path_org_representation_abbrv.format(
                self.biocase_ns_str, self.abcd_ns_str), self.ns)
        organisation_abbrv = organisation_abbrv.text if organisation_abbrv is not None else None

        organisation_address = e.find(
            c.abcd_path_org_address.format(self.biocase_ns_str,
                                           self.abcd_ns_str), self.ns)
        organisation_address = organisation_address.text if organisation_address is not None else None

        units = e.findall(
            c.abcd_path_unit.format(self.biocase_ns_str, self.abcd_ns_str),
            self.ns)

        if organisation_name is None:
            return None

        data_provider = DataProvider()
        data_provider.org_name = organisation_name
        data_provider.org_abbrv = organisation_abbrv
        data_provider.address = organisation_address
        data_provider.contact_name = technical_contact_name
        data_provider.contact_email = technical_contact_email

        abcd_model = ABCDModel()
        abcd_model.provider = data_provider
        abcd_model.record_count = record_count
        abcd_model.record_dropped = record_dropped
        abcd_model.total_searched_hits = total_searched_hits

        for child in units:
            unit = {}
            collection_item = CollectionItem()
            collection_item.ABCD_model = abcd_model

            for key, value in c.attrs.iteritems():
                name = c.attrs[key]['name']
                _relative_path = c.attrs[key][
                    'relative_path']  # this should be iterated.
                _full_path = c.attrs[key][
                    'full_path']  # this should be iterated.
                value = child.find(_relative_path.format(self.abcd_ns_str),
                                   self.ns)
                value = value.text if value is not None else None

                unit[name] = value

                attribute = Attribute()
                attribute.id = key  # this will be 'k' for key (in the dict)
                attribute.name = name
                attribute.relative_path = _relative_path
                attribute.full_path = _full_path
                attribute.values = [value]

                collection_item.add_attribute(attribute)

                if name == 'SourceInstitutionID' and data_provider.org_abbrv is None:
                    data_provider.org_abbrv = value

            abcd_model.add_collection_item(collection_item)

            abcd_model.flag = max([
                int(
                    collection_item.get_attribute(
                        c.attrs[c.name_unit_id]['name']).values[0])
                for collection_item in abcd_model.collection_items
            ])

        return abcd_model
Exemplo n.º 11
0
def event_details(request, event_id=None):
    # Set a detail/attribute
    if request.method == "POST":
        try:
            event = Event.objects.get(pk=event_id)
            data = json.loads(request.read())

            payload = {
                "event_id": event.id,
                "details": [],
            }

            for detail in data["details"]:
                details_type = detail["details_type"]
                key = detail["name"]
                value = detail["value"]
                mode = detail["mode"]

                if details_type == "attribute":
                    attributes = Attribute.objects.filter(event=event, key=key)
                    if not attributes or mode == "append":
                        attribute = Attribute(event=event, key=key, value=value)
                        attribute.save()
                    elif mode == "set":
                        for attribute in attributes[1:]:
                            attribute.delete()
                        attribute = attributes[0]
                        attribute.value = value
                        attribute.save()

                elif details_type == "stream":
                    stream = Stream.objects.filter(event=event, name=key)
                    if stream:
                        stream = stream.get()
                        stream_text = value
                        if mode == "append":
                            stream_text = stream.text + stream_text
                        stream.text = stream_text
                    else:
                        stream = Stream(event=event, name=key, text=value)
                    stream.save()

                payload["details"].append(detail)

            publish("event_details", "update", payload, event_id=event.id)
            return json_response({"msg": ""})

        except IntegrityError as err:
            return json_response({"msg": str(err)}, "error", 400)
        except DatabaseError as err:
            return json_response({"msg": str(err)}, "error", 500)


    # Get Details for an event
    if request.method == "GET":
        try:
            data = {}
            event = Event.objects.get(pk=event_id)
            data["event_id"] = event.id
            data["attributes"] = event.attributes()
            data["streams"] = [stream.to_dict() for stream in event.streams()]
            return json_response(data)
        except Event.DoesNotExist as err:
            return json_response({"msg": str(err)}, "error", 404)