Skip to content

Commit

Permalink
all tests for both feats are passing
Browse files Browse the repository at this point in the history
  • Loading branch information
JeyHightower committed Feb 25, 2025
1 parent b12d9f5 commit 661a6a3
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 118 deletions.
Binary file modified backend/app/api/routes/__pycache__/universe.cpython-39.pyc
Binary file not shown.
69 changes: 50 additions & 19 deletions backend/app/api/routes/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_universe(universe_id):
universe = Universe.get_by_id(session, universe_id)

if not universe:
raise ValidationError('Universe not found')
raise NotFoundError('Universe not found')

# Check if user has access (owner or public universe)
if not (universe.is_public or str(universe.user_id) == str(current_user_id)):
Expand All @@ -94,7 +94,7 @@ def get_universe(universe_id):

return jsonify(response_data)

except (ValidationError, AuthorizationError) as e:
except (ValidationError, AuthorizationError, NotFoundError) as e:
session.rollback()
raise
except Exception as e:
Expand All @@ -110,6 +110,15 @@ def update_universe(universe_id):
if not data:
raise ValidationError('No input data provided')

# Check for invalid fields
allowed_fields = {'name', 'description', 'is_public'}
invalid_fields = set(data.keys()) - allowed_fields
if invalid_fields:
return jsonify({
'error_code': 'ValidationError',
'message': f'Invalid fields: {", ".join(invalid_fields)}'
}), 400

with get_db() as db:
universe = Universe.get_by_id(db, universe_id)
if not universe:
Expand All @@ -121,7 +130,6 @@ def update_universe(universe_id):
raise AuthorizationError('Not authorized to modify this universe')

# Update allowed fields
allowed_fields = {'name', 'description', 'is_public'}
update_data = {k: v for k, v in data.items() if k in allowed_fields}

for key, value in update_data.items():
Expand Down Expand Up @@ -187,27 +195,50 @@ def update_physics(universe_id):

data = request.get_json()
if not data or 'physics_params' not in data:
raise ValidationError('Invalid physics parameters')

# Update physics parameters
universe.update_physics(data['physics_params'])
return jsonify({
'error_code': 'ValidationError',
'message': 'Invalid physics parameters'
}), 400

# Add universe to session and commit
db.add(universe)
db.commit()

# Return complete universe data with user role
response_data = universe.to_dict()
response_data['user_role'] = 'owner'
return jsonify(response_data)

except (ValidationError, AuthorizationError) as e:
try:
# Update physics parameters
universe.update_physics(data['physics_params'])

# Add universe to session and commit
db.add(universe)
db.commit()

# Return complete universe data with user role
response_data = universe.to_dict()
response_data['user_role'] = 'owner'
return jsonify(response_data)

except ValueError as e:
db.rollback()
return jsonify({
'error_code': 'ValidationError',
'message': str(e)
}), 400

except ValidationError as e:
db.rollback()
raise
return jsonify({
'error_code': 'ValidationError',
'message': str(e)
}), 400
except AuthorizationError as e:
db.rollback()
return jsonify({
'error_code': 'AuthorizationError',
'message': str(e)
}), 403
except Exception as e:
db.rollback()
logger.error(f"Error updating physics: {str(e)}", exc_info=True)
raise ValidationError(f"Error updating physics: {str(e)}")
return jsonify({
'error_code': 'InternalError',
'message': 'An internal error occurred'
}), 500

@universe_bp.route('/<uuid:universe_id>/harmony/', methods=['PUT'])
@jwt_required()
Expand Down
Binary file modified backend/app/core/__pycache__/middleware.cpython-39.pyc
Binary file not shown.
Binary file modified backend/app/models/universe/__pycache__/universe.cpython-39.pyc
Binary file not shown.
33 changes: 33 additions & 0 deletions backend/create_demo_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from app.db.session import get_db
from app.models.user import User

def create_demo_user():
with get_db() as db:
# Delete existing demo user if exists
demo_user = db.query(User).filter_by(email='[email protected]').first()
if demo_user:
print("Deleting existing demo user...")
db.delete(demo_user)
db.commit()
print("Deleted demo user")

# Create new demo user
demo_user = User(
email='[email protected]',
username='demo',
is_active=True
)
demo_user.set_password('demo123')
db.add(demo_user)
db.commit()
print("Created demo user")

# Verify password works
if demo_user.check_password('demo123'):
print("Password verification successful")
else:
print("Password verification failed!")
return demo_user

if __name__ == '__main__':
create_demo_user()
Loading

0 comments on commit 661a6a3

Please sign in to comment.