def append_to_stream( self, aggregate_id: uuid.UUID, expect_version: Optional[int], events: List[Event], ) -> None: """ Add new entity into an event stream Update aggregate first and append event second. Using optimistic locking. """ if expect_version: # update existing aggregate aggregate = AggregateModel.select().where( AggregateModel.uuid == aggregate_id, AggregateModel.version == expect_version) aggregate.version = expect_version + 1 aggregate.save() else: # insert new aggregate aggregate = AggregateModel(uuid=aggregate_id, version=1) aggregate.save(force_insert=True) # Update event store # TODO: insert many for event in events: # HACK: prototype only EventModel(aggregate_id=aggregate_id, name=event.__class__.__name__, data=event.__dict__).save(force_insert=True)
def api_event_mine(_uid = None): doc = EventModel.objects(userID=_uid) for d in doc: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) return render_template('event_list.html',events=doc,paras={"title":"My Events",'action':'mine'})
def api_event_map(): doc = EventModel.objects(status='new') return render_template('map_view.html', events=doc, paras={ "title": "All Open Events", 'action': 'all' })
def api_event_demo(): doc = EventModel.objects(status='new') for d in doc: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) # docs = json_util.dumps([d.to_mongo() for d in doc],default=json_util.default) # app.logger.info(docs) return render_template('event_list.html',events=doc,paras={"title":"All Open Events",'action':'all'})
def api_event_myrequest(_uid = None): match = EventMatchModel.objects().filter(reqUserId = _uid) eids = [d.eventId for d in match] matchDict = dict((key, value) for (key, value) in [(d.eventId,d.status) for d in match]) print matchDict print eids event = EventModel.objects(id__in=eids) for d in event: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) #return json_util.dumps([d.to_mongo() for d in doc],default=json_util.default) return render_template('event_list.html',events=event,matchDict=matchDict,paras={"title":"My Requests",'action':'myrequest'})
def api_event_demo(): doc = EventModel.objects(status='new').order_by('-createTime') print doc for d in doc: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) # docs = json_util.dumps([d.to_mongo() for d in doc],default=json_util.default) # app.logger.info(docs) return render_template('event_list.html', events=doc, paras={ "title": "All Open Events", 'action': 'all' })
def load_stream(self, aggregate_id: uuid.UUID) -> EventStream: """ Load and event stream of an aggregate After load all event from database (by ORM), map it into Event object """ # TODO: join into single query with database.atomic() as tx: aggregate = AggregateModel.get(AggregateModel.uuid == aggregate_id) event_models = EventModel.select().where( EventModel.uuid == aggregate_id) events = [ PeeweeEventStore._convert_to_event(event) for event in event_models ] return EventStream(events, aggregate.uuid)
def api_event_near(): """ http://mongoengine-odm.readthedocs.org/guide/querying.html#geo-queries """ print request if request.method == "POST": dist = float(request.json['dist']) _lat = float(request.json['lat']) _lng = float(request.json['lng']) doc = EventModel.objects(status='new') # for d in doc: # print request # lat = d.LatLng.get('coordinates')[0]; # lng = d.LatLng.get('coordinates')[1]; # if isWithin(_lat, _lng, lat, lng, dist) is not 1: # else: # pass nums = {} photos = {} return_doc = [ d.to_mongo() for d in doc if isWithin( d.LatLng.get('coordinates')[0], d.LatLng.get('coordinates')[1], _lat, _lng, dist) == 1 ] for d in doc: if isWithin( d.LatLng.get('coordinates')[0], d.LatLng.get('coordinates')[1], _lat, _lng, dist) == 1: num = EventMatchModel.objects(eventId=str(d.id)).count() # setattr(d, 'numOfRequests', num) nums[str(d.id)] = num #print d.numOfRequests photo = ProfileModel.objects.get(userID=d.userID) photos[str(d.id)] = photo.image #setattr(d, 'image', photo.image) print json_util.dumps(return_doc) return json_util.dumps( { 'doc': return_doc, "nums": nums, "photos": photos }, default=json_util.default)
def api_event_mine(_uid=None): doc = EventModel.objects(userID=_uid) eventDict = dict( (key, value) for (key, value) in [(d.id, d.status) for d in doc]) for d in doc: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) return render_template('event_list.html', events=doc, eventDict=eventDict, paras={ "title": "My Events", 'action': 'mine' })
def api_event_near(): """ http://mongoengine-odm.readthedocs.org/guide/querying.html#geo-queries """ print request if request.method == "POST": dist = float(request.json['dist']) _lat = float(request.json['lat']) _lng = float(request.json['lng']) doc = EventModel.objects(LatLng__geo_within_center=[[_lat, _lng], dist],status='new') for d in doc: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) print num print d.numOfRequests photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) #print len(doc) #print json_util.dumps([d.to_mongo() for d in doc],default=json_util.default) return json_util.dumps([d.to_mongo() for d in doc],default=json_util.default)
def api_event_myrequest(_uid=None): match = EventMatchModel.objects().filter(reqUserId=_uid) eids = [d.eventId for d in match] matchDict = dict((key, value) for (key, value) in [(d.eventId, d.status) for d in match]) print matchDict print eids event = EventModel.objects(id__in=eids) for d in event: num = EventMatchModel.objects(eventId=str(d.id)).count() setattr(d, 'numOfRequests', num) photo = ProfileModel.objects.get(userID=d.userID).image setattr(d, 'image', photo) #return json_util.dumps([d.to_mongo() for d in doc],default=json_util.default) return render_template('event_list.html', events=event, matchDict=matchDict, paras={ "title": "My Requests", 'action': 'myrequest' })
def api_event_near(): """ http://mongoengine-odm.readthedocs.org/guide/querying.html#geo-queries """ print request if request.method == "POST": dist = float(request.json['dist']) _lat = float(request.json['lat']) _lng = float(request.json['lng']) doc = EventModel.objects(status = 'new') # for d in doc: # print request # lat = d.LatLng.get('coordinates')[0]; # lng = d.LatLng.get('coordinates')[1]; # if isWithin(_lat, _lng, lat, lng, dist) is not 1: # else: # pass nums = {} photos = {} return_doc = [d.to_mongo() for d in doc if isWithin(d.LatLng.get('coordinates')[0], d.LatLng.get('coordinates')[1],_lat,_lng,dist) == 1] for d in doc: if isWithin(d.LatLng.get('coordinates')[0], d.LatLng.get('coordinates')[1],_lat,_lng,dist) == 1: num = EventMatchModel.objects(eventId=str(d.id)).count() # setattr(d, 'numOfRequests', num) nums[str(d.id)] = num #print d.numOfRequests photo = ProfileModel.objects.get(userID=d.userID) photos[str(d.id)] = photo.image #setattr(d, 'image', photo.image) print json_util.dumps(return_doc) return json_util.dumps({'doc':return_doc, "nums":nums, "photos":photos},default=json_util.default)
def api_event_map(): doc = EventModel.objects(status='new') return render_template('map_view.html',events=doc,paras={"title":"All Open Events",'action':'all'})
def api_event_post(): _method = request.json['_method'] if _method == 'POST': print "entering here POST" title = request.json['title'] description = request.json['description'] location = request.json['location'] #eventDate = request.json['eventDate'] startTime = request.json['startTime'] endTime = request.json['endTime'] userID = request.json['userID'] latitude = request.json['latitude'] longitude = request.json['longitude'] ZIP = request.json['ZIP'] #might be swapped latitude, longitude = longitude, latitude #model = EventModel(title=title,description=description,location=location,startTime=startTime,endTime=endTime,userID=userID,latitude=latitude,longitude=longitude,ZIP=ZIP) model = EventModel(title=title,description=description,location=location,startTime=startTime,endTime=endTime,userID=userID,LatLng={"type":"Point","coordinates":[latitude,longitude]},ZIP=ZIP) doc = model.save() print json_util.dumps(doc.to_mongo()) app.logger.info(doc) return json_util.dumps(doc.to_mongo()) elif _method == 'PUT': eventId = request.json['eventId'] doc = EventModel.objects.get(id=eventId) print "entering here PUT" title = request.json['title'] description = request.json['description'] location = request.json['location'] #eventDate = request.json['eventDate'] startTime = request.json['startTime'] endTime = request.json['endTime'] userID = request.json['userID'] latitude = request.json['latitude'] longitude = request.json['longitude'] ZIP = request.json['ZIP'] if not title is None or title == '': doc.title = title if not description is None or description == '': doc.description = description if not location is None or location == '': doc.location = location if not startTime is None or startTime == '': doc.startTime = startTime if not endTime is None or endTime == '': doc.endTime = endTime if not userID is None or userID == '': doc.userID = userID if not latitude is None or latitude == '': doc.latitude = latitude if not longitude is None or longitude == '': doc.longitude = longitude if not ZIP is None or ZIP == '': doc.ZIP = ZIP doc.save() return json_util.dumps(doc.to_mongo())
def api_event_post(): _method = request.json['_method'] if _method == 'POST': print "entering here POST" title = request.json['title'] description = request.json['description'] location = request.json['location'] #eventDate = request.json['eventDate'] startTime = request.json['startTime'] endTime = request.json['endTime'] userID = request.json['userID'] latitude = request.json['latitude'] longitude = request.json['longitude'] ZIP = request.json['ZIP'] #might be swapped latitude, longitude = longitude, latitude #model = EventModel(title=title,description=description,location=location,startTime=startTime,endTime=endTime,userID=userID,latitude=latitude,longitude=longitude,ZIP=ZIP) model = EventModel(title=title, description=description, location=location, startTime=startTime, endTime=endTime, userID=userID, LatLng={ "type": "Point", "coordinates": [latitude, longitude] }, ZIP=ZIP) doc = model.save() print json_util.dumps(doc.to_mongo()) app.logger.info(doc) return json_util.dumps(doc.to_mongo()) elif _method == 'PUT': eventId = request.json['eventId'] doc = EventModel.objects.get(id=eventId) print "entering here PUT" title = request.json['title'] description = request.json['description'] location = request.json['location'] #eventDate = request.json['eventDate'] startTime = request.json['startTime'] endTime = request.json['endTime'] userID = request.json['userID'] latitude = request.json['latitude'] longitude = request.json['longitude'] ZIP = request.json['ZIP'] if not title is None or title == '': doc.title = title if not description is None or description == '': doc.description = description if not location is None or location == '': doc.location = location if not startTime is None or startTime == '': doc.startTime = startTime if not endTime is None or endTime == '': doc.endTime = endTime if not userID is None or userID == '': doc.userID = userID if not latitude is None or latitude == '': doc.latitude = latitude if not longitude is None or longitude == '': doc.longitude = longitude if not ZIP is None or ZIP == '': doc.ZIP = ZIP doc.save() return json_util.dumps(doc.to_mongo())