Fixes
This commit is contained in:
parent
20becdbd27
commit
b2fba8870f
33 changed files with 3958 additions and 38 deletions
4
cromosoma/.gitignore
vendored
Normal file
4
cromosoma/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
*~
|
||||
*.pyc
|
||||
__pycache__
|
||||
|
||||
340
cromosoma/LICENSE
Normal file
340
cromosoma/LICENSE
Normal file
|
|
@ -0,0 +1,340 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
{description}
|
||||
Copyright (C) {year} {fullname}
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
{signature of Ty Coon}, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
|
||||
1
cromosoma/README.md
Normal file
1
cromosoma/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# A very simple ORM for Python 3
|
||||
0
cromosoma/__init__.py
Normal file
0
cromosoma/__init__.py
Normal file
100
cromosoma/corefields.py
Normal file
100
cromosoma/corefields.py
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
from paramecio.cromosoma.webmodel import PhangoField
|
||||
from paramecio.cromosoma import coreforms
|
||||
from paramecio.citoplasma.i18n import I18n
|
||||
|
||||
class IntegerField(PhangoField):
|
||||
|
||||
def __init__(self, name, size=11, required=False):
|
||||
super(IntegerField, self).__init__(name, size, required)
|
||||
|
||||
def check(self, value):
|
||||
|
||||
self.error=None
|
||||
self.txt_error=''
|
||||
|
||||
try:
|
||||
|
||||
value=str(int(value))
|
||||
|
||||
if value==0 and self.required==True:
|
||||
self.txt_error="The value is zero"
|
||||
self.error=True
|
||||
except:
|
||||
|
||||
self.error=True
|
||||
|
||||
return value
|
||||
|
||||
def get_type_sql(self):
|
||||
|
||||
return 'INT('+str(self.size)+') NOT NULL DEFAULT "0"'
|
||||
|
||||
class CharField(PhangoField):
|
||||
|
||||
pass
|
||||
|
||||
class ForeignKeyField(IntegerField):
|
||||
|
||||
def __init__(self, name, related_table, size=11, required=False, identifier_field='id', named_field="id", select_fields=[]):
|
||||
|
||||
self.table_id=related_table.name_field_id
|
||||
|
||||
self.table_name=related_table.name
|
||||
|
||||
self.identifier_field=identifier_field
|
||||
|
||||
self.named_field=named_field
|
||||
|
||||
self.select_fields=select_fields
|
||||
|
||||
super(ForeignKeyField, self).__init__(name, size, required)
|
||||
|
||||
self.foreignkey=True
|
||||
|
||||
class BooleanField(IntegerField):
|
||||
|
||||
def __init__(self, name, size=1):
|
||||
|
||||
required=False
|
||||
|
||||
self.yes=I18n.lang('common', 'yes', 'Yes')
|
||||
self.no=I18n.lang('common', 'no', 'No')
|
||||
|
||||
super(IntegerField, self).__init__(name, size, required)
|
||||
|
||||
def check(self, value):
|
||||
|
||||
self.error=False
|
||||
self.txt_error=''
|
||||
|
||||
try:
|
||||
|
||||
value=int(value)
|
||||
|
||||
if value<0 or value>1:
|
||||
self.txt_error="Need 0 or 1 value"
|
||||
self.error=True
|
||||
|
||||
except:
|
||||
|
||||
self.error=True
|
||||
value=0
|
||||
|
||||
value=str(value)
|
||||
|
||||
return value
|
||||
|
||||
def get_type_sql(self):
|
||||
|
||||
return 'BOOLEAN NOT NULL DEFAULT "0"'
|
||||
|
||||
def show_formatted(self, value):
|
||||
|
||||
value=int(value)
|
||||
|
||||
if value==0:
|
||||
value=self.yes_text
|
||||
else:
|
||||
value=self.no_text
|
||||
|
||||
return value
|
||||
76
cromosoma/coreforms.py
Normal file
76
cromosoma/coreforms.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
#Forms para python3
|
||||
|
||||
class BaseForm:
|
||||
|
||||
def __init__(self, name, value):
|
||||
|
||||
self.label=name
|
||||
self.name=name
|
||||
self.default_value=value
|
||||
self.css=''
|
||||
self.type='text'
|
||||
self.field=None
|
||||
self.required=False
|
||||
self.txt_error=''
|
||||
|
||||
def form(self):
|
||||
|
||||
return '<input type="'+self.type+'" class="'+self.css+'" name="'+self.name+'" id="'+self.name+'_form" value="'+self.setform(self.default_value)+'">'
|
||||
|
||||
def show_formatted(self, value):
|
||||
|
||||
return value
|
||||
|
||||
#Method for escape value for html input. DON'T CHANGE IF YOU DON'T KNOWN WHAT ARE YOU DOING
|
||||
|
||||
def setform(self, value):
|
||||
|
||||
value=str(value)
|
||||
|
||||
return value.replace('"', '"')
|
||||
|
||||
class TextForm(BaseForm):
|
||||
|
||||
def __init__(self, name, value):
|
||||
super(TextForm, self).__init__(name, value)
|
||||
|
||||
class PasswordForm(BaseForm):
|
||||
|
||||
def __init__(self, name, value):
|
||||
super(PasswordForm, self).__init__(name, value)
|
||||
self.type='password'
|
||||
|
||||
def setform(self, value):
|
||||
return ""
|
||||
|
||||
class HiddenForm(BaseForm):
|
||||
|
||||
def __init__(self, name, value):
|
||||
super(HiddenForm, self).__init__(name, value)
|
||||
self.type='hidden'
|
||||
|
||||
|
||||
class SelectForm(BaseForm):
|
||||
|
||||
def __init__(self, name, value):
|
||||
super(SelectForm, self).__init__(name, value)
|
||||
self.arr_select=OrderedDict()
|
||||
|
||||
def form(self):
|
||||
|
||||
the_form='<select name="'+self.name+'">\n'
|
||||
|
||||
arr_selected={self.default_value: 'selected'}
|
||||
|
||||
for k,v in self.arr_select.items():
|
||||
arr_selected[k]=arr_selected.get(k, '')
|
||||
the_form+="<option value=\""+str(k)+"\" "+arr_selected[k]+">"+v+"</option>"
|
||||
|
||||
the_form+='</select>\n'
|
||||
|
||||
return the_form
|
||||
|
||||
75
cromosoma/databases/mysql.py
Normal file
75
cromosoma/databases/mysql.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import pymysql.cursors
|
||||
|
||||
class SqlClass:
|
||||
|
||||
error_connection=""
|
||||
connection={}
|
||||
|
||||
def dummy_connect(self, connection, name_connection="default"):
|
||||
pass
|
||||
|
||||
def connect_to_db(self, connection, name_connection="default"):
|
||||
|
||||
try:
|
||||
|
||||
self.connection[name_connection] = pymysql.connect(connection['host'],
|
||||
user=connection['user'],
|
||||
password=connection['password'],
|
||||
db=connection['db'],
|
||||
charset='utf8mb4',
|
||||
cursorclass=pymysql.cursors.DictCursor)
|
||||
|
||||
|
||||
except:
|
||||
e = sys.exc_info()[0]
|
||||
v = sys.exc_info()[1]
|
||||
|
||||
self.error_connection="Error in connection: %s %s" % (e, v)
|
||||
|
||||
#return False
|
||||
raise NameError(self.error_connection)
|
||||
|
||||
#Make def query more simple if not debugging.
|
||||
|
||||
def query(self, sql_query, arguments=[], name_connection="default"):
|
||||
|
||||
#if fetch_type=="ASSOC":
|
||||
#fetch_type=pymysql.cursors.DictCursor
|
||||
|
||||
with self.connection[name_connection].cursor(pymysql.cursors.DictCursor) as cursor:
|
||||
|
||||
try:
|
||||
|
||||
cursor.execute(sql_query, arguments)
|
||||
self.connection[name_connection].commit()
|
||||
|
||||
return cursor
|
||||
|
||||
except:
|
||||
e = sys.exc_info()[0]
|
||||
v = sys.exc_info()[1]
|
||||
|
||||
if hasattr(cursor, '_last_executed'):
|
||||
sql_query=cursor._last_executed
|
||||
|
||||
self.error_connection="Error in query ||"+sql_query+"||: %s %s" % (e, v)
|
||||
|
||||
#return False
|
||||
raise NameError(self.error_connection)
|
||||
|
||||
#Fetcho row return dictionary if is defined in query.
|
||||
|
||||
#def fetch(self, cursor):
|
||||
|
||||
#return cursor.fetchone()
|
||||
|
||||
def close(self, name_connection="default"):
|
||||
|
||||
self.connection[name_connection].close()
|
||||
|
||||
pass
|
||||
|
||||
|
||||
338
cromosoma/dbadmin.py
Normal file
338
cromosoma/dbadmin.py
Normal file
|
|
@ -0,0 +1,338 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import argparse
|
||||
import os,traceback
|
||||
import sys, inspect
|
||||
import shutil
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
from colorama import init, Fore, Back, Style
|
||||
from importlib import import_module, reload
|
||||
from paramecio.cromosoma.webmodel import WebModel
|
||||
|
||||
#from models import books
|
||||
|
||||
def start():
|
||||
|
||||
parser = argparse.ArgumentParser(description='A tool for create tables in databases using models from Cromosoma')
|
||||
|
||||
parser.add_argument('--model', help='Model python path', required=True)
|
||||
|
||||
parser.add_argument('--config', help='The config file', required=False)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
init()
|
||||
|
||||
#Import config
|
||||
|
||||
config_file='config'
|
||||
|
||||
if args.config!=None:
|
||||
config_file=args.config
|
||||
|
||||
try:
|
||||
|
||||
config=import_module('settings.'+config_file)
|
||||
|
||||
except:
|
||||
e = sys.exc_info()[0]
|
||||
v = sys.exc_info()[1]
|
||||
|
||||
print(Fore.WHITE+Back.RED+Style.BRIGHT+"Config file not found: %s %s" % (e, v))
|
||||
|
||||
exit(1)
|
||||
|
||||
#print(WebModel.connections)
|
||||
|
||||
try:
|
||||
|
||||
model=import_module(args.model)
|
||||
|
||||
for name, obj in inspect.getmembers(sys.modules[model.__name__]):
|
||||
if inspect.isclass(obj):
|
||||
if obj.__module__==args.model and hasattr(obj, 'webmodel'):
|
||||
|
||||
WebModel.model[name.lower()]=obj()
|
||||
|
||||
|
||||
#WebModel.modelobj
|
||||
|
||||
except:
|
||||
"""
|
||||
e = sys.exc_info()[0]
|
||||
v = sys.exc_info()[1]
|
||||
|
||||
print(Fore.WHITE+Back.RED+Style.BRIGHT +"Error, file with model not found: %s %s" % (e, v))
|
||||
"""
|
||||
print("Exception in user code:")
|
||||
print("-"*60)
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
print("-"*60)
|
||||
|
||||
exit(1)
|
||||
|
||||
#load the table of databases
|
||||
|
||||
cursor=WebModel.query(WebModel, "show tables")
|
||||
|
||||
table_exists=[]
|
||||
|
||||
for row in cursor:
|
||||
table=list(row.values())[0]
|
||||
|
||||
if table in WebModel.model:
|
||||
table_exists.append(table)
|
||||
|
||||
#If don't want order
|
||||
#set([1,2,3,4]) - set([2,5])
|
||||
|
||||
tables=list(WebModel.model.keys())
|
||||
|
||||
#Array diff ordered
|
||||
|
||||
new_tables=[x for x in tables if x not in table_exists]
|
||||
|
||||
#If don't want order
|
||||
#new_tables=set(tables)-set(table_exists)
|
||||
|
||||
#Need order new_tables
|
||||
|
||||
changes=0
|
||||
|
||||
#Create new tables
|
||||
|
||||
if len(new_tables)>0:
|
||||
print(Style.BRIGHT+"Creating new tables...")
|
||||
|
||||
changes+=1
|
||||
|
||||
for table in new_tables:
|
||||
print(Style.NORMAL+"--Creating table "+table+"...")
|
||||
WebModel.query(WebModel, WebModel.model[table].create_table())
|
||||
print("--Adding indexes and constraints for the new table")
|
||||
|
||||
for k_field, index in WebModel.arr_sql_index[table].items():
|
||||
print("---Added index to "+k_field)
|
||||
WebModel.query(WebModel, index)
|
||||
|
||||
for k_set, index_set in WebModel.arr_sql_set_index[table].items():
|
||||
|
||||
if index_set!="":
|
||||
WebModel.query(WebModel, index_set)
|
||||
print("---Added constraint to "+k_set)
|
||||
|
||||
print("--Adding uniques elements for the new table")
|
||||
|
||||
#See if changes exists
|
||||
|
||||
#Check if created tables are modified.
|
||||
|
||||
try:
|
||||
|
||||
model_old=import_module('backups.'+args.model)
|
||||
|
||||
for name, obj in inspect.getmembers(sys.modules[model_old.__name__]):
|
||||
if inspect.isclass(obj):
|
||||
if obj.__module__=='backups.'+args.model and hasattr(obj, 'webmodel'):
|
||||
|
||||
WebModel.model['old_'+name.lower()]=obj()
|
||||
|
||||
print(Style.BRIGHT+"Checking old versions of model for find changes...")
|
||||
|
||||
for table in tables:
|
||||
#WebModel.query(WebModel, "")
|
||||
#Check if new table
|
||||
|
||||
#fields_to_add, fields_to_modify, fields_to_add_index, fields_to_add_constraint, fields_to_add_unique, fields_to_delete_index, fields_to_delete_unique, fields_to_delete_constraint, fields_to_delete
|
||||
|
||||
fields_to_add=[]
|
||||
fields_to_modify=[]
|
||||
fields_to_add_index=[]
|
||||
fields_to_add_constraint=[]
|
||||
fields_to_add_unique=[]
|
||||
fields_to_delete_index=[]
|
||||
fields_to_delete_unique=[]
|
||||
fields_to_delete_constraint=[]
|
||||
fields_to_delete=[]
|
||||
|
||||
old_table='old_'+table
|
||||
|
||||
for f, v in WebModel.model[table].fields.items():
|
||||
|
||||
if not f in WebModel.model[old_table].fields:
|
||||
|
||||
fields_to_add.append(f)
|
||||
|
||||
#Add index
|
||||
|
||||
if v.indexed==True:
|
||||
|
||||
fields_to_add_index.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
#Add unique
|
||||
|
||||
if v.unique==True:
|
||||
|
||||
fields_to_add_unique.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
#Add constraint
|
||||
|
||||
if v.foreignkey==True:
|
||||
|
||||
fields_to_add_constraint.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
changes+=1
|
||||
|
||||
#If exists field in old webmodel and new
|
||||
|
||||
else:
|
||||
|
||||
v_old=WebModel.model[old_table].fields[f]
|
||||
|
||||
if v.get_type_sql()!=v_old.get_type_sql():
|
||||
|
||||
fields_to_modify.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
#Add index
|
||||
|
||||
if v.indexed==True and v_old.indexed==False:
|
||||
|
||||
fields_to_add_index.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
if v.indexed==False and v_old.indexed==True:
|
||||
|
||||
fields_to_delete_index.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
#Add unique
|
||||
|
||||
if v.unique==True and v_old.unique==False:
|
||||
|
||||
fields_to_add_unique.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
if v.unique==False and v_old.unique==True:
|
||||
|
||||
fields_to_delete_unique.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
#Add constraint
|
||||
|
||||
if v.foreignkey==True and v_old.foreignkey==False:
|
||||
|
||||
fields_to_add_constraint.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
if v.foreignkey==False and v_old.foreignkey==True:
|
||||
|
||||
fields_to_delete_constraint.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
for f, v in WebModel.model[old_table].fields.items():
|
||||
|
||||
if not f in WebModel.model[table].fields:
|
||||
|
||||
#Add constraint
|
||||
|
||||
if v.foreignkey==True:
|
||||
|
||||
fields_to_delete_constraint.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
fields_to_delete.append(f)
|
||||
|
||||
changes+=1
|
||||
|
||||
WebModel.model[table].update_table(fields_to_add, fields_to_modify, fields_to_add_index, fields_to_add_constraint, fields_to_add_unique, fields_to_delete_index, fields_to_delete_unique, fields_to_delete_constraint, fields_to_delete)
|
||||
|
||||
#for field_update in arr_update:
|
||||
|
||||
|
||||
#Make a for in fields, if the field not exist in old model, create, if is not same type, recreate. If no have index now, delete index, if is a new index, create, same thing with uniques
|
||||
|
||||
#for field in WebModel.model
|
||||
|
||||
except ImportError:
|
||||
|
||||
pass
|
||||
|
||||
except:
|
||||
|
||||
print("Exception in user code:")
|
||||
print("-"*60)
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
print("-"*60)
|
||||
|
||||
exit(1)
|
||||
|
||||
original_file_path=args.model.replace('.', '/')+'.py'
|
||||
|
||||
backup_path='backups/'+original_file_path
|
||||
|
||||
if changes>0:
|
||||
print(Style.BRIGHT+"Creating backup of the model. WARNING: DON'T DELETE BACKUPS DIRECTORY IF YOU WANT MAKE CHANGES IN THE FUTURE WITHOUT MODIFY DIRECTLY THE DATABASE")
|
||||
|
||||
create_backup(original_file_path, backup_path)
|
||||
|
||||
else:
|
||||
if not os.path.isfile(backup_path):
|
||||
create_backup(original_file_path, backup_path)
|
||||
|
||||
|
||||
print(Style.BRIGHT+"All tasks finished")
|
||||
|
||||
def create_backup(original_file_path, file_path):
|
||||
|
||||
#Create copy
|
||||
|
||||
path=os.path.dirname(file_path)
|
||||
|
||||
p=Path(path)
|
||||
|
||||
if not p.is_dir():
|
||||
p.mkdir(0o755, True)
|
||||
|
||||
#Create path
|
||||
|
||||
if os.path.isfile(file_path):
|
||||
today = date.today()
|
||||
shutil.copy(file_path, file_path+'.'+today.strftime("%Y%M%d%H%M%S"))
|
||||
|
||||
new_file=""
|
||||
|
||||
f=open(original_file_path)
|
||||
|
||||
for line in f:
|
||||
"""
|
||||
new_line=line.replace("model[\"", "model[\"old_")
|
||||
new_line=new_line.replace("model['", "model['old_")
|
||||
|
||||
new_line=new_line.replace("WebModel(\"", "WebModel(\"old_")
|
||||
new_line=new_line.replace("WebModel('", "WebModel('old_")
|
||||
"""
|
||||
new_file+=line
|
||||
|
||||
f.close()
|
||||
|
||||
f=open(file_path, 'w')
|
||||
|
||||
f.write(new_file)
|
||||
|
||||
f.close()
|
||||
0
cromosoma/extrafields/__init__.py
Normal file
0
cromosoma/extrafields/__init__.py
Normal file
18
cromosoma/extrafields/emailfield.py
Normal file
18
cromosoma/extrafields/emailfield.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from paramecio.cromosoma.corefields import CharField
|
||||
import re
|
||||
|
||||
mail_pattern=re.compile("\w[\w\.-]*@\w[\w\.-]+\.\w+")
|
||||
|
||||
class EmailField(CharField):
|
||||
|
||||
def check(self, value):
|
||||
|
||||
self.error=False
|
||||
self.txt_error=''
|
||||
|
||||
if not mail_pattern.match(value):
|
||||
|
||||
self.error=True
|
||||
self.txt_error='No valid format'
|
||||
|
||||
return value
|
||||
49
cromosoma/extrafields/passwordfield.py
Normal file
49
cromosoma/extrafields/passwordfield.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from paramecio.cromosoma.corefields import PhangoField
|
||||
from paramecio.cromosoma.coreforms import PasswordForm
|
||||
from passlib.hash import bcrypt
|
||||
|
||||
class PasswordField(PhangoField):
|
||||
|
||||
def __init__(self, name, size=255, required=False):
|
||||
|
||||
super(PasswordField, self).__init__(name, size, required)
|
||||
self.protected=True
|
||||
self.name_form=PasswordForm
|
||||
self.default_value=''
|
||||
|
||||
def check(self, value):
|
||||
|
||||
self.txt_error=''
|
||||
self.error=False
|
||||
|
||||
value.strip()
|
||||
|
||||
if value=='':
|
||||
|
||||
if self.model!=None:
|
||||
|
||||
if self.model.updated==True:
|
||||
self.required=False
|
||||
self.check_blank=True
|
||||
return ""
|
||||
else:
|
||||
|
||||
self.txt_error="The value is empty"
|
||||
self.error=True
|
||||
|
||||
else:
|
||||
self.txt_error="The value is empty"
|
||||
self.error=True
|
||||
|
||||
else:
|
||||
value = bcrypt.encrypt(value)
|
||||
|
||||
|
||||
return value
|
||||
|
||||
@staticmethod
|
||||
def verify( password, h):
|
||||
|
||||
return bcrypt.verify(password, h)
|
||||
|
||||
|
||||
42
cromosoma/formsutils.py
Normal file
42
cromosoma/formsutils.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from paramecio.cromosoma import corefields
|
||||
from bottle import request
|
||||
|
||||
def pass_values_to_form(post, arr_form, yes_error=True):
|
||||
|
||||
for key, value in arr_form.items():
|
||||
post[key]=post.get(key, '')
|
||||
|
||||
arr_form[key].default_value=post[key]
|
||||
|
||||
if arr_form[key].field==None:
|
||||
arr_form[key].field=corefields.CharField(key, 255, required=False)
|
||||
|
||||
# Recheck value if no set error field
|
||||
if arr_form[key].field.error == None:
|
||||
arr_form[key].field.check(post[key])
|
||||
|
||||
arr_form[key].txt_error=""
|
||||
|
||||
if arr_form[key].required==True and arr_form[key].field.error==True and yes_error==True:
|
||||
arr_form[key].txt_error=arr_form[key].field.txt_error
|
||||
|
||||
# Reset error on field.
|
||||
|
||||
arr_form[key].field.error=None
|
||||
|
||||
return arr_form
|
||||
|
||||
def show_form(post, arr_form, t, yes_error=True, modelform_tpl='forms/modelform.phtml'):
|
||||
|
||||
pass_values_to_form(post, arr_form, yes_error)
|
||||
|
||||
return t.load_template(modelform_tpl, forms=arr_form)
|
||||
|
||||
#Function for initial values for necessary fields.
|
||||
|
||||
def ini_fields(fields):
|
||||
pass
|
||||
|
||||
|
||||
134
cromosoma/usermodel.py
Normal file
134
cromosoma/usermodel.py
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from paramecio.cromosoma.webmodel import WebModel
|
||||
from paramecio.cromosoma.coreforms import PasswordForm
|
||||
from paramecio.citoplasma.i18n import I18n
|
||||
from paramecio.citoplasma.httputils import GetPostFiles
|
||||
|
||||
class UserModel(WebModel):
|
||||
|
||||
def __init__(self, name_field_id="id"):
|
||||
|
||||
super().__init__(name_field_id)
|
||||
|
||||
self.password_field='password'
|
||||
self.email_field='email'
|
||||
self.username_field='username'
|
||||
self.yes_repeat_password=True
|
||||
|
||||
def create_forms(self, arr_fields={}):
|
||||
|
||||
# Add password_repeat to forms from the model
|
||||
|
||||
super().create_forms(arr_fields)
|
||||
|
||||
if self.password_field in arr_fields and self.yes_repeat_password:
|
||||
|
||||
repeat_password=PasswordForm('repeat_password', '')
|
||||
|
||||
repeat_password.required=1
|
||||
|
||||
repeat_password.label=I18n.lang('common', 'repeat_password', 'Repeat Password')
|
||||
|
||||
self.create_form_after(self.password_field, repeat_password)
|
||||
"""
|
||||
def insert(self, dict_values, external_agent=True):
|
||||
|
||||
if 'password' in dict_values:
|
||||
|
||||
dict_values['repeat_password']=dict_values.get('repeat_password', '')
|
||||
|
||||
if dict_values['repeat_password']!=dict_values[self.password_field]:
|
||||
self.fields[self.password_field].error=True
|
||||
self.fields['password'].txt_error=I18n.lang('common', 'error_passwords_no_match', 'Error: passwords doesn\'t match')
|
||||
|
||||
return super().insert(dict_values, external_agent)
|
||||
"""
|
||||
|
||||
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
||||
|
||||
try:
|
||||
|
||||
fields, values, update_values=super().check_all_fields(dict_values, external_agent, yes_update, errors_set)
|
||||
except:
|
||||
|
||||
return False
|
||||
|
||||
# Check if passwords matches
|
||||
|
||||
if self.password_field in dict_values:
|
||||
|
||||
dict_values['repeat_password']=dict_values.get('repeat_password', '')
|
||||
|
||||
if dict_values['repeat_password']!=dict_values[self.password_field]:
|
||||
|
||||
if dict_values[self.password_field].strip()!="":
|
||||
|
||||
self.fields[self.password_field].error=True
|
||||
self.fields[self.password_field].txt_error=I18n.lang('common', 'error_passwords_no_match', 'Error: passwords doesn\'t match')
|
||||
|
||||
return False
|
||||
|
||||
# Check if exists user with same email or password
|
||||
|
||||
get_id=0
|
||||
|
||||
if self.updated:
|
||||
# Need the id
|
||||
GetPostFiles.obtain_get()
|
||||
GetPostFiles.obtain_post()
|
||||
|
||||
get_id=GetPostFiles.get.get(self.name_field_id, '0')
|
||||
|
||||
post_id=GetPostFiles.post.get(self.name_field_id, '0')
|
||||
|
||||
if get_id!='0':
|
||||
get_id=int(get_id)
|
||||
|
||||
if post_id!='0':
|
||||
get_id=int(post_id)
|
||||
|
||||
pass
|
||||
|
||||
sql_id=''
|
||||
|
||||
original_conditions=self.conditions
|
||||
|
||||
self.reset_conditions()
|
||||
|
||||
if self.username_field in dict_values:
|
||||
|
||||
self.conditions=['WHERE username=%s', [dict_values[self.username_field]]]
|
||||
|
||||
|
||||
if self.email_field in dict_values:
|
||||
|
||||
if len(self.conditions[1])>0:
|
||||
|
||||
self.conditions[0]+=' OR email=%s'
|
||||
else:
|
||||
self.conditions[0]='WHERE email=%s'
|
||||
self.conditions[1]=[]
|
||||
|
||||
self.conditions[1].append([dict_values[self.email_field]])
|
||||
|
||||
if get_id>0:
|
||||
self.conditions[0]=' AND '+self.username_field+'=%s'
|
||||
self.conditions[1].append(get_id)
|
||||
|
||||
|
||||
if self.select_count()>0:
|
||||
|
||||
self.fields[self.username_field].error=True
|
||||
self.fields[self.username_field].txt_error=I18n.lang('common', 'error_username_or_password_exists', 'Error: username or email exists in database')
|
||||
|
||||
return False
|
||||
|
||||
self.conditions=original_conditions
|
||||
|
||||
return fields, values, update_values
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
879
cromosoma/webmodel.py
Normal file
879
cromosoma/webmodel.py
Normal file
|
|
@ -0,0 +1,879 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import re
|
||||
import uuid
|
||||
from importlib import import_module, reload
|
||||
from collections import OrderedDict
|
||||
from paramecio.cromosoma.databases.mysql import SqlClass
|
||||
from paramecio.cromosoma.coreforms import BaseForm, HiddenForm
|
||||
|
||||
# The most important class for the framework
|
||||
#
|
||||
# Webmodel is a class for create objects that represent models. This models are a mirage of SQL tables. You can create fields, add indexes, foreign keys, and more.
|
||||
#
|
||||
#
|
||||
|
||||
class WebModel:
|
||||
|
||||
#Globals class variables for internal tasks
|
||||
|
||||
arr_sql_index={}
|
||||
arr_sql_set_index={}
|
||||
arr_sql_unique={}
|
||||
arr_sql_set_unique={}
|
||||
last_query=""
|
||||
|
||||
make_connection=SqlClass.connect_to_db
|
||||
|
||||
#A dictionary for add models here
|
||||
|
||||
model=OrderedDict()
|
||||
|
||||
connections={'default': {'host': 'localhost', 'user': 'user', 'password': '', 'db': 'default', 'charset': 'utf8', 'set_connection': False} }
|
||||
|
||||
connection_id="default"
|
||||
|
||||
webmodel=True
|
||||
|
||||
# Init the class
|
||||
|
||||
def __init__(self, name_field_id="id"):
|
||||
|
||||
#The name of the table
|
||||
|
||||
self.name=type(self).__name__.lower()
|
||||
|
||||
self.label=self.name
|
||||
|
||||
self.label_general=self.name
|
||||
|
||||
self.name_field_id=name_field_id
|
||||
|
||||
#Fields of the table, inserte with register method
|
||||
|
||||
self.fields=OrderedDict()
|
||||
|
||||
#The tables related with foreignkeyfield to this table
|
||||
|
||||
self.related=[]
|
||||
|
||||
#A dictionary where forms of this model are saved
|
||||
|
||||
self.forms=OrderedDict()
|
||||
|
||||
self.cache_method=''
|
||||
|
||||
# A dictionary with the errors in fields.
|
||||
|
||||
self.fields_errors={}
|
||||
|
||||
self.errors={}
|
||||
|
||||
self.num_errors=0
|
||||
|
||||
self.query_error=""
|
||||
|
||||
self.conditions=["WHERE 1=1", []]
|
||||
|
||||
self.order_by="ORDER BY `"+self.name+"`.`id` ASC"
|
||||
|
||||
self.limit=""
|
||||
|
||||
self.related_models_deleted=[]
|
||||
|
||||
self.required_save={}
|
||||
|
||||
#Create id field
|
||||
self.register(PrimaryKeyField(self.name_field_id))
|
||||
|
||||
#self.model[name]=self
|
||||
|
||||
self.yes_reset_conditions=True
|
||||
|
||||
self.create_fields()
|
||||
|
||||
self.updated=False
|
||||
|
||||
self.valid_fields=[]
|
||||
|
||||
# A method where create the new fields of this model
|
||||
|
||||
def create_fields(self):
|
||||
|
||||
pass
|
||||
|
||||
# A method for register the fields
|
||||
|
||||
def register(self, field_model):
|
||||
|
||||
#self.fields_required[field_model]=field_model.required
|
||||
|
||||
self.fields[field_model.name]=field_model
|
||||
|
||||
self.fields[field_model.name].model=self
|
||||
|
||||
# A method for create the id field.
|
||||
|
||||
def create_id_field(self, field_name="id"):
|
||||
pass
|
||||
|
||||
# A method for select rows from a database.
|
||||
|
||||
def connect_to_db(self):
|
||||
|
||||
if WebModel.make_connection(SqlClass, self.connections[self.connection_id])==False:
|
||||
raise NameError(SqlClass.error_connection)
|
||||
|
||||
WebModel.make_connection=SqlClass.dummy_connect
|
||||
|
||||
def dummy_connect(self, connection):
|
||||
return True
|
||||
|
||||
# Static method for make queries
|
||||
@staticmethod
|
||||
def query(WebModel, str_query, args=[], connection_id='default'):
|
||||
WebModel.connect_to_db(WebModel)
|
||||
return SqlClass.query(SqlClass, str_query, args, connection_id)
|
||||
|
||||
# Insert method, for insert a row in database.using a dictionary
|
||||
# External agent define if the update is in code or from external source, how a form.
|
||||
|
||||
def insert(self, dict_values, external_agent=True):
|
||||
|
||||
# Connect to db
|
||||
|
||||
self.connect_to_db()
|
||||
|
||||
self.query_error=''
|
||||
|
||||
self.fields[self.name_field_id].required=False
|
||||
|
||||
try:
|
||||
|
||||
fields, values, update_values=self.check_all_fields(dict_values, external_agent)
|
||||
|
||||
except:
|
||||
self.query_error='Cannot insert the new row'
|
||||
return False
|
||||
|
||||
sql="insert into `"+self.name+"` (`"+"`, `".join(fields)+"`) VALUES ("+", ".join(values)+")"
|
||||
|
||||
cursor=SqlClass.query(SqlClass, sql, self.conditions[1], self.connection_id)
|
||||
|
||||
if cursor.rowcount>0:
|
||||
|
||||
return True
|
||||
else:
|
||||
self.query_error='Cannot insert the new row'
|
||||
return False
|
||||
|
||||
# Update method. For update one or many rows.
|
||||
|
||||
def update(self, dict_values, external_agent=True):
|
||||
|
||||
# Connect to db
|
||||
|
||||
self.fields[self.name_field_id].required=False
|
||||
|
||||
if self.name_field_id in dict_values:
|
||||
del dict_values[self.name_field_id]
|
||||
|
||||
self.connect_to_db()
|
||||
|
||||
self.query_error=''
|
||||
|
||||
#try:
|
||||
self.updated=True
|
||||
|
||||
try:
|
||||
|
||||
fields, values, update_values=self.check_all_fields(dict_values, external_agent, True, 'update')
|
||||
|
||||
except:
|
||||
return False
|
||||
|
||||
sql="update `"+self.name+"` SET "+", ".join(update_values)+" "+self.conditions[0]
|
||||
|
||||
cursor=SqlClass.query(SqlClass, sql, self.conditions[1], self.connection_id)
|
||||
|
||||
if self.yes_reset_conditions:
|
||||
self.reset_conditions()
|
||||
|
||||
return True
|
||||
|
||||
"""
|
||||
if cursor.rowcount>0:
|
||||
|
||||
if self.yes_reset_conditions:
|
||||
self.reset_conditions()
|
||||
|
||||
return True
|
||||
|
||||
else:
|
||||
|
||||
self.query_error='Cannot update the row'
|
||||
|
||||
return False
|
||||
"""
|
||||
"""
|
||||
except:
|
||||
|
||||
#self.query_error=SqlClass.error_connection
|
||||
e = sys.exc_info()[0]
|
||||
v = sys.exc_info()[1]
|
||||
|
||||
self.error_connection="Error in query: %s %s" % (e, v)
|
||||
|
||||
return False
|
||||
"""
|
||||
|
||||
def reset_conditions(self):
|
||||
|
||||
self.conditions=["WHERE 1=1", []]
|
||||
|
||||
|
||||
# A method for select fields from a table in db. Support for foreignkeys.
|
||||
#Type assoc can be assoc for return dictionaries
|
||||
|
||||
def select(self, arr_select=[], raw_query=0):
|
||||
|
||||
# Connect to db
|
||||
|
||||
self.connect_to_db()
|
||||
|
||||
conditions=self.conditions
|
||||
|
||||
final_fields=[]
|
||||
|
||||
extra_fields=[]
|
||||
|
||||
self.query_error=''
|
||||
|
||||
#First table selecction
|
||||
|
||||
tables_to_select=['`'+self.name+'`']
|
||||
|
||||
keys=list(self.fields.keys())
|
||||
|
||||
if len(arr_select)==0:
|
||||
arr_select=keys
|
||||
|
||||
# Array intersect for obtain the valid fields
|
||||
|
||||
fields = list(set(keys) & set(arr_select))
|
||||
|
||||
#Creating the fields
|
||||
|
||||
for field in fields:
|
||||
|
||||
#Check if foreignkeyfield
|
||||
|
||||
if type(self.fields[field]).__name__=="ForeignKeyField" and raw_query==0:
|
||||
|
||||
table_name=self.fields[field].table_name
|
||||
|
||||
tables_to_select.append('`'+table_name+'`')
|
||||
|
||||
# Add field from related table
|
||||
# as "+table_name+"_"+self.fields[field].named_field
|
||||
extra_fields.append("`"+table_name+"`.`"+self.fields[field].named_field+"` as "+field)
|
||||
|
||||
# Add a condition to sql query for join the two tables.
|
||||
|
||||
conditions[0]+=" AND `"+table_name+"`.`"+self.fields[field].identifier_field+"`=`"+self.name+"`.`"+field+"`"
|
||||
|
||||
# Add extra fields from related table from select_fields ForeignKeyField class member
|
||||
|
||||
for extra_field in self.fields[field].select_fields:
|
||||
|
||||
extra_fields.append("`"+table_name+"`.`"+extra_field+"` as `"+table_name+"_"+extra_field+"`")
|
||||
else:
|
||||
# Add normal field to sql query
|
||||
|
||||
final_fields.append("`"+self.name+"`.`"+field+"`")
|
||||
|
||||
extra_sql_field=""
|
||||
|
||||
if len(extra_fields)>0:
|
||||
|
||||
extra_sql_field=", "+", ".join(extra_fields)
|
||||
|
||||
if len(final_fields)==0:
|
||||
self.query_error="Error: without fields to search"
|
||||
return False
|
||||
|
||||
sql= ("select "+", ".join(final_fields)+extra_sql_field+" from "+", ".join(tables_to_select)+' '+conditions[0]+' '+self.order_by+' '+self.limit).strip()
|
||||
|
||||
self.last_query=sql
|
||||
|
||||
if self.yes_reset_conditions:
|
||||
self.reset_conditions()
|
||||
|
||||
cursor=SqlClass.query(SqlClass, sql, conditions[1], self.connection_id)
|
||||
|
||||
if cursor==False:
|
||||
self.query_error=SqlClass.error_connection
|
||||
return False
|
||||
else:
|
||||
return cursor
|
||||
|
||||
# Show results in a dictionary
|
||||
|
||||
def fetch(self, cursor):
|
||||
|
||||
return cursor.fetchone()
|
||||
|
||||
def insert_id(self, cursor):
|
||||
|
||||
return cursor.lastrowid
|
||||
|
||||
def element_exists(self, id):
|
||||
|
||||
self.conditions=['WHERE `'+self.name_field_id+'`=%s', [id]]
|
||||
|
||||
count=self.select_count(self.name_field_id)
|
||||
|
||||
if self.yes_reset_conditions:
|
||||
self.reset_conditions()
|
||||
|
||||
if count>0:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def select_a_field(self, field):
|
||||
pass
|
||||
|
||||
def select_a_row(self, id, fields_selected=[], raw_query=0):
|
||||
|
||||
self.conditions=['WHERE `'+self.name+'`.`'+self.name_field_id+'`=%s', [id]]
|
||||
|
||||
self.limit="limit 1"
|
||||
|
||||
cursor=self.select(fields_selected, raw_query)
|
||||
|
||||
self.reset_conditions()
|
||||
|
||||
row=cursor.fetchone()
|
||||
|
||||
if row==None:
|
||||
row=False
|
||||
|
||||
return row
|
||||
|
||||
def select_a_row_where(self, fields_selected=[], raw_query=0):
|
||||
|
||||
self.limit="limit 1"
|
||||
|
||||
cursor=self.select(fields_selected, raw_query)
|
||||
|
||||
row=cursor.fetchone()
|
||||
|
||||
if row==None:
|
||||
row=False
|
||||
|
||||
return row
|
||||
|
||||
|
||||
def select_to_array(self, fields_selected=[], raw_query=0):
|
||||
|
||||
if len(fields_selected) > 0 and (self.name_field_id not in fields_selected):
|
||||
fields_selected.append(self.name_field_id)
|
||||
|
||||
cursor=self.select(fields_selected, raw_query)
|
||||
|
||||
results={}
|
||||
|
||||
for row in cursor:
|
||||
|
||||
results[row[self.name_field_id]]=row
|
||||
|
||||
return results
|
||||
|
||||
|
||||
# A method por count num rows affected for sql conditions
|
||||
|
||||
def select_count(self, field_to_count='id', raw_query=1):
|
||||
|
||||
# Connect to db
|
||||
|
||||
self.connect_to_db()
|
||||
|
||||
conditions=self.conditions
|
||||
|
||||
#First table selecction
|
||||
|
||||
tables_to_select=['`'+self.name+'`']
|
||||
|
||||
fields=list(self.fields.keys())
|
||||
|
||||
#Creating the fields
|
||||
|
||||
for field in fields:
|
||||
|
||||
#Check if foreignkeyfield
|
||||
|
||||
if type(self.fields[field]).__name__=="ForeignKeyField" and raw_query==0:
|
||||
|
||||
table_name=self.fields[field].table_name
|
||||
|
||||
tables_to_select.append('`'+table_name+'`')
|
||||
|
||||
# Add a condition to sql query for join the two tables.
|
||||
|
||||
conditions[0]+=" AND `"+table_name+"`.`"+self.fields[field].identifier_field+"`=`"+self.name+"`.`"+field+"`"
|
||||
|
||||
sql= "select count(`"+field_to_count+"`) from "+", ".join(tables_to_select)+conditions[0]
|
||||
|
||||
cursor=SqlClass.query(SqlClass, sql, conditions[1], self.connection_id)
|
||||
|
||||
count=list(cursor.fetchone().values())[0]
|
||||
|
||||
if self.yes_reset_conditions:
|
||||
self.reset_conditions()
|
||||
|
||||
return count
|
||||
|
||||
#+' ORDER BY '+self.order_by+' '+self.limit).strip()
|
||||
|
||||
# A method for delete rows using sql conditions
|
||||
|
||||
def delete(self):
|
||||
|
||||
self.connect_to_db()
|
||||
|
||||
#Need delete rows from other related tables save in self.related_models_deleted
|
||||
|
||||
sql="delete from `"+self.name+"` "+self.conditions[0]
|
||||
|
||||
result=SqlClass.query(SqlClass, sql, self.conditions[1], self.connection_id)
|
||||
|
||||
if self.yes_reset_conditions:
|
||||
self.reset_conditions()
|
||||
|
||||
return result
|
||||
|
||||
# Method for create sql tables
|
||||
|
||||
def create_table(self):
|
||||
|
||||
#self.connect_to_db()
|
||||
|
||||
self.arr_sql_index[self.name]={}
|
||||
self.arr_sql_set_index[self.name]={}
|
||||
self.arr_sql_unique[self.name]={}
|
||||
self.arr_sql_set_unique[self.name]={}
|
||||
|
||||
#foreach($this->components as $field => $data)
|
||||
table_fields=[]
|
||||
|
||||
#Create id field
|
||||
#Not neccesary
|
||||
#table_fields.append('`'+self.name_field_id+"` INT NOT NULL PRIMARY KEY AUTO_INCREMENT")
|
||||
|
||||
for field, data in self.fields.items():
|
||||
|
||||
table_fields.append('`'+field+'` '+data.get_type_sql())
|
||||
|
||||
#Check if indexed
|
||||
|
||||
if self.fields[field].indexed==True:
|
||||
|
||||
self.arr_sql_index[self.name][field]='CREATE INDEX `index_'+self.name+'_'+field+'` ON '+self.name+'(`'+field+'`);'
|
||||
self.arr_sql_set_index[self.name][field]=""
|
||||
|
||||
|
||||
#Check if unique
|
||||
|
||||
if self.fields[field].unique==True:
|
||||
|
||||
self.arr_sql_unique[self.name][field]='ALTER TABLE `'+self.name+'` ADD UNIQUE (`'+field+'`)'
|
||||
self.arr_sql_set_unique[self.name][field]=""
|
||||
|
||||
if type(self.fields[field]).__name__=="ForeignKeyField":
|
||||
|
||||
self.arr_sql_index[self.name][field]='CREATE INDEX `index_'+self.name+'_'+field+'` ON '+self.name+'(`'+field+'`);'
|
||||
|
||||
table_related=self.fields[field].table_name
|
||||
|
||||
id_table_related=self.fields[field].table_id
|
||||
|
||||
self.arr_sql_set_index[self.name][field]='ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE RESTRICT ON UPDATE RESTRICT;'
|
||||
|
||||
return "create table `"+self.name+"` (\n"+",\n".join(table_fields)+"\n) DEFAULT CHARSET=utf8;";
|
||||
|
||||
def update_table(self, fields_to_add, fields_to_modify, fields_to_add_index, fields_to_add_constraint, fields_to_add_unique, fields_to_delete_index, fields_to_delete_unique, fields_to_delete_constraint, fields_to_delete):
|
||||
|
||||
#Obtain new fields
|
||||
|
||||
for field in fields_to_modify:
|
||||
print("---Updating "+field+" in "+self.name)
|
||||
WebModel.query(WebModel, 'ALTER TABLE `'+self.name+'` MODIFY `'+field+'` '+self.fields[field].get_type_sql(), [], self.connection_id)
|
||||
|
||||
for field in fields_to_add:
|
||||
print("---Adding "+field+" in "+self.name)
|
||||
WebModel.query(WebModel, 'ALTER TABLE `'+self.name+'` ADD `'+field+'` '+self.fields[field].get_type_sql(), [], self.connection_id)
|
||||
|
||||
for field in fields_to_add_index:
|
||||
print("---Adding index to "+field+" in "+self.name)
|
||||
WebModel.query(WebModel, 'CREATE INDEX `index_'+self.name+'_'+field+'` ON '+self.name+' (`'+field+'`);', [], self.connection_id)
|
||||
|
||||
for field in fields_to_add_constraint:
|
||||
|
||||
print("---Adding foreign key to "+field+" in "+self.name)
|
||||
|
||||
table_related=self.fields[field].table_name
|
||||
|
||||
id_table_related=self.fields[field].table_id
|
||||
|
||||
WebModel.query(WebModel, 'ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE RESTRICT ON UPDATE RESTRICT;', [], self.connection_id)
|
||||
|
||||
for field in fields_to_add_unique:
|
||||
|
||||
print("---Adding unique to "+field+" in "+self.name)
|
||||
|
||||
WebModel.query(WebModel, 'ALTER TABLE `'+self.name+'` ADD UNIQUE (`'+field+'`)', [], self.connection_id)
|
||||
|
||||
for field in fields_to_delete_index:
|
||||
|
||||
print("---Deleting index from "+field+" in "+self.name)
|
||||
|
||||
WebModel.query(WebModel, 'DROP INDEX `index_'+self.name+'_'+field+'` ON '+self.name, [], self.connection_id)
|
||||
|
||||
for field in fields_to_delete_unique:
|
||||
|
||||
print("---Deleting unique from "+field+" in "+self.name)
|
||||
|
||||
WebModel.query(WebModel, 'DROP INDEX `'+field+'` ON '+self.name, [], self.connection_id)
|
||||
|
||||
for field in fields_to_delete_constraint:
|
||||
|
||||
print("---Deleting foreignkey from "+field+" in "+self.name)
|
||||
|
||||
WebModel.query(WebModel, 'ALTER TABLE `'+self.name+'` DROP FOREIGN KEY '+field+'_'+self.name+'IDX', [], self.connection_id)
|
||||
|
||||
for field in fields_to_delete:
|
||||
|
||||
print("---Deleting "+field+" from "+self.name)
|
||||
|
||||
WebModel.query(WebModel, 'ALTER TABLE `'+self.name+'` DROP `'+field+'`', [], self.connection_id)
|
||||
#Deleting indexes and constraints.
|
||||
|
||||
# Method for drop sql tables and related
|
||||
|
||||
def drop_table(name):
|
||||
pass
|
||||
|
||||
#Return an array with all fields
|
||||
|
||||
def all_fields():
|
||||
pass
|
||||
|
||||
#Check of all fields in table.
|
||||
|
||||
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
||||
|
||||
fields=[]
|
||||
values=[]
|
||||
update_values=[]
|
||||
self.errors[errors_set]=[]
|
||||
self.num_errors=0
|
||||
#A dictionary that define if update property is added
|
||||
|
||||
updated_field={}
|
||||
updated_field['insert']=0
|
||||
updated_field['update']=1
|
||||
|
||||
|
||||
error=False
|
||||
|
||||
if yes_update==True:
|
||||
f_update=lambda field, value: "`"+field+"`="+value+""
|
||||
else:
|
||||
f_update=lambda field, value: ""
|
||||
|
||||
# I can optimize this later
|
||||
|
||||
for k, v in self.fields.items():
|
||||
|
||||
#List where the errors are saved
|
||||
|
||||
self.fields_errors[k]=[]
|
||||
|
||||
if k in dict_values:
|
||||
|
||||
# If fields is protected, but external_agent =0, then insert
|
||||
# If fields is not protected always insert if not error checking
|
||||
|
||||
value=dict_values[k]
|
||||
|
||||
# Need rewrite the error because shitty python don't clean nothing
|
||||
|
||||
self.fields[k].error=False
|
||||
|
||||
if (self.fields[k].protected==None or self.fields[k].protected==False or external_agent==False) and k in self.valid_fields:
|
||||
|
||||
self.fields[k].update=updated_field[errors_set]
|
||||
|
||||
value=self.fields[k].check(value)
|
||||
|
||||
if self.fields[k].check_blank==False or self.updated==False:
|
||||
|
||||
# If error checking, value=False
|
||||
|
||||
if self.fields[k].error==True and self.fields[k].required==True:
|
||||
|
||||
#Error, need this fields.
|
||||
self.num_errors+=1
|
||||
|
||||
self.fields_errors[k].append("Error: "+v.label+" field required")
|
||||
|
||||
error=True
|
||||
|
||||
else:
|
||||
|
||||
fields.append(k)
|
||||
|
||||
final_value=self.fields[k].quot_open+value+self.fields[k].quot_close
|
||||
|
||||
values.append(final_value)
|
||||
|
||||
update_values.append(f_update(k, final_value))
|
||||
|
||||
else:
|
||||
self.num_errors+=1
|
||||
|
||||
self.fields_errors[k].append("Error: "+self.fields[k].label+" is protected field")
|
||||
self.fields[k].error=True
|
||||
self.fields[k].txt_error="Error: "+self.fields[k].label+" is protected field"
|
||||
error=True
|
||||
|
||||
elif v.required==True:
|
||||
|
||||
self.num_errors+=1
|
||||
|
||||
self.fields_errors[k].append("Error: "+v.label+" field required")
|
||||
error=True
|
||||
|
||||
if len(fields)==0:
|
||||
|
||||
self.num_errors+=1
|
||||
|
||||
self.errors[errors_set].append("Error: no elements to insert in table")
|
||||
|
||||
error=True
|
||||
|
||||
if error==True:
|
||||
|
||||
self.num_errors+=1
|
||||
|
||||
self.errors[errors_set].append("Error: error checking the values of the table")
|
||||
|
||||
return False
|
||||
|
||||
return (fields, values, update_values)
|
||||
|
||||
|
||||
#Reset the require field in fields
|
||||
|
||||
def reset_require(self):
|
||||
|
||||
for k, v in self.fields.items():
|
||||
|
||||
self.required_save[k]=self.fields[k].required
|
||||
self.fields[k].required=0
|
||||
|
||||
|
||||
#Reload the require field in fields
|
||||
|
||||
def reload_require(self):
|
||||
|
||||
for k,r in self.fields.items():
|
||||
self.fields[k].required=r
|
||||
|
||||
|
||||
#Create a form based in table.
|
||||
|
||||
def create_forms(self, arr_fields={}):
|
||||
|
||||
if len(arr_fields)==0:
|
||||
arr_fields=self.fields.keys()
|
||||
|
||||
#for name_field, field in self.fields.items():
|
||||
for name_field in arr_fields:
|
||||
self.valid_fields.append(name_field)
|
||||
self.forms[name_field]=self.fields[name_field].create_form()
|
||||
|
||||
def create_form_after(self, form_after, new_form):
|
||||
|
||||
new_dict=OrderedDict()
|
||||
|
||||
for name_form, form in self.forms.items():
|
||||
new_dict[name_form]=form
|
||||
if name_form==form_after:
|
||||
new_dict[new_form.name]=new_form
|
||||
|
||||
self.forms=new_dict
|
||||
|
||||
@staticmethod
|
||||
def close():
|
||||
WebModel.make_connection=SqlClass.connect_to_db
|
||||
SqlClass.close(SqlClass)
|
||||
|
||||
@staticmethod
|
||||
def escape_sql(value):
|
||||
|
||||
value=str(value)
|
||||
|
||||
return value.replace("'","\\'").strip()
|
||||
|
||||
|
||||
class PhangoField:
|
||||
|
||||
def __init__(self, name, size=255, required=False):
|
||||
|
||||
# The name of the field in database table
|
||||
|
||||
self.name=name
|
||||
|
||||
# The label for the Field
|
||||
|
||||
self.label=name.replace('_', ' ').title()
|
||||
|
||||
# If field is required, self.required is True
|
||||
|
||||
self.required=required
|
||||
|
||||
# The size of field in database
|
||||
|
||||
self.size=size
|
||||
|
||||
# Protected, if this value != None, cannot use it in insert or update.
|
||||
|
||||
self.protected=None
|
||||
|
||||
# $quote_open is used if you need a more flexible sql sentence,
|
||||
# @warning USE THIS FUNCTION IF YOU KNOW WHAT YOU ARE DOING
|
||||
|
||||
self.quot_open='\''
|
||||
|
||||
# $quote_close is used if you need a more flexible sql sentence,
|
||||
# @warning USE THIS PROPERTY IF YOU KNOW WHAT YOU ARE DOING
|
||||
|
||||
self.quot_close='\''
|
||||
|
||||
# Variable where the basic text error is saved
|
||||
|
||||
self.error=None
|
||||
|
||||
self.txt_error=""
|
||||
|
||||
# Array for create initial parameters for form..
|
||||
self.parameters=[]
|
||||
|
||||
# Themodel where this component or field live
|
||||
|
||||
self.model=None
|
||||
|
||||
# Property used for set this field how indexed in the database table.
|
||||
|
||||
self.indexed=False
|
||||
|
||||
# Property used for set this field how unique value in the database table.
|
||||
|
||||
self.unique=False
|
||||
|
||||
# Simple property for make more easy identify foreignkeyfields.
|
||||
|
||||
self.foreignkey=False
|
||||
|
||||
# Property that define the default value for this field
|
||||
|
||||
self.default_value=""
|
||||
|
||||
# Property that define if this field is in an update operation or insert operation
|
||||
|
||||
self.update=False
|
||||
|
||||
# Property used for check if this value cannot change if is in blank and is filled
|
||||
|
||||
self.check_blank=False
|
||||
|
||||
# Define the form, when is created forms with create_forms you can change the properties of this class
|
||||
|
||||
self.name_form=BaseForm
|
||||
|
||||
# This method is used for describe the new field in a sql language format.
|
||||
|
||||
|
||||
def get_type_sql(self):
|
||||
|
||||
return 'VARCHAR('+str(self.size)+') NOT NULL DEFAULT "'+self.default_value+'"'
|
||||
|
||||
def show_formatted(self, value):
|
||||
|
||||
return value
|
||||
|
||||
# This method for check the value
|
||||
|
||||
|
||||
def check(self, value):
|
||||
|
||||
self.error=False
|
||||
self.txt_error=''
|
||||
|
||||
value=str(value)
|
||||
|
||||
value=WebModel.escape_sql(value)
|
||||
|
||||
if value=="":
|
||||
self.txt_error="The field is in blank"
|
||||
self.error=True
|
||||
|
||||
|
||||
return value
|
||||
|
||||
def set_relationships(self):
|
||||
pass
|
||||
|
||||
def create_form(self):
|
||||
form=self.name_form(self.name, self.default_value)
|
||||
form.default_value=self.default_value
|
||||
form.required=self.required
|
||||
form.label=self.label
|
||||
form.field=self
|
||||
return form
|
||||
|
||||
class PrimaryKeyField(PhangoField):
|
||||
|
||||
def __init__(self, name, size=11, required=False):
|
||||
super(PrimaryKeyField, self).__init__(name, size, required)
|
||||
self.protected=True
|
||||
self.name_form=HiddenForm
|
||||
self.required=True
|
||||
|
||||
def check(self, value):
|
||||
|
||||
self.error=None
|
||||
self.txt_error=''
|
||||
|
||||
if value=='':
|
||||
value='0'
|
||||
|
||||
value=str(int(value))
|
||||
|
||||
if value==0:
|
||||
self.txt_error="The value is zero"
|
||||
self.error=True
|
||||
|
||||
|
||||
return value
|
||||
|
||||
def get_type_sql(self):
|
||||
|
||||
return 'INT NOT NULL PRIMARY KEY AUTO_INCREMENT'
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue