Electronic stock management. -- not updated anymore
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

forms.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from flask.ext.wtf import Form
  2. from flask.ext.wtf.html5 import NumberInput
  3. from wtforms import StringField, SubmitField, BooleanField, IntegerField
  4. from flask_wtf.file import FileField
  5. from wtforms.validators import DataRequired, NumberRange
  6. from flask_security import RegisterForm
  7. from models import db, Part, PartMeasurementUnit, Storage, Footprint, PartAttachment, Manufacturer, Tag
  8. from wtforms_alchemy import model_form_factory
  9. from wtforms.ext.sqlalchemy.fields import QuerySelectField
  10. from wtforms.ext.sqlalchemy.fields import QuerySelectMultipleField
  11. BaseModelForm = model_form_factory(Form)
  12. class ModelForm(BaseModelForm):
  13. @classmethod
  14. def get_session(cls):
  15. return db.session
  16. class ExtendedRegisterForm(RegisterForm):
  17. name = StringField('Name', [DataRequired()])
  18. def get_part_measurement_units():
  19. return PartMeasurementUnit.query.order_by('name').all()
  20. def get_storages():
  21. return Storage.query.order_by('name').all()
  22. def get_footprints():
  23. return Footprint.query.order_by('name').all()
  24. def get_manufacturers():
  25. return Manufacturer.query.order_by('name').all()
  26. def default_pmu():
  27. return PartMeasurementUnit.query.filter(PartMeasurementUnit.name == 'Pieces').first()
  28. def get_tags():
  29. return Tag.query.all()
  30. class PartForm(ModelForm):
  31. class Meta:
  32. model = Part
  33. name = StringField('Name', [DataRequired()])
  34. needs_review = BooleanField('Needs review')
  35. can_be_sold = BooleanField('Can be sold')
  36. private = BooleanField('Private')
  37. stock_level = IntegerField('Stock level', widget=NumberInput(), validators=[NumberRange(min=0)], default=1)
  38. min_stock_level = IntegerField('Minimal stock', widget=NumberInput(), validators=[NumberRange(min=0)], default=0)
  39. part_measurement_unit = QuerySelectField(default=default_pmu,
  40. query_factory=get_part_measurement_units,
  41. allow_blank=False, label='Part unit')
  42. storage = QuerySelectField(query_factory=get_storages, allow_blank=True, blank_text='None')
  43. footprint = QuerySelectField(query_factory=get_footprints, allow_blank=True, blank_text='None')
  44. tags = QuerySelectMultipleField('Tags', query_factory=get_tags)
  45. add_new = BooleanField('Save and add new part')
  46. submit = SubmitField('Save')
  47. class PartAttachmentForm(ModelForm):
  48. class Meta:
  49. model = PartAttachment
  50. att_description = StringField('Description')
  51. attachment = FileField('Document')
  52. remote_uri = StringField('Alternative remote URI')
  53. submit = SubmitField('Upload')
  54. class ManufacturerLogoForm(ModelForm):
  55. class Meta:
  56. model = PartAttachment
  57. manufacturer = QuerySelectField(query_factory=get_manufacturers, allow_blank=False)
  58. logo = FileField('Image', [DataRequired()])
  59. submit = SubmitField('Upload')