Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PyOTRS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coopdevs
Som Connexió
OTRS
PyOTRS
Commits
406d3505
Commit
406d3505
authored
8 years ago
by
Robert Habermann
Browse files
Options
Downloads
Patches
Plain Diff
refactor DynamicField
parent
a5393391
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pyotrs/lib.py
+68
-49
68 additions, 49 deletions
pyotrs/lib.py
tests/test_dynamic_field.py
+9
-8
9 additions, 8 deletions
tests/test_dynamic_field.py
with
77 additions
and
57 deletions
pyotrs/lib.py
+
68
−
49
View file @
406d3505
...
@@ -190,27 +190,33 @@ class Attachment(object):
...
@@ -190,27 +190,33 @@ class Attachment(object):
class
DynamicField
(
object
):
class
DynamicField
(
object
):
"""
PyOTRS DynamicField class
"""
PyOTRS DynamicField class
Args:
name (unicode): Name of OTRS DynamicField
value (unicode): Value of OTRS DynamicField
.. warning::
.. warning::
DynamicField representation changed between OTRS 4 and OTRS 5.
DynamicField representation changed between OTRS 4 and OTRS 5.
**PyOTRS only supports OTRS 5 style!**
**PyOTRS only supports OTRS 5 style!**
"""
"""
def
__init__
(
self
,
name
=
None
,
value
=
None
,
dct
=
None
):
def
__init__
(
self
,
name
,
value
):
if
dct
:
self
.
name
=
name
if
name
or
value
:
self
.
value
=
value
raise
Exception
(
"
Please provide either
\"
dct
\"
or
\"
name and value
\"
"
)
self
.
name
=
dct
[
"
Name
"
]
self
.
value
=
dct
[
"
Value
"
]
else
:
if
name
and
value
:
self
.
name
=
name
self
.
value
=
value
else
:
raise
Exception
(
"
Please provide either
\"
dct
\"
or
\"
name and value
\"
"
)
def
__repr__
(
self
):
def
__repr__
(
self
):
return
"
<{0}: {1} --> {2} >
"
.
format
(
self
.
__class__
.
__name__
,
self
.
name
,
self
.
value
)
return
"
<{0}: {1} --> {2} >
"
.
format
(
self
.
__class__
.
__name__
,
self
.
name
,
self
.
value
)
@classmethod
def
from_dct
(
cls
,
dct
):
"""
create DynamicField from dct
Args:
dct (dict):
Returns:
DynamicField
"""
return
cls
(
name
=
dct
[
"
Name
"
],
value
=
dct
[
"
Value
"
])
def
to_dct
(
self
):
def
to_dct
(
self
):
"""
represent DynamicField as dict
"""
represent DynamicField as dict
...
@@ -234,7 +240,7 @@ class DynamicField(object):
...
@@ -234,7 +240,7 @@ class DynamicField(object):
Returns:
Returns:
DynamicField
DynamicField
"""
"""
return
DynamicField
(
dct
=
{
'
Name
'
:
'
lastname
'
,
'
Value
'
:
'
Doe
'
})
return
DynamicField
.
from_
dct
(
{
'
Name
'
:
'
lastname
'
,
'
Value
'
:
'
Doe
'
})
class
Ticket
(
object
):
class
Ticket
(
object
):
...
@@ -595,10 +601,10 @@ class Client(object):
...
@@ -595,10 +601,10 @@ class Client(object):
SessionError if neither self.session_id nor session_id is not set
SessionError if neither self.session_id nor session_id is not set
Returns:
Returns:
bool: True if valid,
False
otherwise
.
bool:
**
True
**
if valid, otherwise
**False**
.. note::
.. note::
Calls _ticket_get_json (GET)
**
Calls _ticket_get_json (GET)
**
"""
"""
if
not
session_id
and
not
self
.
session_id_store
.
value
:
if
not
session_id
and
not
self
.
session_id_store
.
value
:
raise
SessionError
(
"
No value set for session_id!
"
)
raise
SessionError
(
"
No value set for session_id!
"
)
...
@@ -618,7 +624,7 @@ class Client(object):
...
@@ -618,7 +624,7 @@ class Client(object):
"""
create new OTRS Session and store Session ID
"""
create new OTRS Session and store Session ID
Returns:
Returns:
bool: True if successful,
False
otherwise
.
bool:
**
True
**
if successful, otherwise
**False**
.. note::
.. note::
Uses HTTP Method: POST
Uses HTTP Method: POST
...
@@ -649,7 +655,7 @@ class Client(object):
...
@@ -649,7 +655,7 @@ class Client(object):
SessionIDFileError
SessionIDFileError
Returns:
Returns:
bool: True if successful,
False
otherwise
.
bool:
**
True
**
if successful, otherwise
**False**
"""
"""
# try to read session_id from file
# try to read session_id from file
self
.
session_id_store
.
value
=
self
.
session_id_store
.
read
()
self
.
session_id_store
.
value
=
self
.
session_id_store
.
read
()
...
@@ -698,8 +704,8 @@ class Client(object):
...
@@ -698,8 +704,8 @@ class Client(object):
dynamic_field_list (list): *DynamicField* object
dynamic_field_list (list): *DynamicField* object
**kwargs: any regular OTRS Fields (not for Dynamic Fields!)
**kwargs: any regular OTRS Fields (not for Dynamic Fields!)
.. todo:
:
Returns
:
2016-04-18 (RH): decide what ticket_create should return (bool or result content)
bool: **True** if successful, otherwise **False**
"""
"""
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
"
{0.webservicename}/Ticket
"
.
format
(
self
))
"
{0.webservicename}/Ticket
"
.
format
(
self
))
...
@@ -740,7 +746,7 @@ class Client(object):
...
@@ -740,7 +746,7 @@ class Client(object):
ResponseJSONParseError
ResponseJSONParseError
Returns:
Returns:
bool: True if successful,
False
otherwise
.
bool:
**
True
**
if successful, otherwise
**False**
"""
"""
self
.
operation
=
"
TicketCreate
"
self
.
operation
=
"
TicketCreate
"
http_method
=
"
POST
"
http_method
=
"
POST
"
...
@@ -755,6 +761,7 @@ class Client(object):
...
@@ -755,6 +761,7 @@ class Client(object):
GenericInterface::Operation::Ticket::TicketGet
GenericInterface::Operation::Ticket::TicketGet
Methods (public):
Methods (public):
* ticket_get_by_id
* ticket_get_by_id
* ticket_get_by_list
* ticket_get_by_number
* ticket_get_by_number
"""
"""
...
@@ -769,22 +776,46 @@ class Client(object):
...
@@ -769,22 +776,46 @@ class Client(object):
Articles (+default: False*)
Articles (+default: False*)
Returns:
Returns:
dict: Ticket
bool: **True** if successful, otherwise **False**
.. todo::
2016-04-18 (RH): decide what ticket_get_by_id should return (bool or result content)
"""
"""
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
))
"
{0.webservicename}/Ticket
"
.
format
(
self
))
payload
=
{
"
SessionID
"
:
self
.
session_id_store
.
value
,
"
TicketID
"
:
"
{}
"
.
format
(
ticket_id
),
"
AllArticles
"
:
int
(
all_articles
),
"
DynamicFields
"
:
int
(
dynamic_fields
)
}
return
self
.
_ticket_get_json
(
url
,
payload
)
def
ticket_get_by_list_of_ids
(
self
,
ticket_id_list
,
dynamic_fields
=
True
,
all_articles
=
False
):
"""
ticket_get_by_list
Args:
ticket_id_list (list): List of Integer value
dynamic_fields (bool): will request OTRS to include all
Dynamic Fields (*default: True*)
all_articles (bool): will request OTRS to include all
Articles (*default: False*)
Returns:
bool: **True** if successful, otherwise **False**
"""
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
"
{0.webservicename}/Ticket
"
.
format
(
self
))
payload
=
{
payload
=
{
"
SessionID
"
:
self
.
session_id_store
.
value
,
"
SessionID
"
:
self
.
session_id_store
.
value
,
"
TicketID
"
:
'
,
'
.
join
([
str
(
item
)
for
item
in
ticket_id_list
]),
"
AllArticles
"
:
int
(
all_articles
),
"
AllArticles
"
:
int
(
all_articles
),
"
DynamicFields
"
:
int
(
dynamic_fields
)
"
DynamicFields
"
:
int
(
dynamic_fields
)
}
}
return
self
.
_ticket_get_json
(
url
,
payload
)
return
self
.
_ticket_get_json
(
url
,
payload
)
# return self.response_json['Ticket'][0]
def
ticket_get_by_number
(
self
,
ticket_number
,
dynamic_fields
=
True
,
all_articles
=
False
):
def
ticket_get_by_number
(
self
,
ticket_number
,
dynamic_fields
=
True
,
all_articles
=
False
):
"""
ticket_get_by_number
"""
ticket_get_by_number
...
@@ -797,7 +828,7 @@ class Client(object):
...
@@ -797,7 +828,7 @@ class Client(object):
Articles (*default: False*)
Articles (*default: False*)
Returns:
Returns:
dict: Ticket
bool: **True** if successful, otherwise **False**
"""
"""
if
isinstance
(
ticket_number
,
int
):
if
isinstance
(
ticket_number
,
int
):
raise
TicketError
(
"
Provide ticket_number as str/unicode. Got ticket_number as int.
"
)
raise
TicketError
(
"
Provide ticket_number as str/unicode. Got ticket_number as int.
"
)
...
@@ -827,7 +858,7 @@ class Client(object):
...
@@ -827,7 +858,7 @@ class Client(object):
ResponseJSONParseError
ResponseJSONParseError
Returns:
Returns:
bool: True if successful,
False
otherwise
.
bool:
**
True
**
if successful, otherwise
**False**
.. note::
.. note::
Uses HTTP Method: GET
Uses HTTP Method: GET
...
@@ -855,11 +886,14 @@ class Client(object):
...
@@ -855,11 +886,14 @@ class Client(object):
**kwargs: Arbitrary keyword arguments (Dynamic Field).
**kwargs: Arbitrary keyword arguments (Dynamic Field).
Returns:
Returns:
list: tickets that were found as list of **str
**
bool: **True** if successful, otherwise **False
**
.. note::
.. note::
If value of kwargs is a datetime object then this object will be
If value of kwargs is a datetime object then this object will be
converted to the appropriate string format for REST API.
converted to the appropriate string format for REST API.
.. note::
tickets that were found as list of **str** stored self.ticket_search_result_list
"""
"""
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
"
{0.webservicename}/Ticket
"
.
format
(
self
))
"
{0.webservicename}/Ticket
"
.
format
(
self
))
...
@@ -878,7 +912,6 @@ class Client(object):
...
@@ -878,7 +912,6 @@ class Client(object):
raise
TicketSearchNothingToLookFor
(
"
Nothing specified to look for!
"
)
raise
TicketSearchNothingToLookFor
(
"
Nothing specified to look for!
"
)
return
self
.
_ticket_search_json
(
url
,
payload
)
return
self
.
_ticket_search_json
(
url
,
payload
)
# return self.ticket_search_result_list
def
ticket_search_full_text
(
self
,
pattern
):
def
ticket_search_full_text
(
self
,
pattern
):
"""
Wrapper for search ticket for full text search
"""
Wrapper for search ticket for full text search
...
@@ -893,12 +926,6 @@ class Client(object):
...
@@ -893,12 +926,6 @@ class Client(object):
Following examples is a dummy doctest. In order for this to work on Python2 the
Following examples is a dummy doctest. In order for this to work on Python2 the
whole docstring needs to be a (Python2) unicode string (prefixed u)
whole docstring needs to be a (Python2) unicode string (prefixed u)
Examples:
>>>
2
+
2
4
>>>
str
(
'
a
'
)
+
str
(
'
b
'
)
'
ab
'
.. warning::
.. warning::
Full Text is not really working yet.
Full Text is not really working yet.
...
@@ -926,7 +953,7 @@ class Client(object):
...
@@ -926,7 +953,7 @@ class Client(object):
ResponseJSONParseError
ResponseJSONParseError
Returns:
Returns:
bool: True if
search
successful,
False
otherwise
.
bool:
**
True
**
if successful, otherwise
**False**
.. note::
.. note::
Uses HTTP Method: GET
Uses HTTP Method: GET
...
@@ -963,10 +990,7 @@ class Client(object):
...
@@ -963,10 +990,7 @@ class Client(object):
**kwargs: any regular OTRS Fields (not for Dynamic Fields!)
**kwargs: any regular OTRS Fields (not for Dynamic Fields!)
Returns:
Returns:
dict: Response from OTRS API if successful (if Article was added new Art. ID included)
bool: **True** if successful, otherwise **False**
.. todo::
2016-04-17 (RH): decide what ticket_update should return (bool or result content)
"""
"""
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
))
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
))
...
@@ -988,7 +1012,6 @@ class Client(object):
...
@@ -988,7 +1012,6 @@ class Client(object):
payload
.
update
({
"
DynamicField
"
:
[
df
.
to_dct
()
for
df
in
dynamic_field_list
]})
payload
.
update
({
"
DynamicField
"
:
[
df
.
to_dct
()
for
df
in
dynamic_field_list
]})
return
self
.
_ticket_update_json
(
url
,
payload
)
return
self
.
_ticket_update_json
(
url
,
payload
)
# return self.response_json
def
ticket_update_set_pending
(
self
,
def
ticket_update_set_pending
(
self
,
ticket_id
,
ticket_id
,
...
@@ -1004,10 +1027,7 @@ class Client(object):
...
@@ -1004,10 +1027,7 @@ class Client(object):
pending_hours (int): defaults to 0
pending_hours (int): defaults to 0
Returns:
Returns:
bool: True if update successful, False otherwise.
bool: **True** if successful, otherwise **False**
.. todo::
2016-04-17 (RH): decide what ticket_update_set_pending should return
"""
"""
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
url
=
(
"
{0.baseurl}/otrs/nph-genericinterface.pl/Webservice/
"
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
))
"
{0.webservicename}/Ticket/{1}
"
.
format
(
self
,
ticket_id
))
...
@@ -1024,7 +1044,6 @@ class Client(object):
...
@@ -1024,7 +1044,6 @@ class Client(object):
}
}
return
self
.
_ticket_update_json
(
url
,
payload
)
return
self
.
_ticket_update_json
(
url
,
payload
)
# return "Set pending till {0} with state \"{1}\"".format(pending_till_str, new_state)
def
_ticket_update_json
(
self
,
url
,
payload
):
def
_ticket_update_json
(
self
,
url
,
payload
):
"""
_ticket_update_json
"""
_ticket_update_json
...
@@ -1037,7 +1056,7 @@ class Client(object):
...
@@ -1037,7 +1056,7 @@ class Client(object):
OTRSAPIError
OTRSAPIError
Returns:
Returns:
bool: True if
update
successful,
False
otherwise
.
bool:
**
True
**
if successful, otherwise
**False**
.. note::
.. note::
Uses HTTP Method: PATCH
Uses HTTP Method: PATCH
...
@@ -1119,7 +1138,7 @@ class Client(object):
...
@@ -1119,7 +1138,7 @@ class Client(object):
OTRSAPIError
OTRSAPIError
Returns:
Returns:
bool
True
bool
: **True** if successful
"""
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_dynamic_field.py
+
9
−
8
View file @
406d3505
...
@@ -22,16 +22,17 @@ from pyotrs import DynamicField # noqa
...
@@ -22,16 +22,17 @@ from pyotrs import DynamicField # noqa
class
DynamicFieldTests
(
unittest
.
TestCase
):
class
DynamicFieldTests
(
unittest
.
TestCase
):
def
test_init_blank
(
self
):
def
test_init_blank
(
self
):
self
.
assertRaisesRegex
(
Exception
,
"
Please provide either
.*
"
,
DynamicField
)
self
.
assertRaisesRegex
(
TypeError
,
"
.*takes exactly 3 arguments
.*
"
,
DynamicField
)
def
test_init_name_only
(
self
):
def
test_init_name_only
(
self
):
self
.
assertRaisesRegex
(
Exception
,
"
Please provide either.*
"
,
DynamicField
,
name
=
"
foo
"
)
self
.
assertRaisesRegex
(
TypeError
,
"
.*takes exactly 3 arguments.*
"
,
DynamicField
,
name
=
"
foo
"
)
def
test_init_value_only
(
self
):
def
test_init_value_only
(
self
):
self
.
assertRaisesRegex
(
Exception
,
"
Please provide either.*
"
,
DynamicField
,
value
=
"
bar
"
)
self
.
assertRaisesRegex
(
TypeError
,
"
.*takes exactly 3 arguments.*
"
,
def
test_init_dict_and_name
(
self
):
DynamicField
,
value
=
"
bar
"
)
self
.
assertRaisesRegex
(
Exception
,
"
Please prov.*
"
,
DynamicField
,
dct
=
{
'
f
'
:
'
b
'
},
value
=
"
c
"
)
def
test_dummy1_static
(
self
):
def
test_dummy1_static
(
self
):
dyn1
=
DynamicField
(
name
=
"
firstname
"
,
value
=
"
Jane
"
)
dyn1
=
DynamicField
(
name
=
"
firstname
"
,
value
=
"
Jane
"
)
...
@@ -39,12 +40,12 @@ class DynamicFieldTests(unittest.TestCase):
...
@@ -39,12 +40,12 @@ class DynamicFieldTests(unittest.TestCase):
self
.
assertEqual
(
dyn1
.
__repr__
(),
'
<DynamicField: firstname --> Jane >
'
)
self
.
assertEqual
(
dyn1
.
__repr__
(),
'
<DynamicField: firstname --> Jane >
'
)
def
test_dummy2_static
(
self
):
def
test_dummy2_static
(
self
):
dyn2
=
DynamicField
(
dct
=
{
'
Name
'
:
'
lastname
'
,
'
Value
'
:
'
Doe
'
})
dyn2
=
DynamicField
.
from_
dct
(
{
'
Name
'
:
'
lastname
'
,
'
Value
'
:
'
Doe
'
})
self
.
assertIsInstance
(
dyn2
,
DynamicField
)
self
.
assertIsInstance
(
dyn2
,
DynamicField
)
self
.
assertEqual
(
dyn2
.
__repr__
(),
'
<DynamicField: lastname --> Doe >
'
)
self
.
assertEqual
(
dyn2
.
__repr__
(),
'
<DynamicField: lastname --> Doe >
'
)
def
test_dummy1_static_to_dict
(
self
):
def
test_dummy1_static_to_dict
(
self
):
dyn1
=
DynamicField
(
name
=
"
firstname
"
,
value
=
"
Jane
"
)
dyn1
=
DynamicField
(
"
firstname
"
,
"
Jane
"
)
self
.
assertIsInstance
(
dyn1
,
DynamicField
)
self
.
assertIsInstance
(
dyn1
,
DynamicField
)
self
.
assertDictEqual
(
dyn1
.
to_dct
(),
{
'
Name
'
:
'
firstname
'
,
'
Value
'
:
'
Jane
'
})
self
.
assertDictEqual
(
dyn1
.
to_dct
(),
{
'
Name
'
:
'
firstname
'
,
'
Value
'
:
'
Jane
'
})
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment