Example #1
0
def parsePage(ilt,html, itemtype):
    global index_global
    try:

        plt=re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
        tlt=re.findall(r'\"raw_title\"\:\".*?\"', html)
        purl=re.findall(r'\"pic_url\"\:\".*?\"', html)
        detail_url = re.findall(r'\"detail_url\"\:\".*?\"', html)
        sales = re.findall(r'\"view_sales\"\:\".*?\"', html)

        # item = Item(item_id=iid,
        #             title=tlt,
        #             price=float(plt),
        #             pic_url=purl,
        #             pic_file=f'data/{itemtype}/{itemtype}_{index_global}.jpg',
        #             sales=sales,
        #             type=itemtype)


        try:
            os.makedirs(f'../data/{itemtype}')
        except:
            pass

        for i in range(len(plt)):
            price=eval(plt[i].split(':')[1])
            title=eval(tlt[i].split(':')[1])
            pic_url = 'http:' + eval(purl[i].split(':')[1])
            sale = eval(sales[i].split(':')[1])
            sale = re.findall(r'\d+', sale)
            sale = int(''.join(sale))
            # ilt.append([price, title, pic_url, itemtype])
            try:
                iid = str(file_dict[itemtype]) + '_' + str(index_global)
                file = f'data/{itemtype}/{itemtype}_{index_global}.jpg'
                # sale = np.random.choice(10000, 1)[0]


                item = Item(item_id=iid,
	                        title=title,
	                        price=float(price),
	                        pic_url=pic_url,
	                        pic_file=file,
	                        sales=sale,
	                        type=itemtype)
                item.save()
                print([iid, price, title, pic_url, itemtype, sale, file])

                urllib.request.urlretrieve(pic_url,f'../data/{itemtype}/{itemtype}_{index_global}.jpg')
                img = Image.open(f'../data/{itemtype}/{itemtype}_{index_global}.jpg')
                out = img.resize((200, 200))
                out.save(f'../static/data/{itemtype}/{itemtype}_{index_global}.jpg', 'jpeg')
                ilt.append([index_global, price, title, pic_url, itemtype, sales])
                # print([index_global, price, title, pic_url, itemtype, sales])
                index_global += 1
                time.sleep(np.random.choice(5, 1))
            except:
                print(f'{title}数据保存失败')
    except:
        return ""
Example #2
0
 def test_add_inventory(self):
     """
     Tests adding two items to the player's inventory.
     """
     nickname = "Bob"
     cnt_a = 42
     cnt_b = 47
     item_a = Item(name="Banapple", description="Is it a Banana? An Apple? No one cares!")
     item_b = Item(name="Apprange", description="Turns out you can compare Apples and Oranges.")
     # Create player and existing items
     Player(nickname=nickname).save()
     item_a.save()
     item_b.save()
     # Save the items.
     test_player = Player.objects.get(nickname=nickname)
     test_item_a = Item.objects.get(name=item_a.name)
     test_item_b = Item.objects.get(name=item_b.name)
     inventory_a = Inventory(item_owner=test_player, item_type=test_item_a, count=cnt_a)
     inventory_a.save()
     inventory_b = Inventory(item_owner=test_player, item_type=test_item_b, count=cnt_b)
     inventory_b.save()
     # Get the items.
     inv = test_player.inventory
     # Check the results.
     self.assertEqual(inv[0], inventory_a)
     self.assertEqual(inv[1], inventory_b)
Example #3
0
def add_item_service(request):
    """Our bookmarklet posts to here"""

    bookmarklet_key_id = request.POST['bookmarklet_key']
    try:
        bookmarklet_key = Contributor.objects.get(key=bookmarklet_key_id)
    except Contributor.DoesNotExist:
        return render_to_response('bookmarklet_denied.html')
    if not bookmarklet_key.is_active:
        return render_to_response('bookmarklet_denied.html')

    description = request.POST["description"]
    link = request.POST["link"]
    description = request.POST["description"]
    contributor = bookmarklet_key.display_name

    item = Item(
        contributor=bookmarklet_key,
        link=link,
        description=description,
    )

    item.save()

    return HttpResponse('success', status=200)
Example #4
0
def new_form(request):
    if request.method == 'POST':
        if request.FILES['image'].size > 10 * (1 << 20):
            template = loader.get_template('items/new.html')
            context = {
                'committees': Committee.objects.order_by('name'),
                'error': 'Your image file is too large. Maximum size is 20MB',
            }
            return HttpResponse(template.render(context, request))
        item = Item(
            desc = request.POST['desc'],
            event = request.POST['event'],
            committee = Committee.objects.get(name=request.POST['committee']),
            cost = request.POST['cost'],
            date_purchased = Item.parseDate(request.POST['date']),
            details = request.POST['details'],
            date_filed = timezone.now(),
            created_by = request.user,
            status = Item.NEW,
            image = request.FILES['image'],
        )

        item.save()

        item.mail_com_chair()

        return HttpResponseRedirect('/items')

    else:
        template = loader.get_template('items/new.html')
        context = {
            'committees': Committee.objects.order_by('name'),
        }

        return HttpResponse(template.render(context, request))
Example #5
0
def add_items_from_file(file_path):
    xml_file = etree.ElementTree(file=file_path)
    xml_tag_list = xml_file.getroot()
    for xml_tag_item in xml_tag_list:
        for key in xml_tag_item.attrib:
            if key == 'id':
                atr_id = xml_tag_item.attrib[key]
            elif key == 'name':
                atr_name = xml_tag_item.attrib[key]
            elif key == 'type':
                atr_type = xml_tag_item.attrib[key]
            else:
                print('Item atribut : %s in tag : %s not parsed.' %
                      (key, xml_tag_item.tag))

        new_item = Item(id=atr_id, name=atr_name, type=atr_type)

        new_item.save()

        for i in xml_tag_item:
            if i.tag == 'set':
                new_param = ItemParam(item=new_item,
                                      name=i.attrib['name'],
                                      value=i.attrib['val'])

                new_param.save()

            elif i.tag == 'for':
                pass
            elif i.tag == 'cond':
                pass
            else:
                print(
                    'Item: %s with id: %s have non parsed tag: %s' %
                    (xml_tag_item.get('name'), xml_tag_item.get('id'), i.tag))
Example #6
0
    def save(self, commit=True):
        for form in self.forms:
            print(form.cleaned_data)
            name = form.cleaned_data.get('name')
            description = form.cleaned_data.get('description', '')

            item = Item(name=name, description=description)
            item.save()
        return self.forms
Example #7
0
 def create_item(self, seed, seller, price=12.00):
     """
     Creates an Item instance and returns it.
     """
     item = Item(title='Item{}'.format(seed),
                 notes='Some notes.',
                 seller=seller,
                 price=price)
     item.save()
     return item
Example #8
0
 def save(self):
     items_csv = csv.reader(self.cleaned_data["file"])
     Item.objects.all().delete()
     for item_csv in items_csv:
         item = Item()
         item.item_id = item_csv[0]
         item.name = item_csv[1]
         item.category = item_csv[2]
         item.price_cash = item_csv[3]
         item.price_credit = item_csv[4]
         item.save()
Example #9
0
    def handle(self, *args, **kwargs):
        dummy_data = {
            'item_names': [
                'Do Androids Dream of Electric Sheep?',
                'The Hitchhiker\'s Guide to the Galaxy',
                'Something Wicked This Way Comes',
                'Pride and Prejudice and Zombies',
                'The Curious Incident of the Dog in the Night-Time',
                'I Was Told There\'d Be Cake', 'To Kill a Mockingbird',
                'The Unbearable Lightness of Being',
                'Eats, Shoots & Leaves: The Zero Tolerance Approach to Punctuation',
                'The Hollow Chocolate Bunnies of the Apocalypse',
                'A Clockwork Orange', 'Are You There, Vodka? It\'s Me, Chelsea'
            ],
            'item_descriptions': [
                'Brand new, never opened or used.',
                'Light usage, good condition',
                'Spilled some water on it, fair condition, but good enough',
                'Mint condition - collectors item'
            ],
            'item_ask_price': ['.1', '.23', '.51', '.233', '.47', '.09'],
            'new_items': []
        }

        for index, value in enumerate(range(kwargs['items'])):
            random_item = choice(dummy_data['item_names'])
            random_desc = choice(dummy_data['item_descriptions'])
            random_price = choice(dummy_data['item_ask_price'])
            random_user = choice(User.objects.all())

            item = Item(
                owner=random_user,
                name=random_item,
                description=random_desc,
                ask_price_xmr=random_price,
            )
            item.save()

            dummy_data['new_items'].append(item)
            self.stdout.write(
                self.style.SUCCESS(
                    f'Item "{item.name} ({item.id})" created successfully!'))

        for i in dummy_data['new_items']:
            all_users = User.objects.all().exclude(username=i.owner.username)
            for u in all_users:
                bid = ItemBid(item=i, bidder=u, bid_price_xmr=i.ask_price_xmr)
                bid.save()
                self.stdout.write(
                    self.style.SUCCESS(
                        f'Bid #{bid.id} for user "{bid.bidder}" created successfully!'
                    ))
Example #10
0
def get_or_create_item(item_name):
    """
    Get or Create an Item instance

    :param item_name: string
    :return item: Item instance
    """
    try:
        item = Item.objects.get(name=item_name)
    except Item.DoesNotExist:
        item = Item()
        item.name = item_name
        item.save()
    return item
Example #11
0
File: views.py Project: vikev/SLIP
def add( request ):
  name = request.GET.get( 'name', '' )
  mass = request.GET.get( 'mass', '' )

  if name and mass:
    item = Item()
    item.name = name
    item.mass = mass
    item.item_id = len( Item.objects.all() )
    item.save()
    return HttpResponseRedirect( '/items/' )

  item = Item.objects.all()
  return render( request, 'items/add.html' )
Example #12
0
    def save(self, *args, **kwargs):
        f = io.TextIOWrapper(self.file.file.file, encoding='UTF-8')
        transactions = csv.reader(f)
        itemDict = refreshItemPrices()

        for line in transactions:
            input_data = Transaction()
            input_data.error_message = ""
            #input_data.time = datetime.strptime(line[4], "%Y/%m/%d %H:%M:%S")
            input_data.time = datetime.fromtimestamp(int(
                line[4])).strftime("%Y-%m-%d %H:%M:%S")
            char = Character.objects.filter(name=line[8].strip())
            if char.exists():
                input_data.character = char.first()
            else:
                character = Character(name=line[8].strip())
                character.save()
                input_data.character = character
                input_data.error_message += "No existing character. "
            input_data.type = line[7].strip()
            item = Item.objects.filter(itemId=line[10])
            if item.exists():
                input_data.item = item.first()
            else:
                new_item = Item(itemId=line[10], itemName=line[9].strip())
                itemId = int(line[10])
                if itemId in itemDict['items'].keys():
                    new_item.points = itemDict['items'][itemId].get(
                        'MarketValue')
                else:
                    input_data.error_message = "Item ID " + line[
                        10] + " not seen before, and not present in TSM response"
                new_item.save()
                input_data.item = new_item
            if input_data.item.itemId == 0:
                #Its gold. Divide by 10000 to switch from gold val to copper val
                #As per terra, round up.
                if int(line[11]) < 10000:
                    input_data.quantity = 1
                elif int(line[11]) % 10000 != 0:
                    input_data.quantity = (int(line[11]) / 10000) + 1
                else:
                    input_data.quantity = int(line[11]) / 10000
            else:
                input_data.quantity = int(line[11])
            input_data.save()
            input_data.process()

        super(TransactionFile, self).save(args, kwargs)
Example #13
0
def populate_items():
    items = populate_instance(Item, API_ITEMS_URL, 'items')

    for item in items:
        try:
            Item.objects.get(pk=item['id'])
            continue
        except Item.DoesNotExist:
            new_instance = Item()
            new_instance.id = item['id']
            new_instance.name = item['name']
            new_instance.localized_name = item['localized_name']
            new_instance.is_recipe = item['recipe']
            new_instance.cost = item['cost']
            new_instance.save()
Example #14
0
def admin_items(request):
    item_array = []
    
    if request.method == 'POST':
        new_items = request.POST.get('new_items')
        item_array = new_items.split('\r\n')
        for i in item_array:
            #logging.debug(i)
            try:
                iarray = i.split(',')
                item = Item(name=iarray[0].strip(),quantity=iarray[1].strip(),price=Decimal(iarray[2].strip()))
                item.save()
            except IndexError:
                pass
    template = 'items/admin/admin_item.html'
    data = {}
    
    return render_to_response(template, data,context_instance=RequestContext(request))
