<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import get_object_or_404

from django.urls import reverse_lazy
from django.views.generic import DetailView, ListView

from company.models import Company
from .models import PartCategory, Part, BomItem
from .models import SupplierPart

from .forms import PartImageForm
from .forms import EditPartForm
from .forms import EditCategoryForm
from .forms import EditBomItemForm
from .forms import BomExportForm

from .forms import EditSupplierPartForm

from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView

from InvenTree.helpers import DownloadFile


class PartIndex(ListView):
    model = Part
    template_name = 'part/category.html'
    context_object_name = 'parts'

    def get_queryset(self):
        return Part.objects.all()  # filter(category=None)

    def get_context_data(self, **kwargs):

        context = super(PartIndex, self).get_context_data(**kwargs).copy()

        # View top-level categories
        children = PartCategory.objects.filter(parent=None)

        context['children'] = children

        return context


class PartCreate(AjaxCreateView):
    """ Create a new part
    - Optionally provide a category object as initial data
    """
    model = Part
    form_class = EditPartForm
    template_name = 'part/create.html'

    ajax_form_title = 'Create new part'
    ajax_template_name = 'modal_form.html'

    def get_data(self):
        return {
            'success': "Created new part",
        }

    def get_category_id(self):
        return self.request.GET.get('category', None)

    # If a category is provided in the URL, pass that to the page context
    def get_context_data(self, **kwargs):
        context = super(PartCreate, self).get_context_data(**kwargs)

        # Add category information to the page
        cat_id = self.get_category_id()

        if cat_id:
            context['category'] = get_object_or_404(PartCategory, pk=cat_id)

        return context

    # Pre-fill the category field if a valid category is provided
    def get_initial(self):

        initials = super(PartCreate, self).get_initial().copy()

        if self.get_category_id():
            initials['category'] = get_object_or_404(PartCategory, pk=self.get_category_id())

        return initials


class PartDetail(DetailView):
    context_object_name = 'part'
    queryset = Part.objects.all()
    template_name = 'part/detail.html'

    # Add in some extra context information based on query params
    def get_context_data(self, **kwargs):
        context = super(PartDetail, self).get_context_data(**kwargs)

        if self.request.GET.get('edit', '').lower() in ['true', 'yes', '1']:
            context['editing_enabled'] = 1
        else:
            context['editing_enabled'] = 0
<commit_msg>Add ability to 'copy' a part<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import get_object_or_404

from django.urls import reverse_lazy
from django.views.generic import DetailView, ListView
from django.forms.models import model_to_dict

from company.models import Company
from .models import PartCategory, Part, BomItem
from .models import SupplierPart

from .forms import PartImageForm
from .forms import EditPartForm
from .forms import EditCategoryForm
from .forms import EditBomItemForm
from .forms import BomExportForm

from .forms import EditSupplierPartForm

from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView

from InvenTree.helpers import DownloadFile


class PartIndex(ListView):
    model = Part
    template_name = 'part/category.html'
    context_object_name = 'parts'

    def get_queryset(self):
        return Part.objects.all()  # filter(category=None)

    def get_context_data(self, **kwargs):

        context = super(PartIndex, self).get_context_data(**kwargs).copy()

        # View top-level categories
        children = PartCategory.objects.filter(parent=None)

        context['children'] = children

        return context


class PartCreate(AjaxCreateView):
    """ Create a new part
    - Optionally provide a category object as initial data
    """
    model = Part
    form_class = EditPartForm
    template_name = 'part/create.html'

    ajax_form_title = 'Create new part'
    ajax_template_name = 'modal_form.html'

    def get_data(self):
        return {
            'success': "Created new part",
        }

    def get_category_id(self):
        return self.request.GET.get('category', None)

    # If a category is provided in the URL, pass that to the page context
    def get_context_data(self, **kwargs):
        context = super(PartCreate, self).get_context_data(**kwargs)

        # Add category information to the page
        cat_id = self.get_category_id()

        if cat_id:
            context['category'] = get_object_or_404(PartCategory, pk=cat_id)

        return context

    # Pre-fill the category field if a valid category is provided
    def get_initial(self):

        # Is the client attempting to copy an existing part?
        part_to_copy = self.request.GET.get('copy_part', None)

        if part_to_copy:
            try:
                original = Part.objects.get(pk=part_to_copy)
                initials = model_to_dict(original)
            except Part.DoesNotExist:
                initials = super(PartCreate, self).get_initial()

        else:
            initials = super(PartCreate, self).get_initial()        

        if self.get_category_id():
            initials['category'] = get_object_or_404(PartCategory, pk=self.get_category_id())

        return initials


class PartDetail(DetailView):
    context_object_name = 'part'
    queryset = Part.objects.all()
    template_name = 'part/detail.html'

    # Add in some extra context information based on query params
    def get_context_data(self, **kwargs):
        context = super(PartDetail, self).get_context_data(**kwargs)

        if self.request.GET.get('edit', '').lower() in ['true', 'yes', '1']:
            context['editing_enabled'] = 1
        else:
            context['editing_enabled'] = 0

{'fraction_non_alphanumeric': 0.09037995149555376, 'fraction_numerical': 0.004203718674211803, 'mean_word_length': 3.140562248995984, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4084200', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1732, 'n_words': 490}