Replies: 2 comments 2 replies
-
@bamarob you should never be afraid of asking questions :) Sounds like a session issue to me, can you share some code? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here's my app.py: from emmett import App
from emmett import session, now
from emmett.orm import Model, Field, belongs_to, has_many
from emmett.tools.auth import AuthUser
from emmett.tools import requires
from emmett import redirect, url
app = App(__name__)
class User(AuthUser):
# will create "users" table and groups/permissions ones
has_many('hunts', 'harvests')
class Hunt(Model):
belongs_to('user')
has_many('harvests')
date = Field.date()
start = Field.time()
end = Field.time()
text = Field.text()
buck_count = Field.int()
doe_count = Field.int()
fawn_count = Field.int()
unknown_count = Field.int()
default_values = {
'user': lambda: session.auth.user.id,
'buck_count': 0,
'doe_count': 0,
'fawn_count': 0,
'unknown_count': 0
}
validation = {
'date': {'presence': True},
'start': {'presence': True},
'end': {'presence': True},
'text': {'allow': 'blank'}
}
fields_rw = {
'user': False
}
class Harvest(Model):
belongs_to('user', 'hunt')
time = Field.time()
sex = Field.string()
weight = Field.int()
left_pts = Field.int()
right_pts = Field.int()
default_values = {
'user': lambda: session.auth.user.id,
'left_pts': 0,
'right_pts': 0
}
validation = {
'time': {'presence': True},
'sex': {'presence': True},
'weight': {'presence': True}
}
fields_rw = {
'user': False,
'hunt': False
}
app.config.auth.single_template = True
app.config.auth.registration_verification = False
app.config.auth.hmac_key = "july.10.1969"
from emmett.orm import Database
from emmett.tools import Auth
app.config_from_yaml('db.yml', 'db')
db = Database(app)
auth = Auth(app, db, user_model=User)
db.define_models(Hunt, Harvest)
@app.command('setup')
def setup():
with db.connection():
# create the user
user = User.create(
email="[email protected]",
first_name="Robert",
last_name="Aldridge",
password="Passw0rd1"
)
# create an admin group
admins = auth.create_group("admin")
# add user to admins group
auth.add_membership(admins, user.id)
db.commit()
from emmett.sessions import SessionManager
app.pipeline = [
SessionManager.cookies('GreatScott'),
db.pipe,
auth.pipe
]
@app.route("/")
async def index():
hunts = Hunt.all().select(orderby=~Hunt.date)
return dict(hunts=hunts)
from emmett import abort
@app.route("/hunt/<int:hid>")
async def one(hid):
def _validate_harvest(form):
# manually set hunt id in harvest form
form.params.hunt = hid
# get hunt and return 404 if doesn't exist
hunt = Hunt.get(hid)
if not hunt:
abort(404)
# get harvests and create a form
harvests = hunt.harvests(orderby=~Harvest.time)
form = await Harvest.form(onvalidation=_validate_harvest)
if form.accepted:
redirect(url('one', hid))
return {'hunt': hunt, 'harvests': harvests, 'form': form}
@app.route("/new")
@requires(lambda: auth.has_membership('admin'), url('index'))
async def new_hunt():
form = await Hunt.form()
if form.accepted:
redirect(url('one', form.params.id))
return {'form': form}
auth_routes = auth.module(__name__) Thanks! |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I hate being that guy... I am learning lots, but have hit a snag that I'm sure someone can get me back on track quickly. I worked through the Bloggy tutorial and got it working great. I was even able to modify it to use PostgreSQL instead of SQLite (Yeah me!). Then, I replaced the Post and Comment models with my own, did the migration, modified templates and routes accordingly. I can manually add records in Postgres and they are displayed in my "index" and "one" views. But, when I try the "new" view, the form loads. But, on Submit, nothing happens (the form just reloads). Whatever needs to be happening to commit the form's contents to the database isn't happening.
When I load the "create new" page, fill in the form, and hit submit, I'm seeing a "200 OK" response:
INFO: Started server process [4836]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:47340 - "GET / HTTP/1.1" 200 OK
INFO: 127.0.0.1:47344 - "GET /emmett/jquery.min.js HTTP/1.1" 200 OK
INFO: 127.0.0.1:47346 - "GET /emmett/helpers.js HTTP/1.1" 200 OK
INFO: 127.0.0.1:47354 - "GET /new HTTP/1.1" 200 OK
INFO: 127.0.0.1:47364 - "POST /new HTTP/1.1" 200 OK
But, the data isn't saved in the DB. Open to suggestions. Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions