Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csentry
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Harrisson
csentry
Commits
b4866aad
Commit
b4866aad
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Implement UI to register items
parent
83352512
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
app/main/forms.py
+23
-1
23 additions, 1 deletion
app/main/forms.py
app/main/views.py
+21
-2
21 additions, 2 deletions
app/main/views.py
app/templates/create_item.html
+15
-4
15 additions, 4 deletions
app/templates/create_item.html
app/utils.py
+9
-0
9 additions, 0 deletions
app/utils.py
with
68 additions
and
7 deletions
app/main/forms.py
+
23
−
1
View file @
b4866aad
...
@@ -10,7 +10,7 @@ This module defines the main forms.
...
@@ -10,7 +10,7 @@ This module defines the main forms.
"""
"""
from
flask_wtf
import
FlaskForm
from
flask_wtf
import
FlaskForm
from
wtforms
import
SelectField
,
StringField
,
validators
from
wtforms
import
SelectField
,
StringField
,
SelectMultipleField
,
validators
from
..
import
utils
,
models
from
..
import
utils
,
models
...
@@ -37,6 +37,28 @@ class QRCodeForm(FlaskForm):
...
@@ -37,6 +37,28 @@ class QRCodeForm(FlaskForm):
)
)
class
ItemForm
(
FlaskForm
):
ics_id
=
StringField
(
'
ICS id
'
,
validators
=
[
validators
.
InputRequired
(),
validators
.
Regexp
(
models
.
ICS_ID_RE
)])
serial_number
=
StringField
(
'
Serial number
'
,
validators
=
[
validators
.
InputRequired
()])
manufacturer_id
=
SelectField
(
'
Manufacturer
'
)
model_id
=
SelectField
(
'
Model
'
)
location_id
=
SelectField
(
'
Location
'
)
status_id
=
SelectField
(
'
Status
'
)
parent_id
=
SelectField
(
'
Parent
'
)
# macs = SelectMultipleField('Macs')
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
manufacturer_id
.
choices
=
utils
.
get_model_choices
(
models
.
Manufacturer
,
allow_blank
=
True
)
self
.
model_id
.
choices
=
utils
.
get_model_choices
(
models
.
Model
,
allow_blank
=
True
)
self
.
location_id
.
choices
=
utils
.
get_model_choices
(
models
.
Location
,
allow_blank
=
True
)
self
.
status_id
.
choices
=
utils
.
get_model_choices
(
models
.
Status
,
allow_blank
=
True
)
self
.
parent_id
.
choices
=
utils
.
get_model_choices
(
models
.
Item
,
allow_blank
=
True
,
attr
=
'
ics_id
'
)
class
HostForm
(
FlaskForm
):
class
HostForm
(
FlaskForm
):
network_id
=
SelectField
(
'
Network
'
)
network_id
=
SelectField
(
'
Network
'
)
# The list of IPs is dynamically created on the browser side
# The list of IPs is dynamically created on the browser side
...
...
This diff is collapsed.
Click to expand it.
app/main/views.py
+
21
−
2
View file @
b4866aad
...
@@ -13,7 +13,7 @@ import sqlalchemy as sa
...
@@ -13,7 +13,7 @@ import sqlalchemy as sa
from
flask
import
(
Blueprint
,
render_template
,
jsonify
,
from
flask
import
(
Blueprint
,
render_template
,
jsonify
,
redirect
,
url_for
,
request
,
flash
,
current_app
)
redirect
,
url_for
,
request
,
flash
,
current_app
)
from
flask_login
import
login_required
from
flask_login
import
login_required
from
.forms
import
QRCodeForm
,
HostForm
from
.forms
import
QRCodeForm
,
HostForm
,
ItemForm
from
..extensions
import
db
from
..extensions
import
db
from
..decorators
import
login_groups_accepted
from
..decorators
import
login_groups_accepted
from
..
import
utils
,
models
from
..
import
utils
,
models
...
@@ -72,7 +72,26 @@ def list_items():
...
@@ -72,7 +72,26 @@ def list_items():
@bp.route
(
'
/items/create
'
,
methods
=
(
'
GET
'
,
'
POST
'
))
@bp.route
(
'
/items/create
'
,
methods
=
(
'
GET
'
,
'
POST
'
))
@login_groups_accepted
(
'
admin
'
,
'
create
'
)
@login_groups_accepted
(
'
admin
'
,
'
create
'
)
def
create_item
():
def
create_item
():
form
=
None
form
=
ItemForm
()
if
form
.
validate_on_submit
():
item
=
models
.
Item
(
ics_id
=
form
.
ics_id
.
data
,
serial_number
=
form
.
serial_number
.
data
,
manufacturer_id
=
form
.
manufacturer_id
.
data
or
None
,
model_id
=
form
.
model_id
.
data
or
None
,
location_id
=
form
.
location_id
.
data
or
None
,
status_id
=
form
.
status_id
.
data
or
None
,
parent_id
=
form
.
parent_id
.
data
or
None
)
current_app
.
logger
.
debug
(
f
'
Trying to create:
{
item
!r}
'
)
db
.
session
.
add
(
item
)
try
:
db
.
session
.
commit
()
except
sa
.
exc
.
IntegrityError
as
e
:
db
.
session
.
rollback
()
current_app
.
logger
.
warning
(
f
'
{
e
}
'
)
flash
(
f
'
{
e
}
'
,
'
error
'
)
else
:
flash
(
f
'
Item
{
item
}
created!
'
,
'
success
'
)
return
redirect
(
url_for
(
'
main.create_item
'
))
return
render_template
(
'
create_item.html
'
,
form
=
form
)
return
render_template
(
'
create_item.html
'
,
form
=
form
)
...
...
This diff is collapsed.
Click to expand it.
app/templates/create_item.html
+
15
−
4
View file @
b4866aad
{%- extends "base.html" %}
{%- extends "base.html" %}
{% from "_helpers.html" import render_field %}
{% block title %}Items - CSEntry{% endblock %}
{% block title %}Items - CSEntry{% endblock %}
...
@@ -14,11 +15,21 @@
...
@@ -14,11 +15,21 @@
<br>
<br>
<div
class=
"card"
>
<form
id=
"itemForm"
method=
"POST"
>
<h4
class=
"card-header"
>
Register new item
</h4>
{{ form.hidden_tag() }}
<div
class=
"card-body"
>
{{ render_field(form.ics_id) }}
{{ render_field(form.serial_number) }}
{{ render_field(form.manufacturer_id) }}
{{ render_field(form.model_id) }}
{{ render_field(form.location_id) }}
{{ render_field(form.status_id) }}
{{ render_field(form.parent_id) }}
<div
class=
"form-group row"
>
<div
class=
"col-sm-10"
>
<button
type=
"submit"
class=
"btn btn-primary"
>
Submit
</button>
</div>
</div>
</div>
</
div
>
</
form
>
{%- endblock %}
{%- endblock %}
{% block csentry_scripts %}
{% block csentry_scripts %}
...
...
This diff is collapsed.
Click to expand it.
app/utils.py
+
9
−
0
View file @
b4866aad
...
@@ -107,6 +107,15 @@ def get_choices(iterable, allow_blank=False, allow_null=False):
...
@@ -107,6 +107,15 @@ def get_choices(iterable, allow_blank=False, allow_null=False):
return
choices
return
choices
def
get_model_choices
(
model
,
allow_blank
=
False
,
attr
=
'
name
'
):
"""
Return a list of (value, label)
"""
choices
=
[]
if
allow_blank
:
choices
=
[(
''
,
''
)]
choices
.
extend
([(
str
(
instance
.
id
),
getattr
(
instance
,
attr
))
for
instance
in
model
.
query
.
all
()])
return
choices
def
get_query
(
query
,
args
):
def
get_query
(
query
,
args
):
"""
Retrieve the query from the arguments
"""
Retrieve the query from the arguments
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment