Example #1
0
    def add_post(self, request):
        response = ResponseParcel.ResponseParcel()
        try:
            post_img = ""
            image_data = ImageUploadForm(request.POST, request.FILES)

            if image_data.is_valid():

                img = PostImage(post_image=image_data.cleaned_data['image'])
                img.save()
                post_img = img.post_image.url[config.env_img_concat_index:]
                print config.env_img_concat_index

            user_post = User.objects.get(pk=request.POST.get('userId'))

            user_post.post_set.create(
                share_type=request.POST.get('share'),
                title=request.POST.get('title') if request.POST.get('title') != "None" else "",
                content_text=request.POST.get('text'),
                content_image=post_img,
                content_link=request.POST.get('link') if request.POST.get('link') != "None" else "",
                date=timezone.now()
            )

            response.set_data({'status': 'saved', 'image': post_img})
            return response.data_to_json()

        except Exception as e:
            print e
            response.set_data({'status': 'error'})
            return response.data_to_json()
Example #2
0
    def get_new_post(self, request):
        response = ResponseParcel.ResponseParcel()
        post_update = dict()

        if request.GET.get('latest') == "None":
            
            try:
                # Get fresh post
                post = Post.objects.all().order_by('-date')[:10]
                post_update = self.prepare_new_post(post)
            
            except:
                # No posts
                post_update['status'] = 'Unavailable'
                
        else:
            get_client_latest_post = int(request.GET.get('latest'))
            check_post = Post.objects.all().order_by('-date').first()
            
            # check if there is a post update
            if check_post.id == get_client_latest_post:
                # posts are already updated
                post_update['status'] = 'updated'
                pass
            else:
                get_update = Post.objects.all().order_by('-date')
                post_update = self.get_update_post(get_update, get_client_latest_post)

        response.set_data(post_update)
        return response.data_to_json()
    def create(self, request):
        response = ResponseParcel.ResponseParcel()
        try:
            cover_img = ""
            image_data = ImageUploadForm(request.POST, request.FILES)

            if image_data.is_valid():

                img = PostImage(post_image=image_data.cleaned_data['image'])
                img.save()
                cover_img = img.post_image.url[config.env_img_concat_index:]

            user = User.objects.get(pk=request.POST.get('userId'))

            user.article_set.create(
                title=request.POST.get('title')
                if request.POST.get('title') != "None" else "",
                content=request.POST.get('content'),
                cover=cover_img,
                date=timezone.now())

            response.set_data({'status': 'saved', 'image': cover_img})
            return response.data_to_json()

        except Exception as e:
            print e
            response.set_data({'status': 'error'})
            return response.data_to_json()
    def logout(self, request):
        response = ResponseParcel.ResponseParcel()

        auth_service = AuthService.AuthService()
        auth_service.set_request_data(request)
        auth_service.end_session()

        response.set_uri('/colla/')
        return response.redirect()
Example #5
0
    def update_profile(self, request):
        response = ResponseParcel.ResponseParcel()

        try:
            user_update = User.objects.get(pk=request.POST['user_id'])
            user_update.username = request.POST['username']
            user_update.save()

            form_image = ImageUploadForm(request.POST, request.FILES)

            profile_update = Profile.objects.get(user=user_update.id)

            if form_image.is_valid():

                img = ProfileImage(
                    profile_image=form_image.cleaned_data['image'])
                img.save()

                try:
                    self.delete_profile_img(config.profile_update.profile_pic)

                except:
                    pass

                profile_img_update = img.profile_image.url[
                    config.env_img_concat_index:]
                profile_update.profile_pic = profile_img_update

            profile_update.dis_name = request.POST['display_name']
            profile_update.first_name = request.POST['first_name']
            profile_update.last_name = request.POST['last_name']
            profile_update.middle_name = request.POST['middle_name']
            profile_update.position = request.POST['position']
            profile_update.company_name = request.POST['company']
            profile_update.mail_address = request.POST['mail']

            profile_update.save()

            # update posts, comments, agrees
            post_update = Post.objects.filter(user=user_update.id)
            post_update.update(user_dis_name=profile_update.dis_name,
                               user_pic=profile_update.profile_pic)

            for post in post_update:
                Comment.objects.filter(post=post.id).update(
                    user_name=profile_update.dis_name,
                    user_pic_url=profile_update.profile_pic)

                Agree.objects.filter(post=post.id).update(
                    user_name=profile_update.dis_name)

            return None

        except Exception as e:
            print e
            response.set_uri('/colla')
            return response.redirect()
    def browse(self, request):
        response = ResponseParcel.ResponseParcel()
        articles = dict()

        try:
            article = Article.objects.all().order_by('-date')
            articles = self.prepare(article)

        except Exception as e:
            print e

        response.set_data(articles)
        return response.data_to_json()
    def new_message(self, request):
        response = ResponseParcel.ResponseParcel()
        sender = request.POST.get('sender')
        receiver = request.POST.get('reciever')
        message = request.POST.get('message')
        
        sender_chat = ChatUser.objects.filter(user_id=sender)
        receiver_chat = ChatUser.objects.filter(user_id=receiver)
        
        # check if they had conversations
        if sender_chat.count() != 0 and receiver_chat.count() != 0:
            cnt_sender = 1
            check_add = 0
            
            for chat_from in sender_chat:
                
                cnt_receiver = 1
                for chat_to in receiver_chat:
                    
                    if chat_from.chat_id == chat_to.chat_id:
                        
                        # They had conversation
                        chat_id = chat_from.chat_id
                        self.add_message(sender, message, chat_id)
                        check_add = 1
                        break
                    
                    else:
                        
                        if sender_chat.count() == cnt_sender:

                            if receiver_chat.count() == cnt_receiver and check_add == 0:

                                # They had no conversations
                                self.create_chat_conversation(sender, receiver, message)
                                check_add = 0
                                break
                            
                    cnt_receiver += 1
                    
                cnt_sender += 1

            response.set_message('Get Messages')
            return response.to_json()
        
        # Create new conversation to them
        else:
            self.create_chat_conversation(sender, receiver, message)
            response.set_message('get their new born chat')
            return response.to_json()
Example #8
0
    def get_more_post(self, request):
        response = ResponseParcel.ResponseParcel()
        post_offset = request.GET.get('offset')
        offset = int(post_offset)
        
        try:
            more_post = Post.objects.all().order_by('-date')[offset:offset+10]
            posts = self.prepare_new_post(more_post)
            
        except Exception as e:
            print e
            posts = {'status': 'no more post'}

        response.set_data(posts)
        return response.data_to_json()
Example #9
0
    def profile(self, request):
        response = ResponseParcel.ResponseParcel()

        try:
            auth = self.get_profile(request)
            user_profile = User.objects.get(pk=auth.user_id)

            response.set_uri('colla/profile.html')
            response.set_data({'user': user_profile})
            return response.render(request)

        except Exception as e:
            print e
            response.set_uri('/colla')
            return response.redirect()
    def authenticate(self, request):
        auth = AuthService()
        auth.set_request_data(request)
        parcel = ResponseParcel.ResponseParcel()

        access_token = Authentication.objects.get(
            access_token=auth.__session_token)

        if access_token is not None:
            if access_token.expired_at > timezone.now():
                return
            else:
                access_token.delete()

        parcel.set_uri('colla/login.html')
        return parcel.render(request)
    def get_message(self, request):
        response = ResponseParcel.ResponseParcel()
        sender = request.GET.get('sender')
        receiver = request.GET.get('reciever')
        
        sender_chat = ChatUser.objects.filter(user_id = sender)
        receiver_chat = ChatUser.objects.filter(user_id = receiver)
        
        messages = dict()
        
        # check if they had conversations
        if sender_chat.count() != 0 and receiver_chat.count() != 0:
            cnt_sender = 1

            for chat_from in sender_chat: 
                cnt_receiver = 1
                
                for chat_to in receiver_chat:
                    
                    if chat_from.chat_id == chat_to.chat_id:
                        
                        # They had conversation
                        chat_id = chat_from.chat_id
                        msg = ChatMessage.objects.filter(chat = chat_id).order_by('date_sent')
                        messages = self.prepare_message(msg)
                        break
                    
                    else:
                        
                        if sender_chat.count() == cnt_sender:

                            if receiver_chat.count() == cnt_receiver:

                                # They had no conversations
                                break
                            
                    cnt_receiver += 1
                    
                cnt_sender += 1

        response.set_data(messages)
        return response.data_to_json()
    def do_register(self, request):
        pass_helper = PasswordHelper.PasswordHelper()
        response = ResponseParcel.ResponseParcel()
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)

        try:

            if user_form.is_valid():
                new_user = User(
                    username=request.POST['username'],
                    password=pass_helper.encode(request.POST['password']),
                    fullname=request.POST['first_name']
                )

                if profile_form.is_valid():
                    new_user.save()
                    new_user.profile_set.create(
                        dis_name=request.POST['first_name'],
                        profile_pic="/static/colla/images/profile_img/av-default.png",
                        first_name=request.POST['first_name'],
                        last_name=request.POST['last_name'],
                        middle_name=request.POST['middle_name'],
                        position=request.POST['position'],
                        company_name=request.POST['company'],
                        mail_address=request.POST['mail']
                    )

                    response.set_message("Success")
                    return response.to_json()

            else:
                response.set_message("Error")
                response.has_error()
                return response.to_json()
        
        except Exception as e:
            print e

            response.has_error()
            return response.to_json()
