Added datetime functions
This commit is contained in:
parent
f95c10a935
commit
ded04f96b0
8 changed files with 164 additions and 25 deletions
93
paramecio/citoplasma/datetime.py
Normal file
93
paramecio/citoplasma/datetime.py
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
from datetime import date, datetime, time
|
||||||
|
from babel.dates import format_date, format_datetime, format_time, get_timezone, UTC
|
||||||
|
from settings import config
|
||||||
|
|
||||||
|
#t=datetime.utcnow()
|
||||||
|
|
||||||
|
#format_date(t, locale='es_ES')
|
||||||
|
|
||||||
|
#format_time(t, locale='es_ES')
|
||||||
|
|
||||||
|
# eastern = get_timezone('Europe/Madrid')
|
||||||
|
|
||||||
|
# format_time(t, locale='es_ES', tzdata=eastern)
|
||||||
|
# 20141112030455
|
||||||
|
|
||||||
|
# format_datetime(t, "yyyyLLddhhmmss")
|
||||||
|
|
||||||
|
# format_datetime(t, "yyyyLLddhhmmss", tzinfo=eastern
|
||||||
|
|
||||||
|
# class datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
|
||||||
|
|
||||||
|
# Is saved in format_datetime utc with utcnow if the field is clear.
|
||||||
|
|
||||||
|
# format_datetime(t, locale="es_ES", tzinfo=eastern)
|
||||||
|
|
||||||
|
# Next convert to
|
||||||
|
|
||||||
|
if hasattr(config, 'timezone'):
|
||||||
|
timezone=config.timezone
|
||||||
|
else:
|
||||||
|
timezone='UTC'
|
||||||
|
|
||||||
|
tzutc=get_timezone('UTC')
|
||||||
|
|
||||||
|
tz = get_timezone(timezone)
|
||||||
|
|
||||||
|
# In utc
|
||||||
|
|
||||||
|
def timenow():
|
||||||
|
|
||||||
|
t=datetime.utcnow()
|
||||||
|
|
||||||
|
return format_datetime(t, "yyyyLLddHHmmss", tzutc)
|
||||||
|
|
||||||
|
#In utc
|
||||||
|
|
||||||
|
def normalize_time(year, month, day, hour, minute, second):
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
t=datetime(year, month, day, hour, minute, second)
|
||||||
|
|
||||||
|
return format_datetime(t, "yyyyLLddHHmmss", tzutc)
|
||||||
|
|
||||||
|
except ValueError:
|
||||||
|
|
||||||
|
return timenow()
|
||||||
|
|
||||||
|
def obtain_fields_time(time):
|
||||||
|
|
||||||
|
#year, month, day, hour, minute, second=time
|
||||||
|
|
||||||
|
year=int(time[:4])
|
||||||
|
|
||||||
|
month=int(time[4:6])
|
||||||
|
|
||||||
|
day=int(time[6:8])
|
||||||
|
|
||||||
|
hour=int(time[8:10])
|
||||||
|
|
||||||
|
minute=int(time[10:12])
|
||||||
|
|
||||||
|
second=int(time[12:14])
|
||||||
|
|
||||||
|
return year, month, day, hour, minute, second
|
||||||
|
|
||||||
|
|
||||||
|
# In the format of tzinfo
|
||||||
|
|
||||||
|
def format_tztime(time):
|
||||||
|
|
||||||
|
#try:
|
||||||
|
|
||||||
|
year, month, day, hour, minute, second=obtain_fields_time(time)
|
||||||
|
|
||||||
|
t=datetime(year, month, day, hour, minute, second)
|
||||||
|
|
||||||
|
return format_datetime(t, locale="es_ES", tzinfo=tz)
|
||||||
|
|
||||||
|
#except:
|
||||||
|
|
||||||
|
# return timenow()
|
||||||
|
|
||||||
|
|
@ -5,6 +5,7 @@ from paramecio.citoplasma.mtemplates import set_flash_message
|
||||||
from paramecio.cromosoma.formsutils import show_form
|
from paramecio.cromosoma.formsutils import show_form
|
||||||
from paramecio.citoplasma.i18n import I18n
|
from paramecio.citoplasma.i18n import I18n
|
||||||
from paramecio.citoplasma.httputils import GetPostFiles
|
from paramecio.citoplasma.httputils import GetPostFiles
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
class GenerateAdminClass:
|
class GenerateAdminClass:
|
||||||
|
|
||||||
|
|
@ -44,6 +45,8 @@ class GenerateAdminClass:
|
||||||
|
|
||||||
self.template_admin='utils/admin.phtml'
|
self.template_admin='utils/admin.phtml'
|
||||||
|
|
||||||
|
self.template_verify_delete='utils/verify_delete.phtml'
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
|
|
||||||
GetPostFiles.obtain_get()
|
GetPostFiles.obtain_get()
|
||||||
|
|
@ -52,13 +55,19 @@ class GenerateAdminClass:
|
||||||
|
|
||||||
GetPostFiles.get['id']=GetPostFiles.get.get('id', '0')
|
GetPostFiles.get['id']=GetPostFiles.get.get('id', '0')
|
||||||
|
|
||||||
|
if len(self.model.forms)==0:
|
||||||
|
|
||||||
|
self.model.create_forms()
|
||||||
|
|
||||||
|
edit_forms=OrderedDict()
|
||||||
|
|
||||||
|
for key_form in self.arr_fields_edit:
|
||||||
|
edit_forms[key_form]=self.model.forms[key_form]
|
||||||
|
|
||||||
if GetPostFiles.get['op_admin']=='1':
|
if GetPostFiles.get['op_admin']=='1':
|
||||||
|
|
||||||
post=None
|
post=None
|
||||||
|
|
||||||
if len(self.model.forms)==0:
|
|
||||||
self.model.create_forms()
|
|
||||||
|
|
||||||
title_edit=I18n.lang('common', 'add_new_item', 'Add new item')
|
title_edit=I18n.lang('common', 'add_new_item', 'Add new item')
|
||||||
|
|
||||||
if GetPostFiles.get['id']!='0':
|
if GetPostFiles.get['id']!='0':
|
||||||
|
|
@ -68,11 +77,6 @@ class GenerateAdminClass:
|
||||||
if post==None:
|
if post==None:
|
||||||
post={}
|
post={}
|
||||||
|
|
||||||
edit_forms={}
|
|
||||||
|
|
||||||
for key_form in self.arr_fields_edit:
|
|
||||||
edit_forms[key_form]=self.model.forms[key_form]
|
|
||||||
|
|
||||||
form=show_form(post, edit_forms, self.t, False)
|
form=show_form(post, edit_forms, self.t, False)
|
||||||
|
|
||||||
return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=GetPostFiles.get['id'])
|
return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=GetPostFiles.get['id'])
|
||||||
|
|
@ -106,7 +110,7 @@ class GenerateAdminClass:
|
||||||
redirect(self.url)
|
redirect(self.url)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
form=show_form(post, self.model.forms, self.t, True)
|
form=show_form(post, edit_forms, self.t, True)
|
||||||
return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=GetPostFiles.get['id'])
|
return self.t.load_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=GetPostFiles.get['id'])
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -114,11 +118,19 @@ class GenerateAdminClass:
|
||||||
|
|
||||||
elif GetPostFiles.get['op_admin']=='3':
|
elif GetPostFiles.get['op_admin']=='3':
|
||||||
|
|
||||||
if GetPostFiles.get['id']!='0':
|
verified=GetPostFiles.get.get('verified', '0')
|
||||||
self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [GetPostFiles.get['id']]]
|
|
||||||
self.model.delete()
|
if verified=='1':
|
||||||
set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
|
|
||||||
redirect(self.url)
|
if GetPostFiles.get['id']!='0':
|
||||||
|
self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [GetPostFiles.get['id']]]
|
||||||
|
self.model.delete()
|
||||||
|
set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
|
||||||
|
redirect(self.url)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
return self.t.load_template(self.template_verify_delete, url=self.url, item_id=GetPostFiles.get['id'], op_admin=3, verified=1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return self.t.load_template(self.template_admin, admin=self)
|
return self.t.load_template(self.template_admin, admin=self)
|
||||||
|
|
|
||||||
8
paramecio/citoplasma/templates/utils/verify_delete.phtml
Normal file
8
paramecio/citoplasma/templates/utils/verify_delete.phtml
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<form method="get" action="${url}">
|
||||||
|
<div class="form">
|
||||||
|
<input type="hidden" name="id" value="${item_id}">
|
||||||
|
<input type="hidden" name="op_admin" value="${op_admin}">
|
||||||
|
<input type="hidden" name="verified" value="${verified}">
|
||||||
|
<input type="submit" value="${lang('common', 'delete_item_you_sure', 'Are you sure for delete this item?')}" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
@ -151,7 +151,7 @@ def start():
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
config_text=config_text.replace("modules=['paramecio.modules.welcome']", "modules=['paramecio.modules.welcome', 'paramecio.modules.admin']")
|
config_text=config_text.replace("modules=['paramecio.modules.welcome']", "modules=['paramecio.modules.welcome', 'paramecio.modules.admin', 'paramecio.modules.lang']")
|
||||||
|
|
||||||
with open(path_settings+'/config.py', 'w') as f:
|
with open(path_settings+'/config.py', 'w') as f:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,11 @@ class UserModel(WebModel):
|
||||||
self.yes_repeat_password=True
|
self.yes_repeat_password=True
|
||||||
self.check_user=True
|
self.check_user=True
|
||||||
|
|
||||||
def create_forms(self, arr_fields={}):
|
def create_forms(self, arr_fields=[]):
|
||||||
|
|
||||||
# Add password_repeat to forms from the model
|
# Add password_repeat to forms from the model
|
||||||
|
|
||||||
super().create_forms(arr_fields)
|
arr_fields=super().create_forms(arr_fields)
|
||||||
|
|
||||||
if self.password_field in arr_fields and self.yes_repeat_password:
|
if self.password_field in arr_fields and self.yes_repeat_password:
|
||||||
|
|
||||||
|
|
@ -34,6 +34,9 @@ class UserModel(WebModel):
|
||||||
repeat_password.field=self.fields['password']
|
repeat_password.field=self.fields['password']
|
||||||
|
|
||||||
self.create_form_after(self.password_field, repeat_password)
|
self.create_form_after(self.password_field, repeat_password)
|
||||||
|
|
||||||
|
return arr_fields
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def insert(self, dict_values, external_agent=True):
|
def insert(self, dict_values, external_agent=True):
|
||||||
|
|
||||||
|
|
@ -50,12 +53,14 @@ class UserModel(WebModel):
|
||||||
|
|
||||||
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
||||||
|
|
||||||
|
error=0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
fields, values, update_values=super().check_all_fields(dict_values, external_agent, yes_update, errors_set)
|
fields, values, update_values=super().check_all_fields(dict_values, external_agent, yes_update, errors_set)
|
||||||
except:
|
except:
|
||||||
|
|
||||||
return False
|
error+=1
|
||||||
|
|
||||||
if self.check_user==True:
|
if self.check_user==True:
|
||||||
|
|
||||||
|
|
@ -65,14 +70,14 @@ class UserModel(WebModel):
|
||||||
|
|
||||||
dict_values['repeat_password']=dict_values.get('repeat_password', '')
|
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()!="":
|
||||||
|
|
||||||
if dict_values[self.password_field].strip()!="":
|
if dict_values['repeat_password']!=dict_values[self.password_field]:
|
||||||
|
|
||||||
self.fields[self.password_field].error=True
|
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')
|
self.fields[self.password_field].txt_error=I18n.lang('common', 'error_passwords_no_match', 'Error: passwords doesn\'t match')
|
||||||
|
|
||||||
return False
|
error+=1
|
||||||
|
|
||||||
# Check if exists user with same email or password
|
# Check if exists user with same email or password
|
||||||
|
|
||||||
|
|
@ -129,10 +134,13 @@ class UserModel(WebModel):
|
||||||
self.fields[self.username_field].error=True
|
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')
|
self.fields[self.username_field].txt_error=I18n.lang('common', 'error_username_or_password_exists', 'Error: username or email exists in database')
|
||||||
|
|
||||||
return False
|
error+=1
|
||||||
|
|
||||||
self.conditions=original_conditions
|
self.conditions=original_conditions
|
||||||
|
|
||||||
|
if error>0:
|
||||||
|
return False
|
||||||
|
|
||||||
return fields, values, update_values
|
return fields, values, update_values
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -734,7 +734,7 @@ class WebModel:
|
||||||
|
|
||||||
#Create a form based in table.
|
#Create a form based in table.
|
||||||
|
|
||||||
def create_forms(self, arr_fields={}):
|
def create_forms(self, arr_fields=[]):
|
||||||
|
|
||||||
if len(arr_fields)==0:
|
if len(arr_fields)==0:
|
||||||
arr_fields=self.fields.keys()
|
arr_fields=self.fields.keys()
|
||||||
|
|
@ -744,6 +744,8 @@ class WebModel:
|
||||||
self.valid_fields.append(name_field)
|
self.valid_fields.append(name_field)
|
||||||
self.forms[name_field]=self.fields[name_field].create_form()
|
self.forms[name_field]=self.fields[name_field].create_form()
|
||||||
|
|
||||||
|
return arr_fields
|
||||||
|
|
||||||
def create_form_after(self, form_after, new_form):
|
def create_form_after(self, form_after, new_form):
|
||||||
|
|
||||||
new_dict=OrderedDict()
|
new_dict=OrderedDict()
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ def admin(t):
|
||||||
|
|
||||||
admin.list.search_fields=['username']
|
admin.list.search_fields=['username']
|
||||||
|
|
||||||
admin.arr_fields_edit=['username', 'password', 'email', 'privileges']
|
admin.arr_fields_edit=['username', 'password', 'repeat_password', 'email', 'privileges']
|
||||||
|
|
||||||
#admin.list.limit_pages=5
|
#admin.list.limit_pages=5
|
||||||
|
|
||||||
|
|
|
||||||
16
tests/datetimetest.py
Normal file
16
tests/datetimetest.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
from settings import config
|
||||||
|
from paramecio.citoplasma import datetime
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
class TestFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_timenow(self):
|
||||||
|
|
||||||
|
time=datetime.normalize_time(2012, 12, 21, 00, 24, 21)
|
||||||
|
|
||||||
|
self.assertEqual(time, '20121221002421')
|
||||||
|
|
||||||
|
value=datetime.format_tztime(time)
|
||||||
|
|
||||||
|
self.assertEqual(value, '21/12/2012 00:24:21')
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue