Skip to content

Commit 23b11a0

Browse files
Arfeyjettify
authored andcommitted
Feat started to implement edit show views (#364)
* feat: added can edit/delete/create and per_page property + prepare view/edit page (cherry picked from commit b9a1552) * feat: added default value for ModelAdmin.fields, now it's primary keys (cherry picked from commit 830b1a6) * feat: fixed show page and added simple docs for `ReadMe.md` (cherry picked from commit 9ab2015) * fix: fixed node version for `CI` (cherry picked from commit 50a3bb1)
1 parent 1f13da0 commit 23b11a0

19 files changed

Lines changed: 11936 additions & 73 deletions

File tree

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"modules": true
1111
}
1212
},
13+
"parser": "babel-eslint",
1314
"extends": [
1415
"eslint:recommended",
1516
"plugin:react/recommended"
@@ -30,6 +31,7 @@
3031
"error",
3132
"single"
3233
],
34+
"react/display-name": 0,
3335
"semi": [
3436
"error",
3537
"always"

aiohttp_admin/backends/sa.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ def table(self):
4747
@staticmethod
4848
def get_type_of_fields(fields, table):
4949
"""
50-
Return data types of `fields` that are in `table`.
50+
Return data types of `fields` that are in `table`. If a given
51+
parameter is empty return primary key.
5152
5253
:param fields: list - list of fields that need to be returned
5354
:param table: sa.Table - the current table
5455
:return: list - list of the tuples `(field_name, fields_type)`
5556
"""
5657

58+
if not fields:
59+
fields = table.primary_key
60+
5761
actual_fields = [
5862
field for field in table.c.items() if field[0] in fields
5963
]

aiohttp_admin/contrib/models.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ class ModelAdmin:
66
77
88
class Users(models.ModelAdmin):
9-
fields = ('id', 'username', )
109
1110
class Meta:
1211
resource_type = PGResource
1312
table = users
1413
1514
"""
15+
can_edit = True
16+
can_create = True
17+
can_delete = True
18+
per_page = 10
19+
fields = None
1620

1721
def __init__(self):
1822
self.name = self.__class__.__name__.lower()
@@ -22,6 +26,12 @@ def to_dict(self):
2226
Return dict with the all base information about the instance.
2327
"""
2428

25-
data = {"name": self.name}
29+
data = {
30+
"name": self.name,
31+
"canEdit": self.can_edit,
32+
"canCreate": self.can_create,
33+
"canDelete": self.can_delete,
34+
"perPage": self.per_page,
35+
}
2636

2737
return data

aiohttp_admin/static/ng-admin/ng-admin.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aiohttp_admin/static/react-admin/dist/bundle.js

Lines changed: 11556 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,63 @@
11
import React, { Component } from 'react';
2-
import { Admin, Resource } from 'admin-on-rest';
2+
import { Admin, Resource, Delete } from 'admin-on-rest';
33
import Icon from 'material-ui/svg-icons/action/bookmark-border';
44

55
import { BaseList } from '../List/List';
6+
import { BaseEdit } from '../Edit/Edit';
7+
import { BaseCreate } from '../Create/Create';
8+
import { BaseShow } from '../Show/Show';
69
import authClient from '../../libs/authClient';
710
import { restClient } from '../../libs/restClient';
11+
import Layout from '../Layout/Layout';
812

913
// styles
1014
import './Admin.sass';
1115

1216
const { state } = window.appData;
1317

1418

15-
function getResourcesByState(state) {
19+
const getAccessMethods = (data) => {
20+
const { canEdit, canCreate, canDelete } = data;
21+
let methods = {
22+
list: newProps => <BaseList {...newProps} data={data} />,
23+
show: newProps => <BaseShow {...newProps} data={data} />,
24+
};
25+
26+
if (canEdit) {
27+
methods = {
28+
...methods,
29+
edit: newProps => <BaseEdit {...newProps} data={data} />
30+
};
31+
}
32+
33+
if (canCreate) {
34+
methods = {
35+
...methods,
36+
create: newProps => <BaseCreate {...newProps} data={data} />
37+
};
38+
}
39+
40+
if (canDelete) {
41+
methods = {
42+
...methods,
43+
remove: Delete
44+
};
45+
}
46+
47+
return methods;
48+
};
49+
50+
51+
function generateResourcesByState(state) {
1652
if (state.endpoints) {
1753
return (
1854
state.endpoints.map((data, index) => (
1955
<Resource
2056
key={index}
2157
name={data.name}
2258
icon={Icon}
23-
list={(newProps) => <BaseList {...newProps} data={data} />}
2459
sort={{ field: 'id', order: 'DESC' }}
60+
{...getAccessMethods(data)}
2561
/>
2662
))
2763
);
@@ -35,14 +71,15 @@ export class AioHttpAdmin extends Component {
3571

3672
render() {
3773
return (
38-
<Admin
39-
title={state.title}
40-
locale="en"
41-
authClient={authClient}
42-
restClient={restClient}
43-
>
44-
{getResourcesByState(state)}
45-
</Admin>
74+
<Admin
75+
title={state.title}
76+
locale="en"
77+
authClient={authClient}
78+
restClient={restClient}
79+
appLayout={Layout}
80+
>
81+
{generateResourcesByState(state)}
82+
</Admin>
4683
);
4784
}
4885
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { Link } from 'react-router-dom';
4+
import IconButton from 'material-ui/IconButton';
5+
import ContentCreate from 'material-ui/svg-icons/content/create';
6+
7+
8+
export const EditButton = ({ basePath = '', record = {} }) => (
9+
<IconButton
10+
containerElement={<Link to={`${basePath}/${record.id}`} />}
11+
style={{ overflow: 'inherit' }}
12+
>
13+
<ContentCreate />
14+
</IconButton>
15+
);
16+
17+
18+
EditButton.propTypes = {
19+
basePath: PropTypes.string,
20+
record: PropTypes.object,
21+
};
22+
23+
24+
EditButton.defaultProps = {
25+
style: { padding: '0 12px' },
26+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { Link } from 'react-router-dom';
4+
import IconButton from 'material-ui/IconButton';
5+
import ContentShow from 'material-ui/svg-icons/image/remove-red-eye';
6+
7+
8+
export const ShowButton = ({ basePath = '', record = {} }) => (
9+
<IconButton
10+
containerElement={<Link to={`${basePath}/${record.id}/show`} />}
11+
style={{ overflow: 'inherit' }}
12+
>
13+
<ContentShow />
14+
</IconButton>
15+
);
16+
17+
18+
ShowButton.propTypes = {
19+
basePath: PropTypes.string,
20+
record: PropTypes.object,
21+
};
22+
23+
24+
ShowButton.defaultProps = {
25+
style: { padding: '0 12px' },
26+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import {
5+
TextInput,
6+
Create,
7+
SimpleForm,
8+
LongTextInput,
9+
} from 'admin-on-rest';
10+
11+
12+
export const BaseCreate = (props) => (
13+
<Create {...props}>
14+
<SimpleForm>
15+
<TextInput source="body" />
16+
</SimpleForm>
17+
</Create>
18+
);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import {
5+
Datagrid,
6+
List,
7+
TextField,
8+
DateField,
9+
NumberField,
10+
BooleanField,
11+
FunctionField,
12+
TabbedForm,
13+
FormTab,
14+
TextInput,
15+
DateInput,
16+
ReferenceManyField,
17+
NbItemsField,
18+
EditButton,
19+
ProductReferenceField,
20+
StarRatingField,
21+
SegmentsInput,
22+
NullableBooleanInput,
23+
Edit,
24+
SimpleForm
25+
} from 'admin-on-rest';
26+
27+
28+
export const BaseEdit = (props) => (
29+
<Edit {...props}>
30+
<SimpleForm>
31+
<TextInput source="id" style={{ display: 'block' }} />
32+
</ SimpleForm>
33+
</Edit>
34+
);

0 commit comments

Comments
 (0)