Example #13
0
    def comment_post(self, request):
        response = ResponseParcel.ResponseParcel()
        try:
            post_id = request.POST.get('post_id')
            user_id = request.POST.get('user_id')

            post_comment = Post.objects.get(pk=post_id)
            post_comment.comments += 1
            post_comment.save()
            post_comment.comment_set.create(
                user_id=user_id,
                comment_date=timezone.now(),
                comment=request.POST.get('comment')
            )
            response.set_data({'status': 'comment saved'})
            return response.data_to_json()
        
        except Exception as e:
            print e
            response.set_uri('/colla')
            return response.redirect()
Example #14
0
    def agree_post(self, request):
        response = ResponseParcel.ResponseParcel()
        try:
            post_id = request.POST.get('post_id')
            post_name_agreed = request.POST.get('user_name')
            post_agree = Post.objects.get(pk=post_id)

            # Checks only anybody who agreed
            if post_agree.user_dis_name != post_name_agreed:
                user_agreed = "Nobody"
                post_agree_checked = post_agree.agree_set.filter(user_name=post_name_agreed)
                
                for post in post_agree_checked:
                    user_agreed = post.user_name

                if user_agreed == post_name_agreed:
                    response.set_data({'status': 'Already agreed'})
                    return response.data_to_json()

                else:
                    post_agree.agrees += 1
                    post_agree.save()
                    post_agree.agree_set.create(
                        user_name=post_name_agreed
                    )
                    response.set_data({'status': 'agreed'})
                    return response.data_to_json()

            else:
                response.set_data({'status': 'You owned the post'})
                return response.data_to_json()

        except Exception as e:
            print e
            response.set_uri('/colla')
            return response.redirect()
Example #15
0
    def facebook_login(self, request):
        response = ResponseParcel.ResponseParcel()
        auth_service = AuthService.AuthService()

        try:

            try:
                social_user = User.objects.get(
                    provider_user_id=request.GET.get('id'))

            except Exception as e:
                print "new social user"
                # new soc user
                social_user = User(fullname=request.GET.get('first_name'),
                                   provider_user_id=request.GET.get('id'))

                social_user.save()

                social_user.profile_set.create(
                    dis_name=request.GET.get('name'),
                    first_name=request.GET.get('first_name'),
                    last_name=request.GET.get('last_name'),
                    profile_pic='http://graph.facebook.com/' +
                    request.GET.get('id') + '/picture?type=large')

            auth_service.set_request_data(request)
            auth_service.set_user(social_user)
            auth_service.add_session()

            response.set_message('Success')
            return response.to_json()

        except Exception as e:
            print e
            response.has_error()
            return response.to_json()
Example #16
0
    def app_login(self, request):
        response = ResponseParcel.ResponseParcel()
        auth_service = AuthService.AuthService()
        auth_service.set_request_data(request)
        pass_helper = PasswordHelper.PasswordHelper()

        now = timezone.localtime(timezone.now())

        if request.method == 'GET':

            try:
                # directing uri
                authentication = Authentication.objects.get(
                    access_token=auth_service.get_token())
                auth_user = User.objects.get(pk=authentication.user_id)

                # active session
                # @TODO: Fix this
                if authentication.expired_at > now:
                    post = Post.objects.all().order_by('-date')[:10]
                    all_users = User.objects.order_by('username')

                    response.set_data({
                        'auth_user': auth_user,
                        'post': post,
                        'users': all_users
                    })
                    response.set_uri('colla/index.html')
                    return response.render(request)

                else:
                    auth_service.end_session()
                    raise Exception("Expired token")

            except Exception as e:
                print e
                response.set_uri('colla/login.html')
                return response.render(request)

        if request.method == 'POST':
            user_name = request.POST['username']
            password = request.POST['password']

            try:

                if user_name != '' and password != '':
                    verified_user = User.objects.get(username=user_name)
                    auth_user = User.objects.get(pk=verified_user.id)

                    request_password = password
                    user_password = pass_helper.decode(verified_user.password)

                    if user_password == request_password:
                        auth_service.set_user(auth_user)
                        auth_service.add_session()
                        post = Post.objects.all().order_by('-date')[:10]
                        all_users = User.objects.order_by('username')

                        response.set_uri('colla/index.html')
                        response.set_data({
                            'auth_user': auth_user,
                            'post': post,
                            'users': all_users
                        })
                        return response.render(request)

                # returns error
                response.has_error()
                response.set_message('Wrong Username Password')
                return response.to_json()

            except Exception as e:
                print e
                response.has_error()
                response.set_message('Wrong Username Password')
                return response.to_json()
 def register(self, request):
     response = ResponseParcel.ResponseParcel()
     response.set_uri('colla/signup.html')
     return response.render(request)
 def index(self, request):
     response = ResponseParcel.ResponseParcel()
     response.set_uri('/colla/')
     return response.redirect()