Esempio n. 1
0
def sendMessage (request):
    if request.method != 'POST':
        return HttpResponse ("There is POST method only.")
    keys = request.POST.keys()
    response = {}
    if "API" not in keys:
        response["Error"] = {"ErrorCode": "666", "ErrorDescription": "There is no API parameter."}
        return HttpResponse (json.dumps(response))
    else:
        api_ver = request.POST.get("API")
    if api_ver != "1":
        response["Error"] = {"ErrorCode": "777", "ErrorDescription": "Wrong API version."}
        return HttpResponse (json.dumps(response))
    else:
        response["Error"] = {"ErrorCode": "", "ErrorDescription": ""}
    
    key = request.POST.get('Key', None)
    episode_id = request.POST.get('EpisodeId', None)
    trailer_id = request.POST.get('TrailerId', None)
    message = request.POST.get('Message', None)
    timeslot_id = request.POST.get('TimeslotId', None)
    screenshot_url = request.POST.get('ScreenshotURL', None)
    user_name = request.POST.get('UserName', None)

    if episode_id and trailer_id:
        response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There are EpisodeId and TrailerId. Should be only one param."}
        return HttpResponse (json.dumps(response))
    
    if not episode_id and not trailer_id:
        response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There are no EpisodeId or TrailerId. Should be one param."}
        return HttpResponse (json.dumps(response))

    if not message:
        response["Error"] = {"ErrorCode": "4", "ErrorDescription": "Message is empty."}
        return HttpResponse (json.dumps(response))

    if len(message) >= 200:
        response["Error"] = {"ErrorCode": "4", "ErrorDescription": "Message is too long."}
        return HttpResponse (json.dumps(response))

    if not user_name:
        response["Error"] = {"ErrorCode": "4", "ErrorDescription": "There is no UserName param."}
        return HttpResponse (json.dumps(response))

    if not key:
        response["Error"] = {"ErrorCode": "100", "ErrorDescription": "Authentication error."}
        return HttpResponse (json.dumps(response))
    
    from restserver.pipture.models import PipUsers
    
    try:
        purchaser = PipUsers.objects.get(Token=key)
    except PipUsers.DoesNotExist:
        response["Error"] = {"ErrorCode": "100", "ErrorDescription": "Authentication error."}
        return HttpResponse (json.dumps(response))
    

    from restserver.pipture.models import PurchaseItems
    SEND_EP = PurchaseItems.objects.get(Description="SendEpisode")
        
    if trailer_id:
        video_url, error = get_video_url_from_episode_or_trailer (id=trailer_id, type_r="T")
        if error:
            response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There is error: %s." % (error)}
            return HttpResponse (json.dumps(response))
        else:
            u_url = new_send_message (user=purchaser, video_id=trailer_id, message=message, video_type="T", user_name=user_name, screenshot_url=(screenshot_url or ''))
            response['MessageURL'] = "/videos/%s/" % (u_url)
            response['Balance'] = "%s" % (purchaser.Balance)
            return HttpResponse (json.dumps(response))
    
    from restserver.pipture.models import TimeSlots
    from restserver.pipture.models import TimeSlotVideos
   
    if timeslot_id and TimeSlots.timeslot_is_current(timeslot_id) and TimeSlotVideos.is_contain_id (timeslot_id, episode_id, "E"):
        u_url = new_send_message (user=purchaser, video_id=episode_id, message=message, video_type="E", user_name=user_name, screenshot_url=(screenshot_url or ''))
        response['MessageURL'] = "/videos/%s/" % (u_url)
        response['Balance'] = "%s" % (purchaser.Balance)
        return HttpResponse (json.dumps(response))

    from restserver.pipture.models import UserPurchasedItems

    video_url, error = get_video_url_from_episode_or_trailer (id=episode_id, type_r="E")
    if error:
        response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There is error: %s." % (error)}
        return HttpResponse (json.dumps(response))


    is_purchased = UserPurchasedItems.objects.filter(UserId=purchaser, ItemId=episode_id, PurchaseItemId = SEND_EP).count()
        
    if is_purchased:
        u_url = new_send_message (user=purchaser, video_id=episode_id, message=message, video_type="E", user_name=user_name, screenshot_url=(screenshot_url or ''))
        response['MessageURL'] = "/videos/%s/" % (u_url)
        response['Balance'] = "%s" % (purchaser.Balance)
        return HttpResponse (json.dumps(response))

    else:
        if (purchaser.Balance - SEND_EP.Price) >= 0:
            new_p = UserPurchasedItems(UserId=purchaser, ItemId=episode_id, PurchaseItemId = SEND_EP, ItemCost=SEND_EP.Price)
            new_p.save()
            purchaser.Balance = Decimal (purchaser.Balance - SEND_EP.Price)
            purchaser.save()
            u_url = new_send_message (user=purchaser, video_id=episode_id, message=message, video_type="E", user_name=user_name, screenshot_url=(screenshot_url or ''))
            response['MessageURL'] = "/videos/%s/" % (u_url)
            response['Balance'] = "%s" % (purchaser.Balance)
            return HttpResponse (json.dumps(response))
        else:
            response["Error"] = {"ErrorCode": "3", "ErrorDescription": "Not enough money."}
            return HttpResponse (json.dumps(response))
