Exemplo n.º 1
0
def add_sample_log(log_sample_data, album_sample_data, city_sample_data, user_sample_data):
    log = Log()
    log.city = City.objects.get(name=city_sample_data['name'])
    log.latitude = log_sample_data['latitude']
    log.longitude = log_sample_data['longitude']
    log.description = log_sample_data['description']
    user_profile = UserProfile.objects.get(user__username=user_sample_data['username'])
    log.user_profile = user_profile
    log.album = Album.objects.get(name=album_sample_data['name'], user_profile=user_profile)
    log.score = 0
    log.save()
    log_picture = LogPicture()
    log_picture.log = log
    log_picture.picture = File(get_small_image())
    log_picture.save()
Exemplo n.º 2
0
def add_sample_log(log_sample_data, album_sample_data, city_sample_data,
                   user_sample_data):
    log = Log()
    log.city = City.objects.get(name=city_sample_data['name'])
    log.latitude = log_sample_data['latitude']
    log.longitude = log_sample_data['longitude']
    log.description = log_sample_data['description']
    user_profile = UserProfile.objects.get(
        user__username=user_sample_data['username'])
    log.user_profile = user_profile
    log.album = Album.objects.get(name=album_sample_data['name'],
                                  user_profile=user_profile)
    log.score = 0
    log.save()
    log_picture = LogPicture()
    log_picture.log = log
    log_picture.picture = File(get_small_image())
    log_picture.save()
Exemplo n.º 3
0
def create_log(request):
    """
    Creates a new log using the provided POST data. First, the
    provided POST data is validated, if no errors are returned,
    then a log is created successfully. Also, new LogPicture is
    created for each of the pictures in the POST data. Also note
    that this view only accepts ajax requests, else a 404 error
    is raised.
    """
    if request.is_ajax():
        user = request.user
        return_data = {}
        if user.is_authenticated():
            post_data = request.POST
            file_data = request.FILES
            location = post_data.get('location', '')
            if ',' in location:
                location = location[0:location.index(',')]
            latitude = post_data.get('latitude', '')
            longitude = post_data.get('longitude', '')
            album_name = post_data.get('album_name', '')
            description = post_data.get('description', '')

            # validate log data
            error = validate_add_log_form(location, latitude, longitude, description, file_data)
            if error is None:
                # get user_profile, album and city associated with this log
                user_profile = UserProfile.objects.get(user=user)
                city = City.objects.get(name=location)
                if album_name != "None":
                    album = Album.objects.get(name=album_name, user_profile=user_profile)
                else:
                    album = None

                # create a new log
                new_log = Log()
                new_log.user_profile = user_profile
                new_log.city = city
                new_log.latitude = latitude
                new_log.longitude = longitude
                new_log.album = album
                new_log.description = description
                new_log.score = 0
                new_log.save()

                # now that we have created_at, update the log score and re-save
                new_log.score = new_log.get_log_score()
                new_log.save()

                # create new log picture for every image submitted by user
                for key, image_file in file_data.iteritems():
                    new_log_picture = LogPicture()
                    new_log_picture.log = new_log
                    new_log_picture.picture = image_file
                    new_log_picture.save()

                # finally, update user travel stats
                user_profile.update_user_travel_stats()

            else:
                return_data['error'] = error
        else:
            return_data['redirect_to'] = '/mytravelog/sign_in/'

        return_data = json.dumps(return_data)
        mimetype = "application/json"
        return HttpResponse(return_data, mimetype)

    else:
        raise Http404
Exemplo n.º 4
0
def edit_log(request, log_id):
    """
    Updates the log with the provided id, using the POST data provided.
    It also checks if the log actually belongs to the current user. If
    this is not the case, then an error message is returned. Else, the
    log is updated successfully. Also, the LogPicture instances are deleted
    based on the on the delete_picture_ids list in POST data. Also note that
    this view only accepts ajax requests, else a 404 error is raised.
    """
    if request.is_ajax():
        user = request.user
        return_data = {}
        if user.is_authenticated():
            post_data = request.POST
            file_data = request.FILES
            album_name = post_data.get('album_name', '')
            description = post_data.get('description', '')
            # get list of picture ids to remove and remove empty string at the end
            delete_picture_ids = post_data.get('images_to_delete', '').split(',')
            delete_picture_ids.pop()

            # get log that has to be edited along with all the pictures associated with it
            log_to_edit = Log.objects.get_log_by_id(log_id)
            if log_to_edit.user_profile.user == user:
                log_pictures = LogPicture.objects.filter(log=log_to_edit)

                # validate log data
                error = validate_edit_log_form(description, len(delete_picture_ids), file_data, len(log_pictures))
                if error is None:
                    # update existing log
                    user_profile = UserProfile.objects.get(user=user)
                    if album_name != "None":
                        log_to_edit.album = Album.objects.get(name=album_name, user_profile=user_profile)
                    else:
                        log_to_edit.album = None
                    log_to_edit.description = description
                    log_to_edit.save()

                    # remove log pictures requested by user
                    for picture_id in delete_picture_ids:
                        LogPicture.objects.get(id=picture_id).delete()

                    # create new log picture for every image submitted by user
                    for key, image_file in file_data.iteritems():
                        new_log_picture = LogPicture()
                        new_log_picture.log = log_to_edit
                        new_log_picture.picture = image_file
                        new_log_picture.save()

                else:
                    return_data['error'] = error
            else:
                return_data['error'] = "This log does not belong to you"
        else:
            return_data['redirect_to'] = '/mytravelog/sign_in/'

        return_data = json.dumps(return_data)
        mimetype = "application/json"
        return HttpResponse(return_data, mimetype)

    else:
        raise Http404
