def post(self): # pylint: disable=E1101 args = request.get_json(force=True, silent=True)['params'] headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation try: event = Event_model.objects.get(id=ObjectId(args['eventID'])) except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['EVENT_ERROR']['NOT_FOUND'], 500 photo_url = args['photoURL'] found = False for url in event['photos']: if url == photo_url: found = True event.update(pull__photos=photo_url) break if not found: event.photos.append(photo_url) try: event.save() except ValidationError: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load( errorCodes)['EVENT_ERROR']['NOT_VALID'], 500 return eval(dumps(event)), 200 return photo_url, 200
def post(self): # TODO: validate parameters args = request.get_json(force=True, silent=True) token_validation = Auth.auth_token(request.headers) if (token_validation != 'True'): return token_validation if args is None: return False, 500 event_data = args try: user = User_model.objects.get(uuid=args['creator_uuid']) except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['USER_ERROR']['NOT_FOUND'], 500 new_event = Event_model(event_date=int(event_data['event_date']), title=event_data['title'], description=event_data['description'], court_id=event_data['court_id'], creator=user['id'], sport=event_data['sport_id']['$oid'], participants=[user['id']]) try: new_event.save() except ValidationError: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['EVENT_ERROR']['NOT_VALID'], 500 event = new_event.to_json() return eval(dumps(event)), 200
def get(self, id): headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation query = [] args = request.args sportID = args.get('sport-id') if sportID is not None: query.append({"$match": {"_id": ObjectId(sportID)}}) sports_data = Sport_model.objects.aggregate(*query) sports_json = eval(dumps(sports_data))[0] resource_url = sports_json['resource_url'] response = requests.get(resource_url) data = response.json() data = data['features'] for feature in data: record = feature['properties'] record['INFOESP'] = record['INFOESP'][0] record.pop('PRECIOS', None) record.pop('HORARIOS', None) record.pop('DESCRIPCION', None) record.pop('CONTACTO', None) if id is not None: if int(id) == record['ID']: return [feature], 200 return data, 200
def put(self, id): # pylint: disable=E1101 args = request.get_json(force=True, silent=True)['params'] headers = request.headers token_validation = Auth.auth_token(headers) event = None if (token_validation != 'True'): return token_validation try: event = Event_model.objects.get(id=id) except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['EVENT_ERROR']['NOT_FOUND'], 500 if args['participantUUID'] is None: try: if args['eventDate'] is not None: event.event_date = args['eventDate'] if args['title'] is not None: event.title = args['title'] if args['description'] is not None: event.description = args['description'] try: event.save() except ValidationError: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load( errorCodes)['EVENT_ERROR']['NOT_VALID'], 500 except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load( errorCodes)['EVENT_ERROR']['NOT_FOUND'], 500 if args['participantUUID'] is not None: try: participant = User_model.objects.get( uuid=args['participantUUID']) except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load( errorCodes)['USER_ERROR']['NOT_FOUND'], 500 found = False for p in event['participants']: if p.id == participant.id: found = True event.update(pull__participants=participant.id) break if not found: event.participants.append(ObjectId(participant.id)) try: event.save() except ValidationError: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load( errorCodes)['EVENT_ERROR']['NOT_VALID'], 500 return eval(dumps(event)), 200 return 200
def delete(self, id): # pylint: disable=E1101 headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation try: Comment_model.objects.get(id=ObjectId(id)).delete() except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['COMMENT_ERROR']['NOT_FOUND'], 500 return True, 200
def get(self): # pylint: disable=E1101 headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation result = [] args = request.args lat = args.get('lat') lon = args.get('lon') transports_data = Transport_model.objects for transport in transports_data: resource_url = transport['resource_url'] response = requests.get(resource_url) data = response.json() data = data['features'] if lat is not None and lon is not None: currentData = [] for feature in data: record = feature['properties'] record.pop('timestamp', None) record.pop('begin', None) record.pop('end', None) record.pop('altitudemode', None) record.pop('tessellate', None) record.pop('extrude', None) record.pop('visibility', None) record.pop('draworder', None) record.pop('icon', None) record['marker_url'] = transport.marker_url flat = float(feature['geometry']['coordinates'][0]) flon = float(feature['geometry']['coordinates'][1]) distance = Distance.calc_distance(lat, lon, flat, flon) if distance >= 0 and distance <= 1: currentData.append(feature) result.append({ "type": transport.name, "data": currentData, "marker_url": transport.marker_url }) return result, 200
def get(self): # pylint: disable=E1101 args = request.args headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation sport_id = None if args is not None: sport_id = args.get('id') try: query = [] if sport_id is not None: query.append({"$match": {"_id": ObjectId(sport_id)}}) except DoesNotExist: return False sport = eval(dumps(Sport_model.objects.aggregate(*query))) return sport, 200
def post(self): # pylint: disable=E1101 args = request.get_json(force=True, silent=True)['params'] headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation try: user = User_model.objects.get(uuid=args['userUUID']) except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['USER_ERROR']['NOT_FOUND'], 500 try: comment = Comment_model(user=user.id, event=args['eventID'], message=args['message']) comment.save() except ValidationError: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load(errorCodes)['COMMENT_ERROR']['NOT_VALID'], 500 return eval(dumps(comment)), 200
def get(self): args = request.args headers = request.headers token_validation = Auth.auth_token(headers) if (token_validation != 'True'): return token_validation event_date = court_id = event_sport = None if args is not None: event_date = args.get('date') court_id = args.get('court') event_sport = args.get('sport') query = [] events = [] if event_sport is not None: query.append({"$match": {"sport": ObjectId(event_sport)}}) if event_date is not None: query.append({"$match": {"event_date": {"$gte": int(event_date)}}}) if court_id is not None: query.append({"$match": {"court_id": int(court_id)}}) result = Event_model.objects.aggregate(*query) for res in result: event = {} event['id'] = eval(dumps(res['_id']))['$oid'] event['eventDate'] = res['event_date'] event['creationDate'] = res['creation_date'] event['title'] = res['title'] event['description'] = res['description'] event['sport'] = res['sport'] event['courtID'] = res['court_id'] event['photos'] = res['photos'] # Creator try: creator = User_model.objects.get(id=res['creator']) except DoesNotExist: with open('utils/errorCodes.json', 'r') as errorCodes: return json.load( errorCodes)['USER_ERROR']['NOT_FOUND'], 500 creator_serialized = {} creator_serialized['uuid'] = creator.uuid creator_serialized['name'] = creator.name creator_serialized['photoURL'] = creator.photo_url event['creator'] = creator_serialized # Participants participants_from_db = res['participants'] participants = [] for p in participants_from_db: user = User_model.objects.get(id=p) user_serialized = {} user_serialized['uuid'] = user.uuid user_serialized['name'] = user.name user_serialized['photoURL'] = user.photo_url participants.append(user_serialized) event['participants'] = participants # Comments comments_from_db = Comment_model.objects(event=event['id']) comments = [] for c in comments_from_db: comment = {} user_serialized = {} user_serialized['uuid'] = c.user.uuid user_serialized['name'] = c.user.name user_serialized['photoURL'] = c.user.photo_url comment['user'] = user_serialized comment['message'] = c.message comment['id'] = eval(dumps(c.id))['$oid'] comments.append(comment) event['comments'] = comments # Sport try: query = [] if res['sport'] is not None: query.append({"$match": {"_id": ObjectId(res['sport'])}}) except DoesNotExist: return False sport = eval(dumps(Sport_model.objects.aggregate(*query))) event['sport'] = sport[0] events.append(eval(dumps(event))) return events, 200