Ejemplo n.º 1
0
    def save_shipping_line(self, request):
        try:

            line_logo = request.FILES.get('logo')
            line_data = json.loads(request.data.get('lineData'))

            line_id = line_data.get('id')
            code_name = line_data.get('codeName')
            name = line_data.get('name')
            website = line_data.get('website')
            tracking_website = line_data.get('trackingWebsite')
            logo_exist = line_data.get('logo')

            if line_id:
                line = ShippingLine.objects.get(id=line_id)
                line.updated_by = request.user
                line.updated_at = dt.now()
                if line_logo:
                    if line.logo:
                        path = Utilities.get_media_directory() + '/' + str(
                            line.logo)
                        os.remove(path)
                    line.logo = line_logo
                else:
                    if not logo_exist:
                        path = Utilities.get_media_directory() + '/' + str(
                            line.logo)
                        os.remove(path)
                        line.logo = None
                success_message = self.messages['successPUT']
            else:
                line = ShippingLine()
                line.created_by = request.user
                if line_logo:
                    line.logo = line_logo
                success_message = self.messages['successPOST']

            line.code_name = code_name
            line.name = name
            line.website = website
            line.tracking_website = tracking_website
            line.save()

            return Response({
                'success': True,
                'message': success_message
            },
                            status=status.HTTP_200_OK)

        except Exception, e:
            return Response({
                'success': False,
                'message': str(e)
            },
                            status=status.HTTP_200_OK)
Ejemplo n.º 2
0
    def save_blog(self, request):
        try:
            base_url = request.META.get('HTTP_HOST')
            research_image = request.FILES.get('image')
            research_data = json.loads(request.data.get('research'))
            research_id = research_data.get('id')
            research_draft = research_data.get('isDraft')
            research_title = research_data.get('title')
            research_slug = research_data.get('slug')
            research_tags = research_data.get('tags')
            research_tags = [tag.get('text') for tag in research_tags]
            self.make_new_research_tags(research_tags)
            height = research_data.get('height')
            width = research_data.get('width')
            research_content = research_data.get('content')
            if research_id:

                success_message = self.messages['successPUT']

                research = Post.objects.get(id=research_id)
                print 'herre'
                research.updated = dt.now()

                if research.image and research_image:
                    try:
                        path = Utilities.get_media_directory() + '/' + str(
                            research.image)
                        os.remove(path)
                    except:
                        pass
            else:
                research = Post()
                success_message = self.messages['successPOST']
            research.image = research_image
            research.slug = research_slug
            research.user = request.user
            research.title = research_title
            research.height_field = height
            research.width_field = width
            research.content = research_content
            research.draft = research_draft
            if not research_draft:
                research.publish = dt.now()
            else:
                research.publish = None
            tags = Tag.objects.filter(name__in=research_tags)
            research.save()
            for tag in tags:
                research.tags.add(tag)
            research.save()
            return Response({
                'success':
                True,
                'researchObj':
                research.get_list_obj(base_url, request.user),
                'message':
                success_message
            })
        except Exception, e:
            return Response({'success': False, 'message': str(e)})
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        try:
            try:
                new_user_data = request.data.get('user')
                user = self.set_user_obj_from_request(new_user_data)
            except IntegrityError as e:
                if e[1] == "Duplicate entry '' for key 'username'":
                    return Response({
                        'success':
                        False,
                        'message':
                        'A user with this user name already exist'
                    })
            except Exception, e:
                return Response({'success': False, 'message': str(e)})

            business = Utilities.get_user_business(request.user)
            new_profile = self.set_profile_obj_from_request(new_user_data,
                                                            business=business)
            new_profile.user = user
            new_profile.created_by = request.user
            new_profile.created_at = dt.now()
            new_profile.save()
            return Response({
                'success': True,
                'user_id': user.id,
                'message': 'User Profile Added Successfully'
            })
Ejemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     user = request.user
     alpha = request.GET.get('alpha')
     base_url = request.META.get('HTTP_HOST')
     business = Utilities.get_user_business(user)
     all_business = self.get_business_list(business, base_url, alpha)
     return Response({'businessList': all_business},
                     status=status.HTTP_200_OK)
Ejemplo n.º 5
0
    def save_product(self, request):
        try:
            user = request.user
            base_url = request.META.get('HTTP_HOST')
            product_image = request.FILES.get('image')
            product_data = json.loads(request.data.get('product'))
            product_id = product_data.get('id')
            product_name = product_data.get('name')
            product_description = product_data.get('description')
            product_category_id = product_data.get('categoryId')
            product_code = product_data.get('productCode')
            related_product = product_data.get('relatedProduct')

            if product_id:
                success_message = self.messages['successPUT']
                error_message = self.messages['errorPUT']
                product = Products.objects.get(id=product_id)
                product.updated_by = user
                product.updated_at = dt.now()
                if product.image and product_image:
                    try:
                        path = Utilities.get_media_directory() + '/' + str(
                            product.image)
                        os.remove(path)
                    except:
                        pass
            else:
                success_message = self.messages['successPOST']
                error_message = self.messages['errorPOST']
                product = Products()
                product.created_by = request.user

            if product_image:
                product.image = product_image
            product.name = product_name
            product.description = product_description
            product.category = ProductCategory.objects.get(
                id=int(product_category_id))
            product.product_code = product_code
            product.save()
            self.save_related_products(product, related_product)
            cache.delete('get_product_drop_down')
            return Response(
                {
                    'success': True,
                    'message': success_message,
                    'id': product.id,
                },
                status=status.HTTP_200_OK)
        except Exception, e:
            return Response({
                'success': False,
                'message': str(e)
            },
                            status=status.HTTP_200_OK)
Ejemplo n.º 6
0
    def put(self, request, *args, **kwargs):
        try:

            logo = request.FILES.get('logo')

            business_data = request.data.get('data')
            business_data = json.loads(business_data)
            business_id = business_data.get('bpId')
            business_type_id = business_data.get('bpType')

            business = BpBasic.objects.get(bp_id=business_id)
            if logo:
                if business.bp_logo:
                    path = Utilities.get_media_directory() + '/' + str(
                        business.bp_logo)
                    os.remove(path)
                business.bp_logo = logo
            business.bp_name = business_data.get('name')
            business.bp_ntn = business_data.get('ntn')
            business.bp_website = business_data.get('website')
            business.bp_database_id = business_data.get('databaseId')
            business.updated_by = request.user

            business.updated_at = dt.now()
            business.save()

            business_type_removed = business.bp_types.exclude(
                id__in=business_type_id)
            business_types = BusinessType.objects.filter(
                id__in=business_type_id)

            # Removing Business Types
            for type in business_type_removed:
                business.bp_types.remove(type)
            # Adding Business Types
            for type in business_types:
                if not business.bp_types.filter(id=type.id).exists():
                    business.bp_types.add(type)
            business.save()
            base_url = request.META.get('HTTP_HOST')
            user_business = request.user.profile.business
            self.delete_cache(user_business, base_url, business.bp_name)
            return Response({
                'success': True,
                'bpId': business.bp_id,
                'message': self.messages['successPUT']
            })
        except Exception, e:

            return Response({
                'success': False,
                'message': self.messages['errorPUT']
            })
Ejemplo n.º 7
0
 def get(self, request, *args, **kwargs):
     user = request.user
     q = request.GET.get('q')
     base_url = request.META.get('HTTP_HOST')
     business = Utilities.get_user_business(user)
     if q == self.ALL_QUERY or q == self.DROP_DOWN_QUERY:
         all_users = UserProfile.objects.filter(business=business)
         return Response(
             {
                 'list':
                 [user.get_list_obj(request.user.id) for user in all_users]
             },
             status=status.HTTP_200_OK)
     else:
         profile = UserProfile.objects.get(user__id=int(q))
         return Response({'user': profile.complete_profile(base_url)},
                         status=status.HTTP_200_OK)
Ejemplo n.º 8
0
    def post(self, request, *args, **kwargs):
        user = self.request.user
        base_url = request.META.get('HTTP_HOST')
        user_id = kwargs.get('user_id')
        pic = request.FILES.get('profile')
        user_profile = UserProfile.objects.get(user__id=user_id)

        if user_profile.profile_pic:
            try:
                path = Utilities.get_media_directory() + '/' + str(
                    user_profile.profile_pic)
                os.remove(path)
            except OSError:
                pass
        user_profile.profile_pic = pic
        user_profile.save()
        print user_profile.__dict__
        return Response(
            {
                'userId': user_id,
                'profile_pic': user_profile.get_profile_pic(base_url)
            },
            status=status.HTTP_200_OK)
Ejemplo n.º 9
0
    def delete(self, request, *args, **kwargs):
        success = False
        try:
            user = request.user
            research_id = kwargs.get('research_id')
            research = Post.objects.get(id=research_id)
            if research.image:
                try:
                    path = Utilities.get_media_directory() + '/' + str(
                        research.image)
                    os.remove(path)
                except:
                    pass

            if (research.user == user) or user.is_superuser:
                research.delete()
                success = True
                message = 'Research has been delete successfully'
            else:

                message = 'You are not authorized to delete this researh'
        except Exception, e:
            message = str(e)
            success = False