def post(self, request, *args, **kwargs): try: Log.debug('Create momend request: '+str(request.POST)) _owner = request.user if 'owner' in request.POST: if request.user.is_superuser: _owner = User.objects.get(pk=request.POST['owner']) _momend_name = request.POST['momend_name'] _privacy = request.POST['privacy_type'] _theme = request.POST['momend_theme'] _theme = 1 # TODO remove after showing theme selection combo _send_mail = request.POST.get('mail', True) # Send mail after create, default True dm = DataManager(_owner) try: _args = dict() if 'selected' in request.POST and request.POST['selected']: _args['is_date'] = False _args['selected'] = json.loads(request.POST['selected']) else: _args['is_date'] = True _args['selected'] = False _args['since'] = datetime.strptime(request.POST['start_date'], '%d %b, %Y').replace(tzinfo=pytz.UTC) _args['until'] = datetime.strptime(request.POST['finish_date'], '%d %b, %Y').replace(tzinfo=pytz.UTC) _create_params = request.POST.keys() for _param in _create_params: if '-active' in _param: # Provider disable requests _args[_param] = request.POST[_param] if 'friends' in request.POST and len(request.POST['friends']) > 0: _args['friends'] = request.POST['friends'].split(',') else: _args['friends'] = False if 'chronological' in request.POST: _args['chronological'] = request.POST['chronological'] else: _args['chronological'] = False _cryptic_id = dm.create_momend(name=_momend_name, duration=60, privacy=_privacy, theme=Theme.objects.get(pk=_theme), send_mail=_send_mail, **_args) if _cryptic_id: # If created momend successfully return _generate_json_response(True, 'Create momend request received', cid=_cryptic_id) _error_msg = dm.get_last_status() return _generate_json_response(False, 'Create momend failed with error message: '+str(_error_msg), _error_msg) except Exception as e: _log_critical_error('Exception while creating the momend', True, request.POST, request.user, traceback.format_exc()) return _generate_json_response(False, 'Error while creating momend: '+str(e), 'An error occurred. Please try again') # Could be any error except KeyError as e: Log.error(traceback.format_exc()) return _generate_json_response(False, 'Error while creating momend: '+str(e), str(e)) # One of the parameters is missing except Exception as e: _log_critical_error('Impossible exception occurred while creating momend', True, request.POST, request.user, traceback.format_exc()) return _generate_json_response(False, 'Error while creating momend (impossible): '+str(e), 'An error occurred. Please try again after a while')
""" This file demonstrates writing tests using the unittest module. These will pass when you run "manage.py test". Replace this with more appropriate tests for your application. """ from django.test import TestCase from django.contrib.auth.models import User from DataManager.DataManager import DataManager from DataManager.models import Provider, RawData from DataManager.DataEnrich.DataEnrichManager import DataEnrichManager from datetime import datetime ert = User.objects.get(username='******') dm = DataManager(ert) start = datetime.strptime('2012-01-01','%Y-%m-%d') end = datetime.strptime('2012-05-01','%Y-%m-%d') dm.create_momend(start,end,50)