Example #15
0
 def test_update_inventory(self):
     nickname = "Bob"
     cnt = 42
     new_cnt = 47
     name = "Banapple"
     description = "Is it a Banana? An Apple? No one cares!"
     # Create player and existing item
     Player(nickname=nickname).save()
     item = Item(name=name, description=description)
     item.save()
     test_player = Player.objects.get(nickname=nickname)
     test_item = Item.objects.get(name=name)
     inventory = Inventory(item_owner=test_player, item_type=test_item, count=cnt)
     inventory.save()
     # Update inventory.
     inv = test_player.inventory[0]
     inv.count = new_cnt
     inv.save()
     inv = test_player.inventory[0]
     # Check if the count of the item is correct.
     self.assertTrue(inv.count == new_cnt)
Example #16
0
    def post(self, req, *args, **kwargs):
        uForm = UploadForm(req.POST, req.FILES)
        #if uForm.is_valid():
        item = Item()
        item.title = uForm.data["title"]
        item.description = uForm.data["description"]
        item.dimension = uForm.data["dimension"]
        item.author = uForm.data["author"]
        item.year = uForm.data["year"]
        item.type = uForm.data["type"]
        item.source = uForm.data["source"]
        item.style = uForm.data["style"]
        item.n_copies = uForm.data["n_copies"]
        item.created = uForm.data["created"]
        item.updated = uForm.data["updated"]
        user_id = uForm.data["user_id"]
        item.user = User.objects.get(id=user_id)

        folder = os.path.join('photos', item.user.username)
        file = req.FILES.get('file')
        fname = file.name
        fpath = os.path.join(settings.MEDIA_ROOT, folder)

        if not os.path.exists(fpath):
            os.makedirs(fpath)
        # save the uploaded file inside that folder.
        full_filename = os.path.join(fpath, fname)

        if not os.path.exists(full_filename):
            fout = open(full_filename, 'wb+')
            fout.write(file.read())
            fout.close()

            item.fpath = os.path.join(folder, fname)
            item.save()
        else:
            return JsonResponse({'saved': False}, safe=False)
        return JsonResponse({'saved': True}, safe=False)
Example #17
0
def add_item_service(request):
    """Our bookmarklet posts to here"""
    
    bookmarklet_key_id = request.POST['bookmarklet_key']
    try:
        bookmarklet_key = Contributor.objects.get(key=bookmarklet_key_id)
    except Contributor.DoesNotExist:
        return render_to_response('bookmarklet_denied.html')
    if not bookmarklet_key.is_active:
        return render_to_response('bookmarklet_denied.html')
        
    description = request.POST["description"]
    link = request.POST["link"]
    description = request.POST["description"]
    contributor = bookmarklet_key.display_name
    
    item = Item(contributor=bookmarklet_key,
                link=link,
                description=description,)
    
    item.save()
    
    return HttpResponse('success', status=200)
Example #18
0
    def handle(self, *args, **kwargs):
        """Loop thru Enum object ItemType and create sample data

        Eg. For total = 10 will create 10 sample Item data into db
        """
        current_time = timezone.now()
        for x in ItemType:
            prefix = x.name[:1]
            for i in range(0, kwargs['total']):
                item = Item()
                item.sku = prefix + str(randint(1000000, 9999999))
                item.name = x.name[:3] + str(randint(1, 99909990))
                item.item_type = x.value
                item.active_timestamp = current_time
                item.expiry_timestamp = current_time + timedelta(days=5)

                item.save()

                price = Price()
                price.item = item
                price.price = uniform(10.00, 200.00)
                price.effective_timestamp = current_time - timedelta(days=10)

                price.save()
