Skip to content

Commit

Permalink
Libraries: Library creation
Browse files Browse the repository at this point in the history
* NEW: library creation by admin ui
* BETTER: Refactoring library form to implement creation

Co-authored-by: Peter Weber <[email protected]>
Signed-off-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr and rerowep committed Dec 17, 2018
1 parent a98039f commit 7f273ef
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 67 deletions.
10 changes: 0 additions & 10 deletions rero_ils/modules/organisations_libraries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ def save_library(data, record_type, record_class, parent_pid=None):
library = record_class.get_record_by_pid(pid)
library.update(data, dbcommit=False, reindex=False)
else:
if 'opening_hours' not in data:
data['opening_hours'] = [
{"day": "monday", "is_open": False, "times": []},
{"day": "tuesday", "is_open": False, "times": []},
{"day": "wednesday", "is_open": False, "times": []},
{"day": "thursday", "is_open": False, "times": []},
{"day": "friday", "is_open": False, "times": []},
{"day": "saturday", "is_open": False, "times": []},
{"day": "sunday", "is_open": False, "times": []}
]
library = record_class.create(data, dbcommit=False, reindex=False)
organisation.add_library(library)
library.dbcommit(reindex=True)
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def clean_dict_keys(data):
if isinstance(data, list):
to_return = []
for item in data:
if item is False:
if item is False or item == []:
to_return.append(item)
else:
tmp = clean_dict_keys(item)
Expand All @@ -94,7 +94,7 @@ def clean_dict_keys(data):
if isinstance(data, dict):
to_return = {}
for k, v in data.items():
if v is False:
if v is False or v == []:
to_return[k] = v
else:
tmp = clean_dict_keys(v)
Expand Down
7 changes: 6 additions & 1 deletion rero_ils/templates/rero_ils/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ <h2>{{_(record_type)}}</h2>
{%- endblock search_sort_select %}
{%- block search_sort_order scoped %}
{%- if record_type | resource_can_create and current_user | can_edit %}
<a href="{{url_for('%s.create_view' % record_type)}}" type="button" class="btn btn-success">{{_('Create')}}</a>
{# TODO: Change this to local template resource to customize the link #}
{%- if record_type == 'lib' %}
<a href="{{ url_for('libraries.index', path='new') }}" type="button" class="btn btn-success">{{_('Create')}}</a>
{% else %}
<a href="{{url_for('%s.create_view' % record_type)}}" type="button" class="btn btn-success">{{_('Create')}}</a>
{% endif %}
<!-- <reroils-export-csv></reroils-export-csv> -->
{% endif %}
{%- endblock search_sort_order %}
Expand Down
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fi
set -e

pipenv check -i 36437
pipenv run flask utils check_json tests rero_ils data
pipenv run flask utils check_json tests rero_ils/modules data
pipenv run pydocstyle rero_ils tests docs
pipenv run isort -rc -c -df --skip ui
pipenv run check-manifest --ignore ".travis-*,docs/_build*,ui/admin/node_modules*,rero_ils/static/js/rero_ils/admin*"
Expand Down
9 changes: 9 additions & 0 deletions ui/admin/proxy.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,14 @@
"pathRewrite": {
"^/static": "http://localhost:5000/static"
}
},
"/admin/lib/*": {
"target": "http://localhost:5000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true,
"pathRewrite": {
"^/static": "http://localhost:5000/admin/lib"
}
}
}
2 changes: 1 addition & 1 deletion ui/admin/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function HttpLoaderFactory(http: HttpClient) {
if (environment.production) {
assets_prefix = '/static/js/rero_ils/admin/';
}
return new TranslateHttpLoader(http, assets_prefix+'assets/i18n/', '.json');
return new TranslateHttpLoader(http, assets_prefix + 'assets/i18n/', '.json');
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h3 class="modal-title">{{ index != null ? 'Edit' : 'Add' }}</h3>
<div class="col-sm-12 pl-0">
<button
type="button"
*ngIf="exceptionForm.value.is_open && times.length == 0"
*ngIf="times.length == 0"
class="btn btn-light btn-sm"(click)="addTime(0)"
><i class="fa fa-plus" aria-hidden="true"></i> period</button>
<ul class="list-unstyled mb-0">
Expand Down
4 changes: 4 additions & 0 deletions ui/admin/src/app/libraries/libraries-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { Routes, RouterModule } from '@angular/router';
import { MainComponent } from './main/main.component';

const routes: Routes = [
{
path: 'new',
component: MainComponent
},
{
path: ':pid',
component: MainComponent
Expand Down
11 changes: 6 additions & 5 deletions ui/admin/src/app/libraries/libraries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ export class LibrariesService {
));
}

save(library: Library, redirectUrl?) {
const library_id = library['id'];
save(library: Library) {
const baseUpdate = '/admin/lib/ajax/update/';
const baseRedirect = '/libraries/';
if (environment.production) {
delete library['id'];
}
this.client.put<Library>(this.librariesUrl + '/' + library_id, library, httpOptions).subscribe(
this.client.post<Library>(baseUpdate, library, httpOptions).subscribe(
lib => {
this.setCurrentLibrary(lib);
if (environment.production && redirectUrl) {
this.browser.redirect(redirectUrl);
if (environment.production) {
this.browser.redirect(baseRedirect + lib.pid);
}
}
);
Expand Down
23 changes: 3 additions & 20 deletions ui/admin/src/app/libraries/library-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class LibraryFormService {
const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
const openings = [];
for (let step = 0; step < 7; step++) {
openings.push(this.buildOpeningHours(false, days[step], [this.buildTimes()]));
openings.push(this.buildOpeningHours(false, days[step], this.fb.array([this.buildTimes()])));
}
return openings;
}
Expand Down Expand Up @@ -88,31 +88,14 @@ export class LibraryFormService {
});
}


populate(library: Library) {
this.form.patchValue({
name: library.name,
address: library.address,
email: library.email,
code: library.code
});

const openings = new FormArray([]);
library.opening_hours.forEach(opening => {
const hours = new FormArray([]);
if (opening.times.length === 0) {
opening.times.push({
start_time: '00:00',
end_time: '00:00'
});
}
opening.times.forEach(time => {
hours.push(this.buildTimes(time.start_time, time.end_time));
});

openings.push(this.buildOpeningHours(opening.is_open, opening.day, hours));
code: library.code,
opening_hours: library.opening_hours
});
this.form.setControl('opening_hours', openings);
}

setId(id) { this.form.value.id = id; }
Expand Down
61 changes: 44 additions & 17 deletions ui/admin/src/app/libraries/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,59 @@ export interface ExceptionDates {
}

export class Library {
$schema: string;
id: number;
pid: number;
name: string;
email: string;
address: string;
code: string;
$schema: string = null;
id: number = null;
pid: number = null;
name: string = null;
email: string = null;
address: string = null;
code: string = null;
opening_hours: Array<OpeningHours>;
exception_dates?: Array<ExceptionDates>;

constructor(obj?: any) {
this.update(obj);
this.opening_hours = this.populateTimes(this.opening_hours);
}

populateTimes(openinghours) {
for (const hours in openinghours) {
if (openinghours[hours].times.length === 0) {
openinghours[hours].times.push({'start_time': '00:00', 'end_time': '00:00'});
}
this.$schema = 'https://ils.test.rero.ch/schema/libraries/library-v0.0.1.json';
this.createOpeningHours();
if (obj) {
this.update(obj);
}
return openinghours;
}

update(obj) {
Object.assign(this, obj);
this.cleanOpeningHours(this.opening_hours);
}

createOpeningHours() {
const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
const openings = [];
let step = 0;
for (step = 0; step < 7; step++) {
openings.push({
'day': days[step],
'is_open': false,
'times': []
});
}
this.opening_hours = openings;
}

cleanOpeningHours(openingHours) {
openingHours.forEach(opening => {
const times = [];
for (let step = 0; step < opening.times.length; step++) {
if (
opening.times[step].start_time !== '00:00'
&& opening.times[step].end_time !== '00:00'
) {
times.push(opening.times[step]);
}
}
if (times.length === 0) {
opening.is_open = false;
}
opening.times = times;
});
}

deleteException(index) {
Expand Down
12 changes: 6 additions & 6 deletions ui/admin/src/app/libraries/library/library.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h1>Library</h1>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label required">Name</label>
<div class="col-sm-10">
<input class="form-control" id="name" formControlName="name" required>
<input class="form-control" id="name" formControlName="name" placeholder="Please insert a name" required>
<div *ngIf="name.invalid && (name.dirty || name.touched)" class="text-danger">
<div *ngIf="name.errors.required">
Name is required.
Expand All @@ -28,7 +28,7 @@ <h1>Library</h1>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input id="email" class="form-control" formControlName="email" required>
<input id="email" class="form-control" formControlName="email">
<div *ngIf="email.invalid && (email.dirty || email.touched)" class="text-danger">
<div *ngIf="email.errors.email">
Email format is not correct.
Expand All @@ -39,7 +39,7 @@ <h1>Library</h1>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label required">Code</label>
<div class="col-sm-10">
<input id="code" class="form-control" formControlName="code">
<input id="code" class="form-control" formControlName="code" placeholder="Please insert a code" required>
<div *ngIf="code.invalid && (code.dirty || code.touched)" class="text-danger">
<div *ngIf="code.errors.isTaken">
Code is already taken.
Expand All @@ -61,13 +61,13 @@ <h1>Library</h1>
<div class="col-10">
<ul class="list-unstyled mb-0">
<li formArrayName="times" *ngFor="let time of day.controls.times.controls; let t=index">
<div [formGroupName]="t">
<div [formGroupName]="t" class="mb-1">
<div class="form-row">
<div class="col-3">
<input class="form-control mr-2 mb-1" formControlName="start_time" [readonly]="!day.value.is_open">
<input class="form-control mr-2" formControlName="start_time" [readonly]="!day.value.is_open">
</div>
<div class="col-3">
<input class="form-control mb-1" formControlName="end_time" [readonly]="!day.value.is_open">
<input class="form-control" formControlName="end_time" [readonly]="!day.value.is_open">
</div>
<div class="col-6">
<button
Expand Down
2 changes: 1 addition & 1 deletion ui/admin/src/app/libraries/library/library.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class LibraryComponent {
this.libraryForm.setLibraryPid(this.library.id);
this.libraryForm.setSchema(this.library.$schema);
this.library.update(this.libraryForm.getValues());
this.librariesService.save(this.library, '/libraries/' + this.library.pid);
this.librariesService.save(this.library);
}

onCancel() {
Expand Down
8 changes: 6 additions & 2 deletions ui/admin/src/app/libraries/main/main.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ export class MainComponent implements OnInit {
public librariesService: LibrariesService
) {
this.librariesService.currentLibrary.subscribe(
library => {this.library = library; }
library => { this.library = library; }
);
}

ngOnInit() {
this.route.params.subscribe(params => {
this.pid = params.pid;
if (params.pid !== undefined) {
this.librariesService.loadLibrary(this.pid);
} else {
this.librariesService.setCurrentLibrary(new Library());
}
});
this.librariesService.loadLibrary(this.pid);
}
}

0 comments on commit 7f273ef

Please sign in to comment.