def get_or_create_ownership_universes(working_as): universe_class = my_class_import('container.models.Universe') attributes_class = my_class_import('container.models.Attributes') third_class = my_class_import('container.models.ThirdPartyContainer') third_id = working_as['third_id'] third = third_class.objects.get(id=third_id) universe = universe_class.objects.filter(public=False, owner=third, short_name=third_id) if not universe.exists(): universe = universe_class() universe.owner = third universe.public = False universe.description = 'Technical universe that allows the management of the global permissions for user:'******'.' universe.name = 'Ownership - ' + third.name universe.short_name = third_id universe.type = attributes_class.objects.get(active=True, type='container_type', identifier='CONT_UNIVERSE') universe.inception_date = datetime.date.today() universe.closed_date = None universe.status = attributes_class.objects.get(active=True, type='status', identifier='STATUS_ACTIVE') universe.save() else: universe = universe[0] effective_third = get_effective_instance(third) all_sub_universes = [universe] if effective_third.type.identifier=='CONT_COMPANY': # TODO Control that there are no loops for subsidiary in effective_third.subsidiary.all(): sub_universe = get_or_create_ownership_universes({'third_id': subsidiary.company.id}) all_sub_universes += sub_universe for member in effective_third.members.all(): sub_universe = get_or_create_ownership_universes({'third_id': member.person.id}) all_sub_universes += sub_universe return all_sub_universes
def complete_fields_information(model_class, information, language_code='en'): all_fields = get_static_fields(model_class) attributes_class = classes.my_class_import('container.models.Attributes') for field in information: field_effective = field if '.' in field: fields_chain = field.split('.') field_effective = fields_chain[0] if all_fields.has_key(field_effective): information[field].update(all_fields[field_effective]) if information[field]['type'] in ['ForeignKey', 'ManyToManyField']: #current_class = classes.my_class_import(information[field]['target_class']) #if hasattr(current_class, 'get_fields'): # information[field]['options'] = getattr(current_class,'get_fields')() if information[field]['target_class']=='container.models.Attributes': information[field]['template'] = 'statics/' + information[field]['link']['type'] + '_' + language_code + '.html' information[field]['is_container'] = False elif information[field]['target_class']=='django.contrib.auth.models.User': # TODO Find a way to automatize information[field]['template'] = 'statics/application_users.html' information[field]['is_container'] = False else: information[field]['is_container'] = issubclass(classes.my_class_import(information[field]['target_class']), classes.my_class_import('container.models.Container')) information[field]['target_class'] if information[field]['is_container']: information[field]['container_type'] = attributes_class.objects.get(active=True, type='container_type_class', name=information[field]['target_class']).identifier.replace('_CLASS','') # if information[field]['type']!='ForeignKey': information[field]['template'] = 'statics/' + information[field]['fields'][information[field]['filter']]['link']['type'] + '_' + language_code + '.html' information[field]['template_m2m'] = 'statics/' + attributes_class.objects.get(type='element_wizard', name=information[field]['target_class'], active=True).short_name + '_' + language_code + '.html' information[field]['template_m2m_complete'] = 'statics/complete_' + attributes_class.objects.get(type='element_wizard', name=information[field]['target_class'], active=True).short_name + '_' + language_code + '.html' information[field]['datasource'] = '/container/filter.html?container_class=' + information[field]['target_class'] return information
def get_effective_instance(container): if container!=None: effective_class_name = classes.my_class_import('container.models.Attributes').objects.get(identifier=container.type.identifier + '_CLASS', active=True).name effective_class = classes.my_class_import(effective_class_name) effective_container = effective_class.objects.get(id=container.id) return effective_container else: return None
def render_many_to_many(request): # TODO: Check user profile = get_or_create_user_profile(request.user.id) container_id = clean_post_value(request.POST['container_id']) container_type = clean_post_value(request.POST['container_type']) container_class = container_type + '_CLASS' container_field = clean_post_value(request.POST['container_field']) rendition_witdh = clean_post_value(request.POST['rendition_width']) widget_index = clean_post_value(request.POST['widget_index']) widget_title = clean_post_value(request.POST['widget_title']) # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) foreign_class = effective_class._meta.get_field(container_field).rel.to container = effective_class.objects.get(id=container_id) context = {'base_template': profile['base_template'], 'profile': profile, 'title': widget_title, 'container_id': container_id, 'container_type': container_type, 'container_field': container_field, 'index':widget_index, 'data': getattr(container,container_field), 'fields': foreign_class.get_displayed_fields(rendition_witdh), 'application_settings': settings} return render(request, 'container/view/many_to_many_field.html', context)
def filters(request): profile = get_or_create_user_profile(request.user.id) if request.GET.has_key('container_type'): container_type = request.GET['container_type'] container_class = container_type + '_CLASS' effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name else: effective_class_name = request.GET['container_class'] searching = request.GET['term'] effective_class = classes.my_class_import(effective_class_name) if getattr(effective_class,'get_querying_class', None)!=None: effective_class = effective_class.get_querying_class() query_filter = None for field in effective_class.get_querying_fields(): query_dict = {} field = field.replace('.','__') + '__icontains' query_dict[field] = searching if query_filter==None: query_filter = Q(**query_dict) else: query_filter = query_filter | Q(**query_dict) results = effective_class.objects.filter(query_filter).distinct() # TODO Optimize results = [result for result in results if container_visible(result.id, profile)] results = dumps([dict_to_json_compliance(model_to_dict(item)) for item in results], default=json_util.default) return HttpResponse('{"result": ' + results + ', "status_message": "Found"}',"json")
def universe_details(request): if request.POST.has_key('universe_id'): universe_id = request.POST['universe_id'] else: universe_id = request.GET['universe_id'] # TODO: Check user user = User.objects.get(id=request.user.id) try: source = Universe.objects.get(Q(id=universe_id),Q(public=True)|Q(owner__id=request.user.id)) except: # TODO: Return error message return redirect('universes.html') context = {'universe': source, 'tracks': {}} for member in source.members.all(): if member.type.identifier not in ['CONT_COMPANY', 'CONT_BACKTEST', 'CONT_PORTFOLIO', 'CONT_COMPANY', 'CONT_OPERATION', 'CONT_PERSON', 'CONT_UNIVERSE']: effective_class_name = Attributes.objects.get(identifier=member.type.identifier + '_CLASS', active=True).name effective_class = classes.my_class_import(effective_class_name) member = effective_class.objects.get(id=member.id) content = get_main_track_content(member, True, True) if content!=None: context['tracks']['track_' + str(member.id)] = content else: context['tracks']['track_' + str(member.id)] = [] return render(request, 'universe_details.html', context)
def populate_attributes_from_xlsx(model_name, xlsx_file): model = classes.my_class_import(model_name) workbook = load_workbook(xlsx_file) sheet = workbook.get_sheet_by_name(name=model.__name__) row_index = 1 # Reading header header = [] for column_index in range(1, sheet.get_highest_column() + 1): value = sheet.cell(row=row_index, column=column_index).value if value != None: header.append(value if value != '' else header[-1]) else: break LOGGER.info('Using header:' + str(header)) row_index += 1 while row_index <= sheet.get_highest_row(): if model.objects.filter( identifier=sheet.cell(row=row_index, column=1).value).exists(): instance = model.objects.get( identifier=sheet.cell(row=row_index, column=1).value) else: instance = model() for i in range(0, len(header)): value = sheet.cell(row=row_index, column=i + 1).value setattr(instance, header[i], value) if instance.identifier == None: break else: instance.save() row_index += 1
def render_custom_template(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.POST['container_id'][0] if isinstance(request.POST['container_id'], list) else request.POST['container_id'] container_type = request.POST['container_type'][0] if isinstance(request.POST['container_type'], list) else request.POST['container_type'] container_class = container_type + '_CLASS' container_template = request.POST['container_template'] widget_index = request.POST['widget_index'][0] if isinstance(request.POST['widget_index'], list) else request.POST['widget_index'] widget_title = request.POST['widget_title'][0] if isinstance(request.POST['widget_title'], list) else request.POST['widget_title'] # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) if container_type=='CONT_MAIL_CAMPAIGN': # TODO Assign also application custom data custom_data = get_mailgun_data(container.external_id) for contact_id in custom_data['persons'].keys() + custom_data['companies'].keys(): if contact_id!='companies': instance = get_effective_instance(Container.objects.get(id=contact_id)) if instance.type.identifier=='CONT_PERSON': custom_data['persons'][contact_id]['container'] = instance else: custom_data['companies'][contact_id]['container'] = instance else: custom_data = get_security_information(container) context = {'title': widget_title, 'index':widget_index, 'container': container, 'custom_data': custom_data, 'labels': {label.identifier: label.field_label for label in FieldLabel.objects.filter(langage='en')}} return render(request, container_template, context)
def add_price(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.POST['container_id'] container_type = request.POST['container_type'] container_class = container_type + '_CLASS' price_date = datetime.datetime.strptime(request.POST['price_date'], '%Y-%m-%d') price_value = float(request.POST['price_value']) # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) track = get_track(container, {'track_default': True, 'track_type': 'NUM_TYPE_NAV'}) if track==None: return HttpResponse('{"result": "No valid track", "status_message": "Not saved"}',"json") all_tokens = get_track_content(track) if all_tokens==None: all_tokens = [] found = False for token in all_tokens: if token['date']==price_date: found = True token['value'] = price_value if not found: all_tokens.append({'date': price_date, 'value': price_value}) set_track_content(track, all_tokens, True) return HttpResponse('{"result": "Token added", "status_message": "Saved"}',"json")
def positions(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' working_date = request.GET['working_date'] # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) filtering = lambda d, k: d[k]['data'] fields = list(itertools.chain(*[filtering(setup_content.get_container_type_details()[container_type]['data'], k) for k in setup_content.get_container_type_details()[container_type]['data'].keys()])) # TODO: Handle other langage and factorize with other views labels = dict_to_json_compliance({label.identifier: label.field_label for label in FieldLabel.objects.filter(identifier__in=fields, langage='en')}) positions = get_valuation_content_display(get_positions_portfolio(container)['data']) positions_date = sorted(positions.keys(), reverse=True) currencies = list(set([account.currency.short_name for account in container.accounts.all()])) working_date = get_closest_date(positions, datetime.datetime.strptime(working_date, '%Y-%m-%d'), False) context = {'currencies': currencies, 'positions': positions, 'positions_date': positions_date , 'working_date': working_date, 'complete_fields': complete_fields_information(effective_class, {field:{} for field in fields}), 'container': container, 'container_json': dumps(dict_to_json_compliance(model_to_dict(container), effective_class)), 'container_type': container_type, 'labels': labels} return render(request,'rendition/container_type/details/positions.html', context)
def partial_save(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.POST['container_id'] container_custom = request.POST['container_custom']=='True' container_data = request.POST['container_data'] container_data = json.loads(container_data) container_type = request.POST['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) if container_data.has_key('many-to-many'): foreign = get_model_foreign_field_class(effective_class, container_data['many-to-many']) if foreign!=None: entry = foreign.retrieve_or_create('web', None, None, container_data) if container_data['id']!=None and container_data['id']!='': getattr(container, container_data['many-to-many']).remove(foreign.objects.get(id=container_data['id'])) getattr(container, container_data['many-to-many']).add(entry) container.save() elif container_custom: for entry in container_data.keys(): set_security_information(container, entry, container_data[entry], None) else: for field_key in container_data.keys(): #TODO Handle relation field_info = Attributes() field_info.short_name = field_key.split('.')[0] field_info.name = field_key.split('.')[0] container.set_attribute('web', field_info, container_data[field_key]) container.save() return HttpResponse('{"result": "Finished", "status_message": "Saved"}',"json")
def populate_labels_from_xlsx(model_name, xlsx_file): model = classes.my_class_import(model_name) workbook = load_workbook(xlsx_file) sheet = workbook.get_sheet_by_name(name=model.__name__) row_index = 1 # Reading header header = [] for column_index in range(1, sheet.get_highest_column() + 1): value = sheet.cell(row = row_index, column=column_index).value if value!=None: header.append(value if value!='' else header[-1]) else: break LOGGER.info('Using header:' + str(header)) row_index += 1 while row_index<=sheet.get_highest_row(): if model.objects.filter(identifier=sheet.cell(row = row_index, column=1).value, language=sheet.cell(row = row_index, column=2)).exists(): instance = model.objects.get(identifier=sheet.cell(row = row_index, column=1).value, language=sheet.cell(row = row_index, column=2)) else: instance = model() for i in range(0,len(header)): value = sheet.cell(row = row_index, column=i+1).value setattr(instance, header[i], value) instance.save() row_index += 1
def populate_model_from_xlsx(model_name, xlsx_file): LOGGER.info("Loading data in " + model_name) model = classes.my_class_import(model_name) workbook = load_workbook(xlsx_file) sheet = workbook.get_sheet_by_name(name=model.__name__) row_index = 1 # Reading header header = [] for column_index in range(1, sheet.get_highest_column() + 1): value = sheet.cell(row = row_index, column=column_index).value if value!=None: header.append(value if value!='' else header[-1]) else: break LOGGER.info('Using header:' + str(header)) row_index += 1 while row_index<=sheet.get_highest_row(): instance = model() for i in range(0,len(header)): if sheet.cell(row = row_index, column=i+1).internal_value!=None: value = sheet.cell(row = row_index, column=i+1).value field_info = Attributes() field_info.short_name = header[i] field_info.name = header[i] instance.set_attribute('excel', field_info, value) instance.save() row_index += 1
def populate_model_from_xlsx(model_name, xlsx_file): LOGGER.info("Loading data in " + model_name) model = classes.my_class_import(model_name) workbook = load_workbook(xlsx_file) sheet = workbook.get_sheet_by_name(name=model.__name__) row_index = 1 # Reading header header = [] for column_index in range(1, sheet.get_highest_column() + 1): value = sheet.cell(row=row_index, column=column_index).value if value != None: header.append(value if value != '' else header[-1]) else: break LOGGER.info('Using header:' + str(header)) row_index += 1 while row_index <= sheet.get_highest_row(): instance = model() for i in range(0, len(header)): value = sheet.cell(row=row_index, column=i + 1).value field_info = Attributes() field_info.short_name = header[i] field_info.name = header[i] instance.set_attribute('excel', field_info, value) instance.finalize() if instance.name == None: break else: instance.save() row_index += 1
def base_edit(request): # TODO: Check user container_type = request.POST['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) active_status = Attributes.objects.get(identifier='STATUS_ACTIVE') container_attribute = Attributes.objects.get(identifier=container_type) if request.POST.has_key('container_id'): portfolio_id = request.POST['container_id'] try: source = effective_class.objects.get(Q(id=portfolio_id)) except: # TODO: Return error message return redirect('/containers.html?item=' + container_type) else: source = effective_class() # Initial setup # TODO: Check if name already exists for that container type source.type = container_attribute source.name = request.POST['name'] source.short_name = request.POST['short_name'] source.status = active_status source.save() # Working on creations mandatory fields creation_data = setup_content.get_container_type_creations() if creation_data.has_key(container_type): creation_data = creation_data[container_type] else: creation_data = {} creation_data = complete_fields_information(effective_class, creation_data) for field in creation_data.keys(): if creation_data[field]['type'] in ['ForeignKey', 'ManyToManyField']: if creation_data[field]['type']=='ForeignKey': # TODO: Implement not attribute setattr(source, field, Attributes.objects.get(identifier=request.POST[field], active=True)) source.save() else: target_class = classes.my_class_import(creation_data[field]['target_class']) new_instance = target_class.retrieve_or_create(source, 'FinaLE', request.POST[field + '-' + creation_data[field]['filter']], request.POST[field]) setattr(source, field,[new_instance]) source.save() else: setattr(source, field, request.POST[field]) return redirect('/containers.html?item=' + container_type)
def lists(request): # TODO: Check user container_type = request.GET['item'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) results = effective_class.objects.all().order_by('name') context = {'containers': results, 'container_type': container_type, 'container_label': Attributes.objects.get(identifier=container_type).name} return render(request, 'statics/' + container_type + '_results_lists_en.html', context)
def generate_wizards(): wizards = Attributes.objects.filter(active=True, type='element_wizard') languages = Attributes.objects.filter(active=True, type='available_language') template = loader.get_template('rendition/container_type/creations/wizard.html') for language in languages: for wizard in wizards: working_class = classes.my_class_import(wizard.name) all_fields = get_static_fields(working_class) all_fields = complete_fields_information(working_class, all_fields, language.short_name) context = Context({'fields': working_class.get_wizard_fields(), 'complete_fields': all_fields, 'language_code': language.short_name}) rendition = template.render(context) outfile = os.path.join(TEMPLATES_STATICS_PATH, wizard.short_name + '_' + language.short_name + '.html') with open(outfile,'w') as o: o.write(rendition.encode('utf-8')) context = Context({'fields': [working_class.get_filtering_field()] + working_class.get_wizard_fields(), 'complete_fields': all_fields, 'language_code': language.short_name}) rendition = template.render(context) outfile = os.path.join(TEMPLATES_STATICS_PATH, 'complete_' + wizard.short_name + '_' + language.short_name + '.html') with open(outfile,'w') as o: o.write(rendition.encode('utf-8')) my_class_import('container.flow.modify').generate_wizards()
def delete(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) effective_class.objects.get(id=container_id).delete() return HttpResponse('{"result": true, "status_message": "Deleted"}',"json")
def object_custom_fields_get(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_class = request.POST['container_type'] + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) object_custom_fields = get_container_type_fields() if object_custom_fields.has_key(request.POST['container_type']): object_custom_fields = object_custom_fields[request.POST['container_type']] else: object_custom_fields = [] return HttpResponse('{"result": ' + dumps(object_custom_fields) + ', "status_message": "Found"}',"json")
def get_or_create_user_profile(user_id): profile = setup_content.get_data('user_profiles', user_id) current_user = User.objects.get(id=user_id) mapping_class = my_class_import('container.models.UserMapping') if profile==None or not profile: language_attribute = my_class_import('container.models.Attributes').objects.get(active=True, identifier='AVAIL_LANGUAGE_EN') profile = {'_id': user_id, 'user_name': current_user.username, 'language':language_attribute.identifier, 'base_template': 'gso_' + language_attribute.short_name + '.html', 'language_code': language_attribute.short_name} if not profile.has_key('user_name') or profile['user_name']!=current_user.username: profile['user_name'] = current_user.username profile['is_staff'] = current_user.is_staff or current_user.is_superuser # TODO Cache that mappings = mapping_class.objects.filter(related_user__id=user_id) profile['available_work_places'] = [] for mapping in mappings: if mapping.third_container.type.identifier=='CONT_COMPANY': profile['available_work_places'] += recurse_company_structure(mapping.third_container) else: profile['available_work_places'].append({'third_id':mapping.third_container.id, 'third_name': mapping.third_container.name, 'third_short_name': mapping.third_container.short_name}) if not profile.has_key('current_work_as'): # TODO Reset at login and clean the mess of tests if profile.has_key('default_work_place') and profile['default_work_place']!='administrator': try: profile['current_work_as'] = next(data for (index, data) in enumerate(profile['available_work_places']) if data['third_name'] == profile['default_work_place']['third_name']) except StopIteration: if len(profile['available_work_places'])>0: profile['current_work_as'] = profile['available_work_places'][0] else: LOGGER.error('Invalid profile setup for user ' + current_user.username) elif profile.has_key('default_work_place') and profile['default_work_place']=='administrator': profile['current_work_as'] = profile['default_work_place'] else: if len(profile['available_work_places'])>0: profile['current_work_as'] = profile['available_work_places'][0] else: LOGGER.error('Invalid profile setup for user ' + current_user.username) setup_content.set_data('user_profiles', profile, False) return profile
def security_operation(request): user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) context = {'container': container, 'container_json': dumps(dict_to_json_compliance(model_to_dict(container), effective_class)), 'active_list': ['OPE_TYPE_BUY','OPE_TYPE_SELL','OPE_TYPE_BUY_FOP','OPE_TYPE_SELL_FOP'] } return render(request,'container/create/security_operation.html', context)
def valuations_compute(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) computer = NativeValuationsComputer() #computer.compute_daily_valuation(container) computer.compute_valuation(container, container.frequency) return HttpResponse('{"result": true, "status_message": "Computed"}',"json")
def render_account_selection(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.POST['container_id'][0] if isinstance(request.POST['container_id'], list) else request.POST['container_id'] container_type = request.POST['container_type'][0] if isinstance(request.POST['container_type'], list) else request.POST['container_type'] container_class = container_type + '_CLASS' account_allow_new = request.POST['account_allow_new'].lower()=='true' account_types = request.POST['account_types'] # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) context = {'accounts': container.accounts.filter(account_type__identifier__in=account_types.split(',')), 'allow_new': account_allow_new } return render(request, 'container/view/accounts_options_list.html', context)
def remove(request): if 'target_class' not in request.POST or 'prefix' not in request.POST or 'id' not in request.POST: json_response = {'success': False, 'message': 'Suppression: Un argument est manquant dans l''appel au serveur.'} else: target_class = clean_post_value(request.POST['target_class']) try: target_class = my_class_import(target_class) prefix = clean_post_value(request.POST['prefix']) entity_id = clean_post_value(request.POST['id']) entity = target_class.objects.get(id=entity_id) json_response = {'success': True, 'prefix': prefix, 'value': dict_to_json_compliance(model_to_dict(entity), target_class)} entity.delete() except: json_response = {'success': False, 'message': 'Suppression: L''objet avec l''identifiant [' + entity_id + '] et le type [' + str(target_class) + '] n'' a pas ete trouve dans la base de donnees.' } return HttpResponse(dumps(json_response),"json")
def render_singles_list(request): # TODO: Check user profile = get_or_create_user_profile(request.user.id) container_id = clean_post_value(request.POST['container_id']) container_type = clean_post_value(request.POST['container_type']) container_class = container_type + '_CLASS' container_fields = eval(clean_post_value(request.POST['container_fields'])) widget_index = clean_post_value(request.POST['widget_index']) widget_title = clean_post_value(request.POST['widget_title']) # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) context = {'base_template': profile['base_template'], 'profile': profile, 'title': widget_title, 'index':widget_index, 'container': container, 'fields': container_fields, 'application_settings': settings} return render(request, 'container/view/simple_fields_list.html', context)
def operation(request, view_extension): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] operation_id = request.GET['operation_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) operation = FinancialOperation.objects.get(id=operation_id) context = {'container': container, 'operation': operation} return render(request,'rendition/container_type/details/operation.' + view_extension, context)
def external_import(request): # TODO: Check user user = User.objects.get(id=request.user.id) external_provider = request.GET['external'] data_type = request.GET['target'] container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) external = classes.my_import('providers.' + external_provider) getattr(external, 'import_' + data_type)(container) return HttpResponse('{"result": true, "status_message": "Executed"}',"json")
def render_singles_list(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.POST['container_id'][0] if isinstance(request.POST['container_id'], list) else request.POST['container_id'] container_type = request.POST['container_type'][0] if isinstance(request.POST['container_type'], list) else request.POST['container_type'] container_class = container_type + '_CLASS' container_fields = eval(request.POST['container_fields']) widget_index = request.POST['widget_index'][0] if isinstance(request.POST['widget_index'], list) else request.POST['widget_index'] widget_title = request.POST['widget_title'][0] if isinstance(request.POST['widget_title'], list) else request.POST['widget_title'] # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) context = {'title': widget_title, 'index':widget_index, 'container': container, 'fields': container_fields, 'labels': {label.identifier: label.field_label for label in FieldLabel.objects.filter(identifier__in=container_fields, langage='en')}} return render(request, 'container/view/simple_fields_list.html', context)
def operation_remove(request): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.POST['container_id'] operation_id = request.POST['operation_id'] container_type = request.POST['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) operation = FinancialOperation.objects.get(id=operation_id) FinancialOperation.objects.filter(associated_operation__id=operation_id).delete() operation.delete() return HttpResponse('{"result": "Operation removed", "status_message": "Removed"}',"json")
def get_filtering_entry(request): user = User.objects.get(id=request.user.id) # TODO: Check user container_type = request.POST['container_type'] filtered_field = request.POST['filtered_field'] filtering_field = request.POST['filtering_field'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) target_class = effective_class._meta.get_field(filtered_field).rel.to limit = dict(target_class._meta.get_field(str(filtering_field)).rel.limit_choices_to) limit['active'] = True results = dumps([model_to_dict(item) for item in target_class._meta.get_field(filtering_field).rel.to.objects.filter(**limit)]) return HttpResponse('{"result": ' + results + ', "status_message": "Saved"}',"json")
def valuations(request, view_extension): # TODO: Check user user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' # TODO: Handle error effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) # TODO: Handle other langage and factorize with other views valuations = get_valuation_content_display(get_portfolio_valuations(container)['data']) valuations_date = sorted(valuations.keys(), reverse=True) currencies = list(set([account.currency.short_name for account in container.accounts.all()])) context = {'currencies': currencies, 'valuations': valuations, 'valuations_date': valuations_date, 'container': container, 'container_json': dumps(dict_to_json_compliance(model_to_dict(container), effective_class)), 'container_type': container_type} return render(request,'rendition/container_type/details/valuations.' + view_extension, context)
def cash_operation(request): user = User.objects.get(id=request.user.id) container_id = request.GET['container_id'] container_type = request.GET['container_type'] container_class = container_type + '_CLASS' effective_class_name = Attributes.objects.get(identifier=container_class, active=True).name effective_class = classes.my_class_import(effective_class_name) container = effective_class.objects.get(id=container_id) context = {'container': container, 'container_json': dumps(dict_to_json_compliance(model_to_dict(container), effective_class)), 'active_list': ['OPE_TYPE_CASH_WITHDRAWAL','OPE_TYPE_CASH_CONTRIBUTION','OPE_TYPE_WITHDRAWAL', 'OPE_TYPE_CONTRIBUTION','OPE_TYPE_INTERNAL_TRANSFER','OPE_TYPE_FEES', 'OPE_TYPE_COMMISSION','OPE_TYPE_TAX','OPE_TYPE_COUPON','OPE_TYPE_DIVIDEND'] } return render(request,'container/create/cash_operation.html', context)
def update_model_from_xlsx(model_name, xlsx_file, keys, fields): LOGGER.info("Loading data in " + model_name) model = classes.my_class_import(model_name) workbook = load_workbook(xlsx_file) sheet = workbook.get_sheet_by_name(name=model.__name__) row_index = 1 # Reading header header = [] for column_index in range(1, sheet.get_highest_column() + 1): value = sheet.cell(row=row_index, column=column_index).value if value != None: header.append(value if value != '' else header[-1]) else: break LOGGER.info('Using header:' + str(header)) row_index += 1 while row_index <= sheet.get_highest_row(): filtering_by = {} for key in keys: if model._meta.get_field(key).get_internal_type() == 'ForeignKey': filtering_by[key + '__name'] = sheet.cell( row=row_index, column=header.index(key, ) + 1).value else: filtering_by[key] = sheet.cell(row=row_index, column=header.index(key, ) + 1).value by_identifier = model.objects.filter(**filtering_by) if by_identifier.exists(): instance = by_identifier[0] for i in range(0, len(header)): if header[i] in fields: value = sheet.cell(row=row_index, column=i + 1).value field_info = Attributes() field_info.short_name = header[i] field_info.name = header[i] instance.set_attribute('excel', field_info, value) instance.finalize() if instance.name == None: break else: instance.save() else: LOGGER.error("Could not find model with " + str(by_identifier)) row_index += 1