Skip to content

Commit

Permalink
我想要你继续帮我完善我的这个资产管理系统,之前只做了一个资产登记的功能,请你继续帮我完善其它的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
charging-kuafuai committed Apr 25, 2024
1 parent b146038 commit f7aeef4
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 59 deletions.
14 changes: 14 additions & 0 deletions app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField

class SearchForm(FlaskForm):
asset_name = StringField('Asset Name')
submit = SubmitField('Search')

class UpdateForm(FlaskForm):
asset_name = StringField('Asset Name')
submit = SubmitField('Update')

class DeleteForm(FlaskForm):
asset_name = StringField('Asset Name')
submit = SubmitField('Delete')
66 changes: 58 additions & 8 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
from app import db
from flask import Blueprint, jsonify, request
from app.models import Asset, db

class Demo(db.Model):
id = db.Column(db.Integer, primary_key=True)
info1 = db.Column(db.String(80), unique=True, nullable=False)
info2 = db.Column(db.String(120), unique=True, nullable=False)
routes = Blueprint('routes', __name__)

def __init__(self, info1, info2):
self.info1 = info1
self.info2 = info2
@routes.route('/assets', methods=['GET'])
def get_assets():
name = request.args.get('name')
type = request.args.get('type')
page = request.args.get('page', 1, type=int)
limit = request.args.get('limit', 10, type=int)

assets = Asset.query.filter_by(name=name, type=type).paginate(page, limit)

result = {
'assets': [asset.serialize() for asset in assets.items],
'total': assets.total,
'pages': assets.pages,
'has_next': assets.has_next,
'has_prev': assets.has_prev
}

return jsonify(result), 200

@routes.route('/assets/<int:asset_id>', methods=['GET'])
def get_asset(asset_id):
asset = Asset.query.get(asset_id)

if asset is None:
return jsonify({'message': 'Asset not found'}), 404

return jsonify(asset.serialize()), 200

@routes.route('/assets/<int:asset_id>', methods=['PUT'])
def update_asset(asset_id):
asset = Asset.query.get(asset_id)

if asset is None:
return jsonify({'message': 'Asset not found'}), 404

data = request.get_json()

asset.name = data.get('name', asset.name)
asset.type = data.get('type', asset.type)

db.session.commit()

return jsonify(asset.serialize()), 200

@routes.route('/assets/<int:asset_id>', methods=['DELETE'])
def delete_asset(asset_id):
asset = Asset.query.get(asset_id)

if asset is None:
return jsonify({'message': 'Asset not found'}), 404

db.session.delete(asset)
db.session.commit()

return jsonify({'message': 'Asset deleted'}), 200
70 changes: 19 additions & 51 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,23 @@
from flask import request, jsonify
from app import app, db
from app.models import Demo
from flask import Flask, request

# 添加用户
@app.route('/demos', methods=['POST'])
def add_demo():
data = request.get_json()
new_demo = Demo(info1=data['info1'], info2=data['info2'])
db.session.add(new_demo)
db.session.commit()
return jsonify({'message': 'Demo added successfully'})
app = Flask(__name__)

# 获取所有用户
@app.route('/demos', methods=['GET'])
def get_demos():
demos = Demo.query.all()
demo_list = []
for demo in demos:
demo_data = {'id': demo.id, 'info1': demo.info1, 'info2': demo.info2}
demo_list.append(demo_data)
return jsonify({'demos': demo_list})
@app.route('/assets', methods=['GET'])
def search_assets():
# handle GET request to search assets
query_params = request.args
# process query parameters and perform asset search
# return search results

# 获取单个用户
@app.route('/demos/<int:demo_id>', methods=['GET'])
def get_demo(demo_id):
demo = Demo.query.get(demo_id)
if demo:
demo_data = {'id': demo.id, 'info1': demo.info1, 'info2': demo.info2}
return jsonify(demo_data)
return jsonify({'message': 'Demo not found'}), 404
@app.route('/assets/<asset_id>', methods=['PUT'])
def update_asset(asset_id):
# handle PUT request to update asset
# retrieve asset information from request body
# update asset information in database
# return updated asset information

# 更新用户信息
@app.route('/demos/<int:demo_id>', methods=['PUT'])
def update_demo(demo_id):
demo = Demo.query.get(demo_id)
if not demo:
return jsonify({'message': 'Demo not found'}), 404

data = request.get_json()
demo.info1 = data['info1']
demo.info2 = data['info2']
db.session.commit()
return jsonify({'message': 'Demo updated successfully'})

# 删除用户
@app.route('/demos/<int:demo_id>', methods=['DELETE'])
def delete_demo(demo_id):
demo = Demo.query.get(demo_id)
if not demo:
return jsonify({'message': 'Demo not found'}), 404

db.session.delete(demo)
db.session.commit()
return jsonify({'message': 'Demo deleted successfully'})
@app.route('/assets/<asset_id>', methods=['DELETE'])
def delete_asset(asset_id):
# handle DELETE request to delete asset
# delete asset from database
# return success message
1 change: 1 addition & 0 deletions app/static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Add your CSS code here */
10 changes: 10 additions & 0 deletions app/templates
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask import render_template

def search():
return render_template('search.html')

def update():
return render_template('update.html')

def delete():
return render_template('delete.html')
73 changes: 73 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from flask import Flask, jsonify, request

app = Flask(__name__)

assets = [
{
"id": 1,
"name": "Asset 1",
"description": "This is asset 1"
},
{
"id": 2,
"name": "Asset 2",
"description": "This is asset 2"
},
{
"id": 3,
"name": "Asset 3",
"description": "This is asset 3"
}
]

def get_asset(asset_id):
for asset in assets:
if asset["id"] == asset_id:
return asset
return None

def update_asset(asset_id, data):
for asset in assets:
if asset["id"] == asset_id:
asset.update(data)
return asset
return None

def delete_asset(asset_id):
for asset in assets:
if asset["id"] == asset_id:
assets.remove(asset)
return True
return False

@app.route("/assets/<int:asset_id>", methods=["GET"])
def get_asset_route(asset_id):
asset = get_asset(asset_id)
if asset:
return jsonify(asset)
else:
return jsonify({"error": "Asset not found"}), 404

@app.route("/assets/<int:asset_id>", methods=["PUT"])
def update_asset_route(asset_id):
asset = get_asset(asset_id)
if asset:
data = request.get_json()
updated_asset = update_asset(asset_id, data)
if updated_asset:
return jsonify(updated_asset)
else:
return jsonify({"error": "Failed to update asset"}), 500
else:
return jsonify({"error": "Asset not found"}), 404

@app.route("/assets/<int:asset_id>", methods=["DELETE"])
def delete_asset_route(asset_id):
asset = get_asset(asset_id)
if asset:
if delete_asset(asset_id):
return jsonify({"message": "Asset deleted successfully"})
else:
return jsonify({"error": "Failed to delete asset"}), 500
else:
return jsonify({"error": "Asset not found"}), 404

0 comments on commit f7aeef4

Please sign in to comment.