mirror of
https://github.com/dkmstr/openuds.git
synced 2025-02-02 09:47:13 +03:00
Added fld4 to stats events. Added validators to providers
This commit is contained in:
parent
3feb502fec
commit
4a2401b622
@ -164,6 +164,7 @@ def __registerUser(authenticator, authInstance, username):
|
||||
if usr is not None and State.isActive(usr.state):
|
||||
# Now we update database groups for this user
|
||||
usr.getManager().recreateGroups(usr)
|
||||
# And add an login event
|
||||
events.addEvent(authenticator, events.ET_LOGIN, username=username, srcip=getRequest().ip) # pylint: disable=maybe-no-member
|
||||
return usr
|
||||
|
||||
|
@ -32,6 +32,9 @@
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.translation import get_language
|
||||
from django.utils import formats
|
||||
|
||||
import re
|
||||
import logging
|
||||
|
||||
@ -40,21 +43,16 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def parseDate(dateToParse):
|
||||
import datetime
|
||||
from django.utils.translation import get_language
|
||||
from django.utils import formats
|
||||
|
||||
if get_language() == 'fr':
|
||||
date_format = '%d/%m/%Y'
|
||||
else:
|
||||
date_format = formats.get_format('SHORT_DATE_FORMAT').replace('Y', '%Y').replace('m', '%m').replace('d', '%d')
|
||||
date_format = formats.get_format('SHORT_DATE_FORMAT').replace('Y', '%Y').replace('m', '%m').replace('d', '%d') # pylint: disable=maybe-no-member
|
||||
|
||||
return datetime.datetime.strptime(dateToParse, date_format).date()
|
||||
|
||||
|
||||
def dateToLiteral(date):
|
||||
from django.utils.translation import get_language
|
||||
from django.utils import formats
|
||||
|
||||
# Fix for FR lang for datepicker
|
||||
if get_language() == 'fr':
|
||||
date = date.strftime('%d/%m/%Y')
|
||||
@ -78,16 +76,16 @@ def extractKey(dictionary, key, **kwargs):
|
||||
|
||||
# Regular expressions for User Agents
|
||||
# These both are for Internet Explorer
|
||||
_msie = re.compile('MSIE ([0-9]+)\.([0-9]+)')
|
||||
_trident = re.compile('Trident/.*rv:([0-9]+)\.([0-9]+)')
|
||||
_msie = re.compile(r'MSIE ([0-9]+)\.([0-9]+)')
|
||||
_trident = re.compile(r'Trident/.*rv:([0-9]+)\.([0-9]+)')
|
||||
# Opera
|
||||
_opera = re.compile('OPR/([0-9]+)\.([0-9]+)')
|
||||
_opera = re.compile(r'OPR/([0-9]+)\.([0-9]+)')
|
||||
# Firefox
|
||||
_firefox = re.compile('Firefox/([0-9]+)\.([0-9]+)')
|
||||
_firefox = re.compile(r'Firefox/([0-9]+)\.([0-9]+)')
|
||||
# Chrome
|
||||
_chrome = re.compile('Chrome/([0-9]+)\.([0-9]+)')
|
||||
_chrome = re.compile(r'Chrome/([0-9]+)\.([0-9]+)')
|
||||
# Webkit in general
|
||||
_webkit = re.compile('AppleWebKit/([0-9]+)\.([0-9]+)')
|
||||
_webkit = re.compile(r'AppleWebKit/([0-9]+)\.([0-9]+)')
|
||||
|
||||
_browsers = {
|
||||
'ie': [_trident, _msie],
|
||||
|
116
server/src/uds/core/util/validators.py
Normal file
116
server/src/uds/core/util/validators.py
Normal file
@ -0,0 +1,116 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2014 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
|
||||
# may be used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
'''
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from uds.core.BaseModule import Module
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
import re
|
||||
import logging
|
||||
import six
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def validateNumeric(numericStr, minValue=None, maxValue=None, returnAsInteger=True, fieldName=None):
|
||||
'''
|
||||
Validates that a numeric value is valid
|
||||
:param numericStr: Numeric value to check (as string)
|
||||
:param minValue: If not None, min value that must be the numeric or exception is thrown
|
||||
:param maxValue: If not None, max value that must be the numeric or exception is thrown
|
||||
:param returnAsInteger: if True, returs value as integer (default), else returns as string
|
||||
:param fieldName: If present, the name of the field for "Raising" exceptions, defaults to "Numeric value"
|
||||
:return: Raises Module.Validation exception if is invalid, else return the value "fixed"
|
||||
'''
|
||||
numericStr = numericStr.replace(' ', '')
|
||||
fieldName = fieldName if fieldName is not None else _('Numeric')
|
||||
|
||||
try:
|
||||
numeric = int(numericStr)
|
||||
if minValue is not None and numeric < minValue:
|
||||
raise Module.ValidationException(_('{0} must be greater than or equal to {1}'.format(fieldName, minValue)))
|
||||
|
||||
if maxValue is not None and numeric > maxValue:
|
||||
raise Module.ValidationException(_('{0} must be lower than or equal to {1}'.format(fieldName, maxValue)))
|
||||
|
||||
numericStr = six.u(str(numeric))
|
||||
|
||||
except ValueError:
|
||||
raise Module.ValidationException(_('{0} contains invalid characters').format(fieldName))
|
||||
|
||||
if returnAsInteger:
|
||||
return int(numericStr)
|
||||
|
||||
return numericStr
|
||||
|
||||
|
||||
def validatePort(portStr, returnAsInteger=True):
|
||||
'''
|
||||
Validates that a port number is valid
|
||||
:param portStr: port to validate, as string
|
||||
:param returnAsInteger: if True, returns value as integer, if not, as string
|
||||
:return: Raises Module.Validation exception if is invalid, else return the value "fixed"
|
||||
'''
|
||||
return validateNumeric(portStr, minValue=0, maxValue=65535, returnAsInteger=returnAsInteger, fieldName='Port')
|
||||
|
||||
|
||||
def validateTimeout(timeOutStr, returnAsInteger=True):
|
||||
'''
|
||||
Validates that a timeout value is valid
|
||||
:param timeOutStr: timeout to validate
|
||||
:param returnAsInteger: if True, returns value as integer, if not, as string
|
||||
:return: Raises Module.Validation exception if is invalid, else return the value "fixed"
|
||||
'''
|
||||
return validateNumeric(timeOutStr, minValue=0, returnAsInteger=returnAsInteger, fieldName='Timeout')
|
||||
|
||||
|
||||
def validateMacRange(macRange):
|
||||
'''
|
||||
Corrects mac range (uppercase, without spaces), and checks that is range is valid
|
||||
:param macRange: Range to fix
|
||||
:return: Raises Module.Validation exception if is invalid, else return the value "fixed"
|
||||
'''
|
||||
# Removes white spaces and all to uppercase
|
||||
macRange = macRange.upper().replace(' ', '')
|
||||
|
||||
macRE = re.compile(r'^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$') # In fact, it could be XX-XX-XX-XX-XX-XX, but we use - as range separator
|
||||
|
||||
try:
|
||||
macRangeStart, macRangeEnd = macRange.split('-')
|
||||
|
||||
if macRE.match(macRangeStart) is None or macRE.match(macRangeEnd) is None:
|
||||
raise Exception()
|
||||
except Exception:
|
||||
raise Module.ValidationException(_('Invalid mac range. Mac range must be in format XX:XX:XX:XX:XX:XX-XX:XX:XX:XX:XX:XX'))
|
||||
|
||||
return macRange
|
20
server/src/uds/migrations/0013_statsevents_fld4.py
Normal file
20
server/src/uds/migrations/0013_statsevents_fld4.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uds', '0012_auto_20141221_1210'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='statsevents',
|
||||
name='fld4',
|
||||
field=models.CharField(default='', max_length=128),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
@ -33,11 +33,12 @@
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
__updated__ = '2014-11-26'
|
||||
__updated__ = '2015-01-22'
|
||||
|
||||
from datetime import datetime
|
||||
from django.db import connection
|
||||
from time import mktime
|
||||
import re
|
||||
|
||||
|
||||
import logging
|
||||
|
@ -124,13 +124,12 @@ class OVirtLinkedService(Service):
|
||||
tooltip=_('Length of numeric part for the names of this machines (betwen 3 and 6'), required=True)
|
||||
|
||||
display = gui.ChoiceField(label=_('Display'), rdonly=False, order=8,
|
||||
tooltip=_('Display type (only for administration pourposses)'),
|
||||
values=[
|
||||
gui.choiceItem('spice', 'Spice'),
|
||||
gui.choiceItem('vnc', 'Vnc')
|
||||
],
|
||||
defvalue='1' # Default value is the ID of the choicefield
|
||||
)
|
||||
tooltip=_('Display type (only for administration pourposses)'),
|
||||
values=[gui.choiceItem('spice', 'Spice'),
|
||||
gui.choiceItem('vnc', 'Vnc')
|
||||
],
|
||||
defvalue='1' # Default value is the ID of the choicefield
|
||||
)
|
||||
|
||||
ov = gui.HiddenField()
|
||||
ev = gui.HiddenField() # We need to keep the env so we can instantiate the Provider
|
||||
|
@ -39,6 +39,7 @@ from uds.core.util.State import State
|
||||
from uds.core.services import ServiceProvider
|
||||
from OVirtLinkedService import OVirtLinkedService
|
||||
from uds.core.ui import gui
|
||||
from uds.core.util import validators
|
||||
|
||||
from client import oVirtClient
|
||||
|
||||
@ -118,6 +119,11 @@ class Provider(ServiceProvider):
|
||||
# Just reset _api connection variable
|
||||
self._api = None
|
||||
|
||||
if values is not None:
|
||||
self.macsRange.value = validators.validateMacRange(self.macsRange.value)
|
||||
self.timeout.value = validators.validateTimeout(self.timeout.value, returnAsInteger=False)
|
||||
logger.debug(self.host.value)
|
||||
|
||||
def testConnection(self):
|
||||
'''
|
||||
Test that conection to oVirt server is fine
|
||||
|
@ -37,6 +37,7 @@ from django.utils.translation import ugettext_noop as _
|
||||
from uds.core.util.State import State
|
||||
from uds.core.services import ServiceProvider
|
||||
from uds.core.ui import gui
|
||||
from uds.core.util import validators
|
||||
|
||||
from xen_client import XenServer
|
||||
from xen_client import XenFailure, XenFault
|
||||
@ -47,7 +48,7 @@ import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
__updated__ = '2014-12-07'
|
||||
__updated__ = '2015-01-22'
|
||||
|
||||
|
||||
CACHE_TIME_FOR_SERVER = 1800
|
||||
|
Loading…
x
Reference in New Issue
Block a user