Web platform for sharing free data for ML and research

By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

Add ratings to API

roundabout,
created on Saturday, 14 September 2024, 11:54:51 (1726314891), received on Saturday, 14 September 2024, 11:54:53 (1726314893)
Author identity: vlad <vlad.muntoiu@gmail.com>

3e95f026da98d4c103776d0fe4077f5670454a3a

app.py

@@ -1214,6 +1214,13 @@ def query_pictures():

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                except APIError as e:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    return flask.jsonify({"error": e.message}), e.status_code
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            rating_count_subquery = db.select(db.func.count(PictureRating.id)).where(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                PictureRating.resource_id == PictureResource.id).scalar_subquery()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            region_count_subquery = db.select(db.func.count(PictureRegion.id)).where(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                PictureRegion.resource_id == PictureResource.id).scalar_subquery()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            rating_subquery = db.select(db.func.coalesce(db.func.avg(PictureRating.rating), 0)).where(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                PictureRating.resource_id == PictureResource.id).scalar_subquery()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                match ordering:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    case "date-desc":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        query = query.order_by(PictureResource.timestamp.desc())
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -1226,9 +1233,17 @@ def query_pictures():

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    case "random":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        query = query.order_by(db.func.random())
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    case "number-regions-desc":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(db.func.count(PictureResource.regions).desc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(region_count_subquery.desc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    case "number-regions-asc":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(db.func.count(PictureResource.regions).asc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(region_count_subquery.asc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                case "rating-desc":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(rating_subquery.desc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                case "rating-asc":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(rating_subquery.asc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                case "number-ratings-desc":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(rating_count_subquery.desc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                case "number-ratings-asc":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    query = query.order_by(rating_count_subquery.asc())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                query = query.offset(offset).limit(limit)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                resources = query.all()
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -1294,6 +1309,8 @@ def api_picture(id):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    "replaced_by": resource.replaced_by_id,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    "regions": [],
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    "download": config.ROOT_URL + flask.url_for("raw_picture", id=resource.id),
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                "rating_average": resource.average_rating,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                "rating_count": resource.rating_totals,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                }
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                for region in resource.regions:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    json_resource["regions"].append({
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -1504,3 +1521,39 @@ def api_update_picture(id):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                return flask.jsonify({"message": "Picture updated successfully"})
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        @app.route("/api/picture/<int:id>/rate", methods=["POST"])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def api_rate_picture(id):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            resource = db.session.get(PictureResource, id)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if resource is None:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(404)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            current_user = db.session.get(User, flask.session.get("username"))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if current_user is None:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(401)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            rating = int(flask.request.json.get("rating"))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not rating:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # Delete the existing rating
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                if PictureRating.query.filter_by(resource=resource, user=current_user).first():
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    db.session.delete(PictureRating.query.filter_by(resource=resource,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                                                     user=current_user).first())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                return flask.jsonify({"message": "Existing rating removed"})
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not 1 <= rating <= 5:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.flash("Invalid rating")
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                return flask.jsonify({"error": "Invalid rating"}), 400
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if PictureRating.query.filter_by(resource=resource, user=current_user).first():
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                PictureRating.query.filter_by(resource=resource, user=current_user).first().rating = rating
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            else:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # Create a new rating
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                db.session.add(PictureRating(resource, current_user, rating))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return flask.jsonify({"message": "Rating saved"})