Example #19
0
File: views.py Project: acutexyz/cx
def add(request):
	if request.method == 'POST':
		form = ItemAddForm(request.POST)
		if form.is_valid():
			# i = Item(**form.cleaned_data)
			i = Item(
				name=form.cleaned_data['name'],
				date_created=timezone.now(),
				created_by=CustomUser(id=request.user.id),
				category=form.cleaned_data['category']
			)
			duplicates_list = i.get_similar_item_ids()
			if len(duplicates_list) > 0:
				possible_duplicates = ItemPossibleDuplicates(str_list=str(duplicates_list), number=len(duplicates_list))
				possible_duplicates.save()
				i.possible_duplicates = possible_duplicates
			i.save()
			return HttpResponseRedirect(reverse('items.views.view', kwargs={'item_id': i.id}))
	else:
		form = ItemAddForm() # An unbound form

	return render(request, 'items/add.html', {
		'form': form,
	})
Example #20
0
def add(request):
    if request.method == 'POST':
        form = ItemAddForm(request.POST)
        if form.is_valid():
            # i = Item(**form.cleaned_data)
            i = Item(name=form.cleaned_data['name'],
                     date_created=timezone.now(),
                     created_by=CustomUser(id=request.user.id),
                     category=form.cleaned_data['category'])
            duplicates_list = i.get_similar_item_ids()
            if len(duplicates_list) > 0:
                possible_duplicates = ItemPossibleDuplicates(
                    str_list=str(duplicates_list), number=len(duplicates_list))
                possible_duplicates.save()
                i.possible_duplicates = possible_duplicates
            i.save()
            return HttpResponseRedirect(
                reverse('items.views.view', kwargs={'item_id': i.id}))
    else:
        form = ItemAddForm()  # An unbound form

    return render(request, 'items/add.html', {
        'form': form,
    })
Example #21
0
def add(request):
#    user, item = get_user_profile(request.user.id)
    form = ItemForm()
    if request.method == "POST":
        form = ItemForm(request.POST, request.FILES)
        if form.is_valid():
            form.cleaned_data['seller'] = request.user
            data = form.cleaned_data
            image1 = data['image1']
            image2 = data['image2']
            image3 = data['image3']
            if not (is_image(image1) and is_image(image2) and is_image(image3)):
                request.flash['message'] = "Sorry please upload a picture in an acceptable format"
                return HttpResponseRedirect(reverse('add_item'))
            item = Item(
                name=data['name'],
                description=data['description'],
                condition=data['condition']or 0,
                price=round(float(data['price']),2),
                longitude=data['longitude'] or 0,
                latitude=data['latitude'] or 0,
                seller=request.user,
                category=data['category'],
                location=data['location'],
                is_published = True,
                is_service = data['is_service']
            )

            item_obj = item.save()
            upload_item_images(image1, image2, image3, item)
            request.flash['message'] = "Item saved successfully"

            #            else:
            #                request.flash['message'] = "Sorry can't save item"
            return HttpResponseRedirect(reverse('my_listing'))
        else:
            request.flash['message'] = "Invalid data"

    return render_to_response("items/add.html", {'form': form}, context_instance=RequestContext(request))
