def update_data_view(view_id): user_id = g.current_user_id view = session.query(View).get(view_id) # make sure the view was found if not view: return jsonify(response="Cannot update data for view {0} from user {1} - view not found".format(view_id,user_id)),404 # make sure the view belongs to the userid if not view.user_id == int(user_id): return jsonify(response="Cannot update data view {0} it does not belong to user {1}".format(view_id,user_id)),404 view_json = request.get_json() # we are only checking the view_track display params # other updates need to happen through the other end points view_tracks = view_json.get('view_tracks') for view_track in view_tracks: view_track_obj = session.query(ViewTrack).get(view_track.get('view_track_id')) if not view_track_obj: return jsonify(response="Cannot update display parameters for view_track {0} it does not exist".format(view_track_id)),404 param_array = view_track.get('display_params') for param in ['sticky','hidden','y_max']: val = param_array.get(param) if val: setattr(view_track_obj,param,val) session.add(view_track_obj) session.commit() return jsonify(view.to_data())
def update_view(view_id): user_id = g.current_user_id json = request.get_json() view = session.query(View).get(view_id) if not view: return jsonify(response="Can't fetch view with id: {}".format(view_id)),404 if not view.user_id == int(user_id): return jsonify(response="Cant update view {0} for user {1} they do not own it".format(view.id,user.id)),404 # update the view_name if given view_name = json.get('view_name') if view_name: view.view_name = view_name # update the viewtracks if given track_ids = json.get('track_ids') if track_ids is not None: track_ids = set(track_ids) existing_ids = set(view_track.track_id for view_track in view.view_tracks) for new_track_id in track_ids.difference(existing_ids): app.logger.warning('Adding track {}'.format(new_track_id)) session.add(ViewTrack(track_id=new_track_id, view_id=view.id)) for stale_track_id in existing_ids.difference(track_ids): app.logger.warning('Removing track {}'.format(stale_track_id)) session.delete(session.query(ViewTrack).filter_by(track_id=stale_track_id,view_id=view.id).first()) session.commit() return jsonify(view.to_json())
def to_json(self): track = session.query(Track).get(self.track_id) data = [] if track.data_type == 'fasta': fasta = session.query(Fasta).get(track.data_id) data.append(fasta.header) data.append("".join(str(base) for base in fasta.base_pairs)) elif track.data_type == 'wig': wig = session.query(Wig).get(track.data_id) for wig_val in wig.values: data.append([wig_val.position, wig_val.value]) elif track.data_type == 'gtf': gtf = session.query(Gtf).get(track.data_id) for gtf_val in gtf.values: data.append(gtf_val.to_json()) elif track.data_type == 'bed': bed = session.query(Bed).get(track.data_id) for bed_value in bed.values: data.append(bed_value.to_json()) return { 'track_name' : track.track_name, 'track_id' : self.track_id, 'data_type' : track.data_type, 'data' : data, 'view_track_id' : self.id, 'display_params': {'sticky': self.sticky, 'hidden': self.hidden, 'y_max' : self.y_max } }
def to_json(self): track = session.query(Track).get(self.track_id) data = [] if track.data_type == 'fasta': fasta = session.query(Fasta).get(track.data_id) data.append(fasta.header) data.append("".join(str(base) for base in fasta.base_pairs)) elif track.data_type == 'wig': wig = session.query(Wig).get(track.data_id) for wig_val in wig.values: data.append([wig_val.position, wig_val.value]) elif track.data_type == 'gtf': gtf = session.query(Gtf).get(track.data_id) for gtf_val in gtf.values: data.append(gtf_val.to_json()) elif track.data_type == 'bed': bed = session.query(Bed).get(track.data_id) for bed_value in bed.values: data.append(bed_value.to_json()) return { 'track_name': track.track_name, 'track_id': self.track_id, 'data_type': track.data_type, 'data': data, 'view_track_id': self.id, 'display_params': { 'sticky': self.sticky, 'hidden': self.hidden, 'y_max': self.y_max } }
def new_view(): user_id = g.current_user_id json = request.get_json() track_ids = json.get('track_ids') view_name = json.get('view_name') for field in [track_ids, view_name]: if not field: return jsonify(response="Could not create view, {} is required".format(field)),404 new_view = View(view_name = view_name, user_id = user_id) session.add(new_view) session.commit() view_tracks = [] for track_id in track_ids: track = session.query(Track).get(track_id) if not track: return jsonify(response="Could not create view, {} is not a valid track id".format(track_id)),404 if not track.user_id == int(user_id): return jsonify(response="Could not create view with track {} is not a owned by user {}".format(track_id,user_id)),404 view_track = ViewTrack(track_id = track_id, view_id = new_view.id) view_tracks.append(view_track) session.add_all(view_tracks) session.commit() return jsonify(view_id=new_view.id),200
def update_track(track_id): user_id = g.current_user_id if not track_id: return jsonify(response="Can't fetch track, track_id required"), 404 track = session.query(Track).get(track_id) json = request.get_json() if not track: return jsonify( response="Can't fetch track with id: {}".format(track_id)), 404 if not track.user_id == int(user_id): return jsonify( response="Cant update track {0} for user {1} they do not own it". format(track.id, user.id)), 404 for field in ['track_name', 'data_type', 'data_id']: val = json.get(field) if val: setattr(track, field, val) session.commit() return jsonify(track.to_json())
def get_user(): user_id = g.current_user_id user = session.query(User).get(user_id) if user: return jsonify( username=user.username, user_id=user.id, email=user.email ) else: return jsonify(response="Can't fetch user with id: {}".format(user_id)),404
def get_all_tracks(): user_id = g.current_user_id # look up all the tracks tracks = session.query(Track).filter_by(user_id=user_id).all() if not tracks: return jsonify(response="user {} has no tracks".format(user_id)), 404 return jsonify(tracks=[track.to_json() for track in tracks])
def delete_user(): user_id = g.current_user_id user = session.query(User).get(user_id) if not user: return jsonify(response="Could not fetch user with id: "+user_id),404 session.delete(user) session.commit() return jsonify()
def get_all_tracks(): user_id = g.current_user_id # look up all the tracks tracks = session.query(Track).filter_by(user_id=user_id).all() if not tracks: return jsonify(response="user {} has no tracks".format(user_id)),404 return jsonify(tracks=[ track.to_json() for track in tracks])
def get_views(): user_id = g.current_user_id views = session.query(View).filter_by(user_id=user_id).all() # make sure the view was found if not views: return jsonify(response="Cannot fetch views from user {0}".format(user_id)),404 # otherwise return it return jsonify(views=[ view.to_json() for view in views])
def delete_view(view_id): user_id = g.current_user_id view = session.query(View).get(view_id) if not view: return jsonify(response="Could not fetch view with id: {}".format(view_id)),404 if not view.user_id == int(user_id): return jsonify(response="Could not delete view {0} it is not owned by user {1}".format(view.id,user_id)),404 session.delete(view) session.commit() return jsonify()
def delete_track(track_id): user_id = g.current_user_id track = session.query(Track).get(track_id) if not track: return jsonify(response="Could not fetch track with id: {}".format(track_id)),404 if not track.user_id == int(user_id): return jsonify(response="Could not delete track {0} it is not owned by user {1}".format(track.id,user_id)),404 session.delete(track) session.commit() return jsonify()
def get_data_view(view_id): user_id = g.current_user_id view = session.query(View).get(view_id) # make sure the view was found if not view: return jsonify(response="Cannot fetch view {0} from user {1}".format(view_id,user_id)),404 # make sure the view belongs to the userid if not view.user_id == int(user_id): return jsonify(response="Cannot return view {0} it does not belong to user {1}".format(view_id,user_id)),404 # otherwise return it return jsonify(view.to_data())
def get_one_track(track_id): user_id = g.current_user_id # get the track track = session.query(Track).get(track_id) # make sure the track was found if not track: return jsonify(response="Cannot fetch track {0} from user {1}".format(track_id,user_id)),404 # make sure the track belongs to the userid if not track.user_id == int(user_id): return jsonify(response="Cannot return track {0} it does not belong to user {1}".format(track_id,user_id)),404 # otherwise return it return jsonify(track.to_json())
def delete_track(track_id): user_id = g.current_user_id track = session.query(Track).get(track_id) if not track: return jsonify( response="Could not fetch track with id: {}".format(track_id)), 404 if not track.user_id == int(user_id): return jsonify( response="Could not delete track {0} it is not owned by user {1}". format(track.id, user_id)), 404 session.delete(track) session.commit() return jsonify()
def get_one_track(track_id): user_id = g.current_user_id # get the track track = session.query(Track).get(track_id) # make sure the track was found if not track: return jsonify(response="Cannot fetch track {0} from user {1}".format( track_id, user_id)), 404 # make sure the track belongs to the userid if not track.user_id == int(user_id): return jsonify( response="Cannot return track {0} it does not belong to user {1}". format(track_id, user_id)), 404 # otherwise return it return jsonify(track.to_json())
def update_user(): user_id = g.current_user_id json = request.get_json() user = session.query(User).get(user_id) if not user: return jsonify(response="Can't fetch user with id: "+user_id),404 if json.get('username'): user.username = json.get('username') if json.get('email'): user.email = json.get('email') if json.get('password'): user.password = user.hash_password(json.get('password')) session.commit() return jsonify(username=user.username, user_id=user.id, email=user.email)
def update_track(track_id): user_id = g.current_user_id if not track_id: return jsonify(response="Can't fetch track, track_id required"),404 track = session.query(Track).get(track_id) json = request.get_json() if not track: return jsonify(response="Can't fetch track with id: {}".format(track_id)),404 if not track.user_id == int(user_id): return jsonify(response="Cant update track {0} for user {1} they do not own it".format(track.id,user.id)),404 for field in ['track_name','data_type','data_id']: val = json.get(field) if val: setattr(track,field,val) session.commit() return jsonify(track.to_json())