Esempio n. 2
0
def getVideo (request):
    keys = request.GET.keys()
    response = {}
    if "API" not in keys:
        response["Error"] = {"ErrorCode": "666", "ErrorDescription": "There is no API parameter."}
        return HttpResponse (json.dumps(response))
    else:
        api_ver = request.GET.get("API")
    if api_ver != "1":
        response["Error"] = {"ErrorCode": "777", "ErrorDescription": "Wrong API version."}
        return HttpResponse (json.dumps(response))
    else:
        response["Error"] = {"ErrorCode": "", "ErrorDescription": ""}
    timeslot_id = request.GET.get('TimeslotId', None)
    episode_id = request.GET.get('EpisodeId', None)
    trailer_id = request.GET.get('TrailerId', None)
    force_buy = request.GET.get('ForceBuy', None)
    purchaser = request.GET.get('Key', None)
    
    from restserver.pipture.models import TimeSlots
    from restserver.pipture.models import TimeSlotVideos

    
    if force_buy and not purchaser:
        response["Error"] = {"ErrorCode": "4", "ErrorDescription": "Wrong authentication error"}
        return HttpResponse (json.dumps(response))
    if not (timeslot_id or force_buy) and not trailer_id:
        response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There are TimeslotId and TrailerId. Should be only one param."}
        return HttpResponse (json.dumps(response))
    if episode_id and trailer_id:
        response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There are EpisodeId and TrailerId. Should be only one param."}
        return HttpResponse (json.dumps(response))
    if not episode_id and not trailer_id:
        response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There are no EpisodeId or TrailerId. Should be one param."}
        return HttpResponse (json.dumps(response))
    if trailer_id and not timeslot_id:
        video_url, error = get_video_url_from_episode_or_trailer (id = trailer_id, type_r = "T")
        if error:
            response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There is error: %s." % (error)}
            return HttpResponse (json.dumps(response))
        response['VideoURL'] = video_url
        return HttpResponse (json.dumps(response))

    elif timeslot_id:

        if episode_id:
            video_type = "E"
        else:
            video_type = "T"
        
        if TimeSlots.timeslot_is_current(timeslot_id) and TimeSlotVideos.is_contain_id (timeslot_id, episode_id or trailer_id, video_type):
            video_url, error = get_video_url_from_episode_or_trailer (id = episode_id or trailer_id, type_r = video_type)
            if error:
                response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There is error: %s." % (error)}
                return HttpResponse (json.dumps(response))
            response['VideoURL'] = video_url
            return HttpResponse (json.dumps(response))
        else:
            response["Error"] = {"ErrorCode": "1", "ErrorDescription": "Timeslot expired"}
            return HttpResponse (json.dumps(response))


    else:
        force_buy = force_buy or "0"
        if force_buy not in ["1", "0"]:
            response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There is wrong ForceBuy %s (not 0 or 1)" % (force_buy)}
            return HttpResponse (json.dumps(response))

        from restserver.pipture.models import PipUsers
        
        try:
            purchaser = PipUsers.objects.get(Token=purchaser)
        except PipUsers.DoesNotExist:
            response["Error"] = {"ErrorCode": "100", "ErrorDescription": "Authentication error."}
            return HttpResponse (json.dumps(response))

        from restserver.pipture.models import UserPurchasedItems
        from restserver.pipture.models import PurchaseItems
        WATCH_EP = PurchaseItems.objects.get(Description="WatchEpisode")
        SEND_EP = PurchaseItems.objects.get(Description="SendEpisode")

        
        
        is_purchased = UserPurchasedItems.objects.filter(UserId=purchaser, ItemId=episode_id, PurchaseItemId = WATCH_EP).count()
        
        video_url, error = get_video_url_from_episode_or_trailer (id = episode_id, type_r = "E")
        if error:
            response["Error"] = {"ErrorCode": "888", "ErrorDescription": "There is internal error. Wrong video URL"}
            return HttpResponse (json.dumps(response))
            
        
        if is_purchased:
            response['VideoURL'] = video_url
            response['Balance'] = "%s" % (purchaser.Balance)
            return HttpResponse (json.dumps(response))
        else:
            if force_buy == "0":
                response["Error"] = {"ErrorCode": "2", "ErrorDescription": "Video not purchased."}
                return HttpResponse (json.dumps(response))
            else:
                if (purchaser.Balance - WATCH_EP.Price) >= 0:
                    new_p = UserPurchasedItems(UserId=purchaser, ItemId=episode_id, PurchaseItemId = WATCH_EP, ItemCost=WATCH_EP.Price)
                    new_p.save()
                    purchaser.Balance = Decimal (purchaser.Balance - WATCH_EP.Price)
                    purchaser.save()
                    response['VideoURL'] = video_url
                    response['Balance'] = "%s" % (purchaser.Balance)
                    return HttpResponse (json.dumps(response))
                else:
                    response["Error"] = {"ErrorCode": "3", "ErrorDescription": "Not enough money."}
                    return HttpResponse (json.dumps(response))