def post(self): lat_lng = json.loads(self.request.body) pin_lat = round(lat_lng["lat"], 4) pin_long = round(lat_lng["lng"], 4) pin_record = Pin(latitude=pin_lat, longitude=pin_long) pin_record.user_id = users.get_current_user().user_id() pin_record.put()
def UpdatePin(id=0): pin = Pin.query.get_or_404(id) user = get_jwt_identity() if (User.Role(user['role']) not in [User.Role.ADMIN, User.Role.VERIFIED]) and (user['id'] != pin.edits[0].user_id): return Response('only the creator or an admin can edit a pin', status=403) try: json = request.json details = '' PROPERTY_LIST = ['position_x', 'position_y', 'symbol', 'resource', 'rank', 'name', 'amount', 'respawn', 'notes', 'x_cord', 'y_cord'] for prop in PROPERTY_LIST: old_value = getattr(pin, prop) enum = False if hasattr(old_value, 'value'): old_value = old_value.value enum = True if old_value != json[prop]: details += f'{prop} changed from {old_value} to {json[prop]}\n' if enum is True: if json[prop] in [item.value for item in Pin.Symbol]: setattr(pin, prop, Pin.Symbol(json[prop])) elif json[prop] in [item.value for item in Pin.Resource]: setattr(pin, prop, Pin.Resource(json[prop])) else: setattr(pin, prop, json[prop]) db.session.commit() newEdit = Edit(details, pin.id, get_jwt_identity()['id']) db.session.add(newEdit) db.session.commit() return jsonify(pin.to_dict()) except IntegrityError as error: return Response(error.args[0], status=400)
def CreatePin(): json = request.json try: newPin = Pin( json['position_x'], json['position_y'], Pin.Symbol(json['symbol']), Pin.Resource(json['resource']), json['world_id'] or None, json['rank'], json['name'], json['amount'], json['respawn'], json['notes'], json['x_cord'], json['y_cord'] ) db.session.add(newPin) db.session.commit() newEdit = Edit(json['notes'], newPin.id, get_jwt_identity()['id']) db.session.add(newEdit) db.session.commit() data = jsonify(newPin.to_dict()) data.status_code = 201 return data except IntegrityError as error: return Response(error.args[0], status=400)
def get(self): template = JINJA_ENVIRONMENT.get_template('templates/maps.html') current_user = users.get_current_user() user_pins = Pin.query().filter( Pin.user_id == current_user.user_id()).fetch() dict_for_template = {'user_pins': user_pins} self.response.write(template.render(dict_for_template))
def setUp(self): app = create_app(test_config) with app.app_context(): db.drop_all() db.create_all() self.app = app.test_client() self.app_context = app.app_context() self.app_context.push() guild = Guild('Flames of Exile') db.session.add(guild) db.session.commit() foe_guild = db.session.query(Guild).filter_by( name='Flames of Exile').first() admin = User('DiscordBot', sha256_crypt.encrypt('admin'), foe_guild.id, User.Role.ADMIN) admin.discord_confirmed = True db.session.add(admin) db.session.commit() campaign = Campaign('campaign_name', '/mediafiles/campaigns/campaign.png', True) db.session.add(campaign) db.session.commit() world = World('world_name', '/mediafiles/campaigns/campaign_name/world.png', 1, 1, 1, 1) db.session.add(world) db.session.commit() pin = Pin(1, 1, Pin.Symbol.ANIMAL, Pin.Resource.WOLF, 1, 1, '', 1, 1, 'notes', 1, 1) db.session.add(pin) db.session.commit() edit = Edit("", 1, 1) db.session.add(edit) db.session.commit() event = Event('event', 'game', datetime.datetime.now().isoformat(), 'note') db.session.add(event) db.session.commit() self.DEFAULT_GUILD = guild self.DEFAULT_USER = admin self.DEFAULT_CAMPAIGN = campaign self.DEFAULT_WORLD = world self.DEFAULT_PIN = pin self.DEFAULT_TOKEN = f'Bearer {self.login("DiscordBot", "admin").get_json()["token"]}' self.DEFAULT_EVENT = event self.maxDiff = None self.assertEqual(app.debug, False)
def fetch_pin(self,pin_id): """ Returns a pin corresponding to a particular pin_id """ try: pin=Pin.load(pindb,pin_id) return pin except: return "error:pin does not exist"
def insert_pin(self, pName, pDesc, bName, userid): maxpval = 0 for index in database: if(database[index].get('userid')== userid): print database[index].get('pin_id') maxpval = database[index].get('pin_id') print maxpval flag = True break else: flag = False for index in database: if(database[index].get('pin_id')>0): maxpval = database[index].get('pin_id') print maxpval else: print "Not Found" for iad in database: if(database[iad].get('boardName') == bName): print iad flagcheck = True break else: flagcheck = False if(maxpval == None): maxpval = 0 if(flag and flagcheck): pin = Pin(pin_name=pName, pin_url=pDesc) pin['pin_id'] = maxpval+1 pin['puserid'] = userid pin['board_name'] = bName pin.store(database) URL = 'url' boards = str('/boards/') users = str('users/') pins = str('/pins/') return {"links":[{URL:users+str(userid)+boards+bName+pins+str(pin['pin_id']),"method":"PUT"},{URL:users+str(userid)+boards+bName+pins+str(pin['pin_id']),"method":"GET"},{URL:users+str(userid)+boards+bName+pins+str(pin['pin_id']),"method":"DELETE"}]} else: return '{"links":[]}'
def post(self): lat_lng = json.loads(self.request.body) current_user = users.get_current_user() pin_lat = round(lat_lng["lat"], 4) pin_long = round(lat_lng["lng"], 4) deleted_pin = Pin.query().filter( Pin.user_id == current_user.user_id()).filter( Pin.latitude == pin_lat).filter( Pin.longitude == pin_long).get() if deleted_pin is not None: deleted_pin.key.delete()
def get_pin(self,pin_id): """ Returns Pin item with information of user id,pin id, pin name, pin url and comments """ try: pin=Pin.load(pindb,pin_id) pin_item={} pin_item['pin_id']=pin_id pin_item['pin_name']=pin.pin_name pin_item['pin_url']=request.urlparts.scheme+"://"+request.urlparts.netloc+"/"+pin.pin_url comments=[] for comments_item in pin.comments: comment={} comment['user_id']=comments_item.user_id comment['comment']=comments_item.comment comments.append(comment) pin_item['comments']=comments return pin_item except: return False
def get_board(self,board_id): """ Returns All the pins in a particular board with board_id """ try: board=Board.load(boarddb,board_id) result={} result['board_id']=board_id result['board_name']=board.board_name pins=[] for pin_item in board.pins: pin2=Pin.load(pindb,pin_item.pin_id) pin={} pin['pin_id']=pin_item.pin_id pin['pin_name']=pin2.pin_name pin['pin_url']=request.urlparts.scheme+"://"+request.urlparts.netloc+"/"+pin2.pin_url pins.append(pin) result['pins']=pins return result except: return False
def get_pins(): filter = request.params pins = Pin.get_many(filter) return json({'pins': [pin.dict() for pin in pins]})
def get_pin(_id): pin = Pin.get(_id) return json(pin)
def delete_pin(_id): Pin.delete({'_id': _id}) return json({})
def update_pin(_id): Pin.update({'_id': _id}, request.json) return json({})
def create_pin(): pin = Pin.create(request.json) return json({'_id': pin._id})