def create_simple_user(request): """create simple user fiscal info :basic_info: (dict) :param email: (str) :param first_name: (str) :param last_name: (str) :param fiscal: (dict) :param rfc: (str) :param business_name: (str) :param physical_person: (bool) :param address: (dict) :param street: (str) :param street_number: (str) :param zip_code: (str) :param neighborhood: (str) :param city: (str) :param state: (str) """ import random import re req_inf = RequestInfo() try: try: username = request.data['basic_info']['username'] except KeyError: request.data['basic_info']['username'] = re.sub( "[!@#$%^&*()[]{};:,./<>?\|`~-=_+]", " ", request.data['basic_info']['email'][:(request.data['basic_info']['email'].find('@'))] ) request.data['basic_info']['username'] = '******'.format( request.data['basic_info']['username'], random.randrange(10**8) ) except Exception: pass user_serializer = BasicUserSerializer(data=request.data.get('basic_info')) if user_serializer.is_valid(): user_serializer.save() request.data['fiscal']['user'] = user_serializer.instance.id fiscal_serializer = FiscalSerializer( Fiscal.objects.get(user_id=user_serializer.instance.id), data=request.data.get('fiscal') ) if fiscal_serializer.is_valid(): fiscal_serializer.save() request.data['address']['fiscal'] = fiscal_serializer.instance.id address_serializer = AddreesSerializer( Address.objects.get(fiscal_id=fiscal_serializer.instance.id), data=request.data.get('address')) if address_serializer.is_valid(): address_serializer.save() return req_inf.status() except Exception as e: return req_inf.status(e.args[0], status.HTTP_400_BAD_REQUEST)
def get(self, request): """Get fiscal information from user """ req_inf = RequestInfo() rfc = request.GET.get('rfc', None) user = request.user try: if rfc is not None: user = Fiscal.objects.get(rfc=rfc).user return Response(UserInfoSerializer(user).data) except ObjectDoesNotExist as e: return req_inf.status(e.args[0], status.HTTP_404_NOT_FOUND) except Exception as e: return req_inf.status(e.args[0], status.HTTP_400_BAD_REQUEST)
def create_bill(request): """create bill :param basic_information: (dict) :param emisor_rfc: (str) :param receiver_rfc: (str) :param date_expedition: (date) :param coin: (str) :param folio: (str) :param way_to_pay: (str) :param concepts: (list of dicts) :param product_key: (str) :param quantity: (str) :param description: (str) :param amount: (str) """ req_inf = RequestInfo() errors = [] concepts = [] try: request.data['basic_information']['emisor'] = Fiscal.objects.get( rfc=request.data['basic_information']['emisor_rfc']).user.id request.data['basic_information']['receiver'] = Fiscal.objects.get( rfc=request.data['basic_information']['receiver_rfc']).user.id request.data['basic_information'].pop('emisor_rfc') request.data['basic_information'].pop('receiver_rfc') for data in request.data.get('concepts'): concept_serializer = ConceptSerializer(data=data) if concept_serializer.is_valid(): concept_serializer.save() concepts.append(concept_serializer.instance) else: errors.append(concept_serializer.errors) if len(errors) == 0: bill_serializer = SimpleFacturaSerializer( data=request.data.get('basic_information')) if bill_serializer.is_valid(): bill_serializer.save() for con in concepts: bill_serializer.instance.concepts.add(con) bill_serializer.save() return req_inf.status() else: return req_inf.status(bill_serializer.errors, status.HTTP_400_BAD_REQUEST) else: return req_inf.return_status(errors) except Exception as e: return req_inf.status(e.args[0], status.HTTP_400_BAD_REQUEST)
def put(self, request): """modification of tax data :param fiscal: (dic): :param rfc: (str) :param business_name: (str) :param physical_person: (bool) :param address: (dict) :param street: (str) :param street_number: (str) :param zip_code: (str) :param neighborhood: (str) :param city: (str) :param state: (str) """ req_inf = RequestInfo() errors = [] rfc = request.data.get('rfc', None) user = request.user if rfc is not None: user = Fiscal.objects.get(rfc=rfc).user try: fiscal_serializer = FiscalSerializer( Fiscal.objects.get(user=user), data=request.data.get('fiscal') ) address_serializer = AddreesSerializer( Address.objects.get(fiscal_id=fiscal_serializer.instance.id), data=request.data.get('address') ) if fiscal_serializer.is_valid() and address_serializer.is_valid(): fiscal_serializer.save() address_serializer.save() return req_inf.status() else: try: errors.append(fiscal_serializer.errors) except Exception: pass try: errors.append(address_serializer.errors) except Exception: pass return req_inf.status(errors, status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: return req_inf.status(e.args[0], status.HTTP_404_NOT_FOUND) except Exception as e: return req_inf.status(e.args[0], status.HTTP_400_BAD_REQUEST)
def get_bill(request): """download bill """ req_inf = RequestInfo() try: bill = Factura.objects.get(_id=request.GET.get('bill')) name = 'bill-{}{}.xml'.format(request.user.username, bill.id) with open(name, 'wb') as f: f.write( dicttoxml(FacturaDetailSerializer(bill).data, attr_type=False)) bill_file = open('{}/{}'.format(settings.BASE_DIR, name), 'rb') response = HttpResponse(FileWrapper(bill_file), content_type='application/xml') response['Content-Disposition'] = 'attachment; filename="{}"'.format( name) os.remove('{}/{}'.format(settings.BASE_DIR, name)) return response except ObjectDoesNotExist as e: return req_inf.status(e.args[0], status.HTTP_404_NOT_FOUND) except Exception as e: return req_inf.status(e.args[0], status.HTTP_400_BAD_REQUEST)
def post(self, request): """Creates user accounts :param email: str :param first_name: str :param last_name: str :param password: str :return token: jwt_token """ import re req_inf = RequestInfo() try: username = request.data['basic_info']['username'] except KeyError: request.data['username'] = re.sub( "[!@#$%^&*()[]{};:,./<>?\|`~-=_+]", " ", request.data['email'][:(request.data['email'].find('@'))] ) request.data['username'] = '******'.format( request.data['username'], random.randrange(10**8) ) except Exception: pass try: serializer = CreateUserSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({ 'token': generate_jwt(serializer.instance) }) else: return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST ) except ObjectDoesNotExist as e: return req_inf.status(e.args[0], status.HTTP_404_NOT_FOUND) except Exception as e: return req_inf.status(e.args[0], status.HTTP_400_BAD_REQUEST)
def post(self, request): """UserClientAPIView post Description: Create clients Args: :param name: (str) the name of the client """ req_inf = RequestInfo() serializer = ClientSerializer(data=request.data) if serializer.is_valid(): try: serializer.save() log = Log.objects.create( action=Log.NOTIFICATION_TYPE.get_value('create_client'), user=request.user, description='Creacion de cliente {} - {}'.format( serializer.instance.id, serializer.instance.name)) log.save() return req_inf.status_200() except Exception as e: return req_inf.status(e.args[0]) else: return req_inf.status_400(serializer.errors)