Ejemplo n.º 1
0
def merge_single_file(model, form, file_category, field_name):
    from civictechprojects.models import ProjectFile
    if field_name in form.data:
        file_content = form.data.get(field_name)
        if file_content and len(file_content) > 0:
            file_json = json.loads(file_content)
            ProjectFile.replace_single_file(model, file_category, file_json)
Ejemplo n.º 2
0
 def set_avatar_at_login(sender, sociallogin, **kwargs):
     owner = sociallogin.user.contributor
     user_avatar_url = sociallogin.account.get_provider().get_avatar_url(
         sociallogin)
     if user_avatar_url:
         file_json = copy_external_thumbnail_to_s3(
             user_avatar_url, sociallogin.account.provider, owner)
         ProjectFile.replace_single_file(
             owner, FileCategory(file_json['file_category']), file_json)
Ejemplo n.º 3
0
def _upload_thumbnail(thumbnail):
    file_json = copy_external_thumbnail_to_s3(thumbnail.file_url, 'UNKNOWN',
                                              thumbnail.file_user)
    new_file_category = file_json['file_category']
    if new_file_category != FileCategory.THUMBNAIL.value:
        not_thumbnail = ProjectFile.from_json(thumbnail.file_user,
                                              FileCategory(new_file_category),
                                              file_json)
        raise NotThumbnailException(not_thumbnail)
    ProjectFile.replace_single_file(
        thumbnail.file_user,
        FileCategory.THUMBNAIL,
        file_json,
        new_file_category=FileCategory(new_file_category))
Ejemplo n.º 4
0
    def edit_user(request, user_id):
        user = Contributor.objects.get(id=user_id)

        if not (request.user.username == user.username
                or request.user.is_staff):
            raise PermissionDenied()

        project_fields_changed = False
        form = DemocracyLabUserCreationForm(request.POST)
        project_fields_changed |= read_form_field_string(
            user, form, 'first_name')
        project_fields_changed |= read_form_field_string(
            user, form, 'last_name')
        read_form_field_string(user, form, 'about_me')
        read_form_field_string(user, form, 'postal_code')
        read_form_field_string(user, form, 'country')

        Tag.merge_tags_field(user.user_technologies,
                             form.data.get('user_technologies'))

        user.save()

        links_json_text = form.data.get('user_links')
        if len(links_json_text) > 0:
            links_json = json.loads(links_json_text)
            ProjectLink.merge_changes(user, links_json)

        files_json_text = form.data.get('user_files')
        if len(files_json_text) > 0:
            files_json = json.loads(files_json_text)
            ProjectFile.merge_changes(user, files_json)

        user_thumbnail_location = form.data.get('user_thumbnail_location')
        if len(user_thumbnail_location) > 0:
            thumbnail_file_json = json.loads(user_thumbnail_location)
            project_fields_changed |= ProjectFile.replace_single_file(
                user, FileCategory.THUMBNAIL, thumbnail_file_json)

        user_resume_file = form.data.get('user_resume_file')
        if len(user_resume_file) > 0:
            user_resume_file_json = json.loads(user_resume_file)
            ProjectFile.replace_single_file(user, FileCategory.RESUME,
                                            user_resume_file_json)

        if project_fields_changed:
            user.update_linked_items()

        SubscribeUserToQiqoChat(user)
Ejemplo n.º 5
0
 def set_avatar_at_login(sender, sociallogin, **kwargs):
     owner = sociallogin.user.contributor
     user_avatar_url = sociallogin.account.get_provider().get_avatar_url(
         sociallogin)
     if user_avatar_url:
         file_json = {
             'publicUrl': user_avatar_url,
             'file_user': owner,
             'file_category': FileCategory.THUMBNAIL.value,
             'visibility': 'PUBLIC',
             'fileName':
             f'{owner.first_name}{owner.last_name}_thumbnail.{sociallogin.account.provider} avatar',
             'key': f'{sociallogin.account.provider}/{owner.username}'
         }
         ProjectFile.replace_single_file(owner, FileCategory.THUMBNAIL,
                                         file_json)
Ejemplo n.º 6
0
    def edit_user(request, user_id):
        user = Contributor.objects.get(id=user_id)

        if not request.user.username == user.username:
            raise PermissionDenied()

        form = DemocracyLabUserCreationForm(request.POST)
        user.about_me = form.data.get('about_me')
        user.postal_code = form.data.get('postal_code')
        user.country = form.data.get('country')
        user.first_name = form.data.get('first_name')
        user.last_name = form.data.get('last_name')

        Tag.merge_tags_field(user.user_technologies,
                             form.data.get('user_technologies'))

        user.save()

        links_json_text = form.data.get('user_links')
        if len(links_json_text) > 0:
            links_json = json.loads(links_json_text)
            ProjectLink.merge_changes(user, links_json)

        files_json_text = form.data.get('user_files')
        if len(files_json_text) > 0:
            files_json = json.loads(files_json_text)
            ProjectFile.merge_changes(user, files_json)

        user_thumbnail_location = form.data.get('user_thumbnail_location')
        if len(user_thumbnail_location) > 0:
            thumbnail_file_json = json.loads(user_thumbnail_location)
            ProjectFile.replace_single_file(user, FileCategory.THUMBNAIL,
                                            thumbnail_file_json)

        user_resume_file = form.data.get('user_resume_file')
        if len(user_resume_file) > 0:
            user_resume_file_json = json.loads(user_resume_file)
            ProjectFile.replace_single_file(user, FileCategory.RESUME,
                                            user_resume_file_json)

        SubscribeUserToQiqoChat(user)
Ejemplo n.º 7
0
def merge_single_file(model, form, file_category, field_name):
    """
    Merge change for a single file form field
    :param model: Model instance
    :param form: form wrapper
    :param file_category: File type
    :param field_name: field name in model and form
    :return: True if there were changes
    """
    from civictechprojects.models import ProjectFile
    field_changed = False
    if field_name in form.data:
        file_content = form.data.get(field_name)
        if file_content and len(file_content) > 0:
            file_json = json.loads(file_content)
            field_changed = ProjectFile.replace_single_file(
                model, file_category, file_json)
    return field_changed