Example #22
0
    def post(self):

        form = AddItemForm()
        item = Item()

        categories = Category.objects.all()
        licenses = License.objects.all()
        form.set_categories(categories, g.lang)
        form.set_licenses(licenses)

        if form.validate_on_submit():
            # first, the user has to share something !
            if not form.github.data and not form.files.data:
                flash('Neither a repo URL nor files has been shared, come on!',
                      category='alert')
                return render_template('items/add_item.html', form=form)

            # give that something at least one title
            if not form.ar_title.data and not form.fr_title.data and \
               not form.en_title.data:

                flash('You need to give this item at least one title, \
                       just pick one language and name it!',
                      category='alert')
                return render_template('items/add_item.html', form=form)

            # now we can proceed
            ar_title = Title()
            fr_title = Title()
            en_title = Title()

            ar_title.title = form.ar_title.data.strip()
            ar_title.lang = 'ar'

            fr_title.title = form.fr_title.data.strip()
            fr_title.lang = 'fr'

            en_title.title = form.en_title.data.strip()
            en_title.lang = 'en'

            item.titles.append(ar_title)
            item.titles.append(fr_title)
            item.titles.append(en_title)

            item.description = form.description.data
            item.tags = form.tags.data.strip().split(',')
            item.category = Category.objects.get(category_id=
                                                 int(form.category.data))

            item.submitter = User.objects.get(id=current_user.id)

            thumbnail = request.files['thumbnail']
            thumbnail_name = secure_filename(thumbnail.filename)

            if thumbnail and allowed_thumbnails(thumbnail_name):
                ext = thumbnail.mimetype.split('/')[-1]
                # use the 'thumbnail' name for all thumbnails
                filename = '.'.join(["thumbnail", ext])
                item.thumbnail.put(thumbnail.stream,
                                   content_type=thumbnail.mimetype,
                                   filename=filename)

            if form.github.data:
                item.github = form.github.data
                item.save()
                # no need to process any uploaded files
                flash('Item submitted successfully', category='success')
                return redirect(url_for('items.detail', item_id=item.item_id))
            else:
                item.license = License.objects.get(license_id=
                                                   int(form.license.data))

        else:
            flash('upload unsuccessful', category='error')
            return render_template('items/add_item.html', form=form)

        uploaded_files = request.files.getlist("files")
        for file in uploaded_files:
            # Make the filename safe, remove unsupported chars
            filename = secure_filename(file.filename)
            # Check if the file is one of the allowed types/extensions
            if file and allowed_file(filename):
                # put the file in the ListField.
                # see https://gist.github.com/tfausak/1299339
                file_ = GridFSProxy()
                file_.put(file.stream,
                          content_type=file.mimetype,
                          filename=filename)
                item.files.append(file_)
        # Save the thing
        item.save()
        flash('Item uploaded successfully', category='success')
        return redirect(url_for('items.detail', item_id=item.item_id))
Example #23
0
def add_item(uuid, created_at):
    time.sleep(3)
    item = Item(uuid=uuid, created_at=created_at)
    item.save()
Example #24
0
def add(request):
	user = request.session.get('user', None)
	
	if user != 'admin':
		request.session['referer'] = reverse('add')
		return redirect(reverse('admin_login'))
	
	if request.is_ajax():
		form_title   = request.POST.get("title", None)
		instructions = request.POST.get("instructions", None)
		logo         = request.POST.get("logo", None)
		logo_file    = request.FILES.get('logo-new-file', None)			
		email        = request.POST.get("email", None)
		gym          = request.POST.get("gym", None)
		forSale      = request.POST.get("for-sale", None)
		
		val = Validate()		
		val.isEmpty(form_title, "Please enter Form Title.")
		val.isEmpty(instructions, "Please enter Form Instructions.")
		val.isEmpty(logo, "Please select Form Logo.")
		
		if logo == "new":			
			val.isImage(logo_file, "New Logo Image")
		
		val.isEmail(email, "Please enter valid Customer Email.")
		val.isEmpty(gym, "Please enter Customer Gym.")
		items = []
		
		if (forSale):
			for i in range(1, int(forSale)+1):
				order = i
				title = request.POST.get("item-title-"+str(i), None)
				price = request.POST.get("item-price-"+str(i), None)
				image = request.FILES.get('item-image-file-'+str(i), None)
				sizes = request.POST.get("item-sizes-"+str(i), None)

				val.isEmpty(title, "Please enter Item "+str(i)+" Title.")
				val.isInteger(price, "Please enter valid Item "+str(i)+" Price.")				
				val.isImage(image, "Item "+str(i)+" Image")
				val.isEmpty(sizes, "Please enter Item "+str(i)+" Sizes.")
				
				item       = Item()
				item.order = order
				item.title = title
				item.price = price
				item.image = image
				item.sizes = sizes
				item.form  = None

				items.append(item)
		
		errors = val.getErrors()
		
		if errors:
			return JsonResponse(errors, safe=False)
		else:
			form              = Form()
			form.title        = form_title
			form.instructions = instructions
			form.logo         = logo_file if logo_file else logo
			form.email        = email
			form.gym          = gym
			form.forSale      = forSale
			form.save()
			
			for item in items:
				item.form = form
				item.save()				
		
			return JsonResponse({"url": reverse('forms')})
	else:
		files  = []
		bucket = get_bucket()
		source = "https://%s.s3.%s.amazonaws.com/" %(bucket.name, settings.AWS_S3_REGION_NAME)
		
		for obj in bucket.objects.filter(Prefix='logos'):
			files.append(obj.key)
		
		return render(request, 'forms/add.html', {'source': source, 'files': files})