Exemplo n.º 5
0
def create_log(request):
    """
    Creates a new log using the provided POST data. First, the
    provided POST data is validated, if no errors are returned,
    then a log is created successfully. Also, new LogPicture is
    created for each of the pictures in the POST data. Also note
    that this view only accepts ajax requests, else a 404 error
    is raised.
    """
    if request.is_ajax():
        user = request.user
        return_data = {}
        if user.is_authenticated():
            post_data = request.POST
            file_data = request.FILES
            location = post_data.get('location', '')
            if ',' in location:
                location = location[0:location.index(',')]
            latitude = post_data.get('latitude', '')
            longitude = post_data.get('longitude', '')
            album_name = post_data.get('album_name', '')
            description = post_data.get('description', '')

            # validate log data
            error = validate_add_log_form(location, latitude, longitude,
                                          description, file_data)
            if error is None:
                # get user_profile, album and city associated with this log
                user_profile = UserProfile.objects.get(user=user)
                city = City.objects.get(name=location)
                if album_name != "None":
                    album = Album.objects.get(name=album_name,
                                              user_profile=user_profile)
                else:
                    album = None

                # create a new log
                new_log = Log()
                new_log.user_profile = user_profile
                new_log.city = city
                new_log.latitude = latitude
                new_log.longitude = longitude
                new_log.album = album
                new_log.description = description
                new_log.score = 0
                new_log.save()

                # now that we have created_at, update the log score and re-save
                new_log.score = new_log.get_log_score()
                new_log.save()

                # create new log picture for every image submitted by user
                for key, image_file in file_data.iteritems():
                    new_log_picture = LogPicture()
                    new_log_picture.log = new_log
                    new_log_picture.picture = image_file
                    new_log_picture.save()

                # finally, update user travel stats
                user_profile.update_user_travel_stats()

            else:
                return_data['error'] = error
        else:
            return_data['redirect_to'] = '/mytravelog/sign_in/'

        return_data = json.dumps(return_data)
        mimetype = "application/json"
        return HttpResponse(return_data, mimetype)

    else:
        raise Http404
Exemplo n.º 6
0
def edit_log(request, log_id):
    """
    Updates the log with the provided id, using the POST data provided.
    It also checks if the log actually belongs to the current user. If
    this is not the case, then an error message is returned. Else, the
    log is updated successfully. Also, the LogPicture instances are deleted
    based on the on the delete_picture_ids list in POST data. Also note that
    this view only accepts ajax requests, else a 404 error is raised.
    """
    if request.is_ajax():
        user = request.user
        return_data = {}
        if user.is_authenticated():
            post_data = request.POST
            file_data = request.FILES
            album_name = post_data.get('album_name', '')
            description = post_data.get('description', '')
            # get list of picture ids to remove and remove empty string at the end
            delete_picture_ids = post_data.get('images_to_delete',
                                               '').split(',')
            delete_picture_ids.pop()

            # get log that has to be edited along with all the pictures associated with it
            log_to_edit = Log.objects.get_log_by_id(log_id)
            if log_to_edit.user_profile.user == user:
                log_pictures = LogPicture.objects.filter(log=log_to_edit)

                # validate log data
                error = validate_edit_log_form(description,
                                               len(delete_picture_ids),
                                               file_data, len(log_pictures))
                if error is None:
                    # update existing log
                    user_profile = UserProfile.objects.get(user=user)
                    if album_name != "None":
                        log_to_edit.album = Album.objects.get(
                            name=album_name, user_profile=user_profile)
                    else:
                        log_to_edit.album = None
                    log_to_edit.description = description
                    log_to_edit.save()

                    # remove log pictures requested by user
                    for picture_id in delete_picture_ids:
                        LogPicture.objects.get(id=picture_id).delete()

                    # create new log picture for every image submitted by user
                    for key, image_file in file_data.iteritems():
                        new_log_picture = LogPicture()
                        new_log_picture.log = log_to_edit
                        new_log_picture.picture = image_file
                        new_log_picture.save()

                else:
                    return_data['error'] = error
            else:
                return_data['error'] = "This log does not belong to you"
        else:
            return_data['redirect_to'] = '/mytravelog/sign_in/'

        return_data = json.dumps(return_data)
        mimetype = "application/json"
        return HttpResponse(return_data, mimetype)

    else:
        raise Http404