Skip to content

Commit

Permalink
Item, Item Group and Partner Web Page generators moved from Shopping …
Browse files Browse the repository at this point in the history
…Cart to ERPNext
  • Loading branch information
anandpdoshi committed Apr 18, 2014
1 parent 603e6e2 commit 5d591eb
Show file tree
Hide file tree
Showing 24 changed files with 625 additions and 6 deletions.
2 changes: 1 addition & 1 deletion erpnext/selling/doctype/lead/test_records.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"email_id": "[email protected]",
"lead_name": "_Test Lead",
"status": "Open",
"territory": "_Test Territory Rest Of The World"
"territory": "_Test Territory"
},
{
"doctype": "Lead",
Expand Down
20 changes: 19 additions & 1 deletion erpnext/setup/doctype/item_group/item_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from frappe.utils.nestedset import NestedSet
from frappe.website.website_generator import WebsiteGenerator
from frappe.website.render import clear_cache

class ItemGroup(NestedSet, WebsiteGenerator):
nsm_parent_field = 'parent_item_group'
Expand All @@ -19,7 +20,9 @@ def validate(self):
self.parent_item_group)

def on_update(self):
super(ItemGroup, self).on_update()
NestedSet.on_update(self)
WebsiteGenerator.on_update(self)
invalidate_cache_for(self)
self.validate_name_with_item()
self.validate_one_root()

Expand All @@ -32,3 +35,18 @@ def on_trash(self):
def validate_name_with_item(self):
if frappe.db.exists("Item", self.name):
frappe.throw(frappe._("An item exists with same name ({0}), please change the item group name or rename the item").format(self.name))

def get_parent_item_groups(item_group_name):
item_group = frappe.get_doc("Item Group", item_group_name)
return frappe.db.sql("""select name, page_name from `tabItem Group`
where lft <= %s and rgt >= %s
and ifnull(show_in_website,0)=1
order by lft asc""", (item_group.lft, item_group.rgt), as_dict=True)

def invalidate_cache_for(doc, item_group=None):
if not item_group:
item_group = doc.name

for i in get_parent_item_groups(item_group):
if i.page_name:
clear_cache(i.page_name)
11 changes: 11 additions & 0 deletions erpnext/stock/doctype/item/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from frappe import msgprint, _
from frappe.utils import cstr, flt, getdate, now_datetime, formatdate
from frappe.website.website_generator import WebsiteGenerator
from erpnext.setup.doctype.item_group.item_group import invalidate_cache_for
from frappe.website.render import clear_cache

class WarehouseNotSet(frappe.ValidationError): pass

Expand Down Expand Up @@ -46,6 +48,7 @@ def validate(self):

def on_update(self):
super(Item, self).on_update()
invalidate_cache_for_item(self)
self.validate_name_with_item_group()
self.update_item_price()

Expand Down Expand Up @@ -230,6 +233,9 @@ def before_rename(self, olddn, newdn, merge=False):

def after_rename(self, olddn, newdn, merge):
super(Item, self).after_rename(olddn, newdn, merge)
if self.page_name:
invalidate_cache_for_item(self)
clear_cache(self.page_name)

frappe.db.set_value("Item", newdn, "item_code", newdn)
if merge:
Expand Down Expand Up @@ -344,3 +350,8 @@ def get_last_purchase_details(item_code, doc_name=None, conversion_rate=1.0):
})

return out

def invalidate_cache_for_item(doc):
invalidate_cache_for(doc, doc.item_group)
for d in doc.get({"doctype":"Website Item Group"}):
invalidate_cache_for(doc, d.item_group)
Empty file.
84 changes: 84 additions & 0 deletions erpnext/templates/generators/item.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{% block title %} {{ title }} {% endblock %}

{% block header %}<h2>{{ title }}</h2>{% endblock %}

{% block content %}
<div class="item-content">
{% include 'templates/includes/product_search_box.html' %}
{% include 'templates/includes/product_breadcrumbs.html' %}
<div class="product-page-content" itemscope itemtype="http://schema.org/Product">
<div class="row">
<div class="col-md-6">
{% if slideshow %}
{% include "templates/includes/slideshow.html" %}
{% else %}
{% if website_image %}
<image itemprop="image" class="item-main-image"
src="{{ website_image }}" />
{% else %}
<div class="img-area">
{% include 'templates/includes/product_missing_image.html' %}
</div>
{% endif %}
{% endif %}
</div>
<div class="col-md-6">
<h3 itemprop="name" style="margin-top: 0px;">{{ item_name }}</h3>
<p class="help">Item Code: <span itemprop="productID">{{ name }}</span></p>
<h4>Product Description</h4>
<div itemprop="description">
{{ web_long_description or description or "[No description given]" }}
</div>
<div style="min-height: 100px; margin: 10px 0;">
<div class="item-price-info" style="display: none;">
<h4 class="item-price" itemprop="price"></h4>
<div class="item-stock" itemprop="availablity"></div>
<div id="item-add-to-cart">
<button class="btn btn-primary">
<i class="icon-shopping-cart"></i> Add to Cart</button>
</div>
<div id="item-update-cart" class="input-group col-md-4" style="display: none;
padding-left: 0px; padding-right: 0px;">
<input class="form-control" type="text">
<div class="input-group-btn">
<button class="btn btn-primary">
<i class="icon-ok"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
{% if doc.get({"doctype":"Item Website Specification"}) -%}
<div class="row" style="margin-top: 20px">
<div class="col-md-12">
<h4>Specifications</h4>
<table class="table table-bordered" style="width: 100%">
{% for d in doc.get(
{"doctype":"Item Website Specification"}) -%}
<tr>
<td style="width: 30%;">{{ d.label }}</td>
<td>{{ d.description }}</td>
</tr>
{%- endfor %}
</table>
</div>
</div>
{%- endif %}
</div>
</div>
<script>
{% include "templates/includes/product_page.js" %}

$(function() {
if(window.logged_in && getCookie("system_user")==="yes") {
frappe.has_permission("Item", "{{ name }}", "write", function(r) {
frappe.require("/assets/frappe/js/frappe/website/editable.js");
frappe.make_editable($('[itemprop="description"]'), "Item", "{{ name }}", "web_long_description");
});
}
});
</script>
{% endblock %}

{% block sidebar %}{% include "templates/includes/sidebar.html" %}{% endblock %}
20 changes: 20 additions & 0 deletions erpnext/templates/generators/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals

import frappe
from erpnext.setup.doctype.item_group.item_group import get_parent_item_groups
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow

doctype = "Item"
condition_field = "show_in_website"

def get_context(context):
item_context = context.doc.as_dict()
item_context["parent_groups"] = get_parent_item_groups(context.doc.item_group) + \
[{"name":context.doc.name}]
if context.doc.slideshow:
item_context.update(get_slideshow(context.doc))

return item_context
58 changes: 58 additions & 0 deletions erpnext/templates/generators/item_group.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{% block title %} {{ title }} {% endblock %}

{% block header %}<h2>{{ title }}</h2>{% endblock %}

{% block content %}
<div class="item-group-content">
{% include 'templates/includes/product_search_box.html' %}
{% include 'templates/includes/product_breadcrumbs.html' %}
<div>
{% if slideshow %}<!-- slideshow -->
{% include "templates/includes/slideshow.html" %}
{% endif %}
{% if description %}<!-- description -->
<div itemprop="description">{{ description or ""}}</div>
{% else %}
<h3>{{ name }}</h3>
{% endif %}
</div>
<div>
{% if sub_groups %}
<hr />
<div class="row">
{% for d in sub_groups %}
<div class="col-sm-4">
<a href="{{ d.page_name }}">{{ d.name }} ({{ d.count }})</a>
</div>
{% endfor %}
</div>
<hr />
{% endif %}
{% if items %}
<div id="search-list" class="row">
{% for item in items %}
{{ item }}
{% endfor %}
</div>
{% if (items|length)==100 %}
<div class="alert alert-info info">Showing top 100 items.</div>
{% endif %}
{% else %}
<div class="alert alert-warning">No items listed.</div>
{% endif %}
</div>
</div>
<script>
$(function() {
if(window.logged_in && getCookie("system_user")==="yes") {
frappe.has_permission("Item Group", "{{ name }}", "write", function(r) {
frappe.require("/assets/frappe/js/frappe/website/editable.js");
frappe.make_editable($('[itemprop="description"]'), "Item Group", "{{ name }}", "description");
});
}
});
</script>

{% endblock %}

{% block sidebar %}{% include "templates/includes/sidebar.html" %}{% endblock %}
64 changes: 64 additions & 0 deletions erpnext/templates/generators/item_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals

import frappe
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
from erpnext.setup.doctype.item_group.item_group import get_parent_item_groups

doctype = "Item Group"
condition_field = "show_in_website"

def get_context(context):
item_group_context = context.doc.as_dict()
item_group_context.update({
"sub_groups": frappe.db.sql("""select name, page_name
from `tabItem Group` where parent_item_group=%s
and ifnull(show_in_website,0)=1""", context.docname, as_dict=1),
"items": get_product_list_for_group(product_group = context.docname, limit=100),
"parent_groups": get_parent_item_groups(context.docname),
"title": context.docname
})

if context.doc.slideshow:
item_group_context.update(get_slideshow(context.doc))

for d in item_group_context.sub_groups:
d.count = get_group_item_count(d.name)

return item_group_context

def get_product_list_for_group(product_group=None, start=0, limit=10):
child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)])

# base query
query = """select name, item_name, page_name, website_image, item_group,
web_long_description as website_description
from `tabItem` where docstatus = 0 and show_in_website = 1
and (item_group in (%s)
or name in (select parent from `tabWebsite Item Group` where item_group in (%s))) """ % (child_groups, child_groups)

query += """order by weightage desc, modified desc limit %s, %s""" % (start, limit)

data = frappe.db.sql(query, {"product_group": product_group}, as_dict=1)

return [get_item_for_list_in_html(r) for r in data]

def get_child_groups(item_group_name):
item_group = frappe.get_doc("Item Group", item_group_name)
return frappe.db.sql("""select name
from `tabItem Group` where lft>=%(lft)s and rgt<=%(rgt)s
and show_in_website = 1""", item_group.as_dict())

def get_item_for_list_in_html(context):
return frappe.get_template("templates/includes/product_in_grid.html").render(context)

def get_group_item_count(item_group):
child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)])
return frappe.db.sql("""select count(*) from `tabItem`
where docstatus = 0 and show_in_website = 1
and (item_group in (%s)
or name in (select parent from `tabWebsite Item Group`
where item_group in (%s))) """ % (child_groups, child_groups))[0][0]

29 changes: 29 additions & 0 deletions erpnext/templates/generators/partner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% block title %} {{ title }} {% endblock %}

{% block header %}<h2>{{ title }}</h2>{% endblock %}

{% block content %}
<div class="partner-content" itemscope itemtype="http://schema.org/Organization">
<div class="row">
<div class="col-md-4">
{% if logo -%}
<img itemprop="brand" src="{{ logo }}" class="partner-logo"
alt="{{ partner_name }}" title="{{ partner_name }}" />
<br><br>
{%- endif %}
<address>
{% if partner_website -%}<p><a href="{{ partner_website }}"
target="_blank">{{ partner_website }}</a></p>{%- endif %}
{% if partner_address -%}<p itemprop="address">{{ partner_address }}</p>{%- endif %}
{% if phone -%}<p itemprop="telephone">{{ phone }}</p>{%- endif %}
{% if email -%}<p itemprop="email"><span class="icon-envelope"></span> {{ email }}</p>{%- endif %}
</address>
</div>
<div class="col-md-8">
<p>{{ description }}</p>
</div>
</div>
</div>
{% endblock %}

{% block sidebar %}{% include "templates/includes/sidebar.html" %}{% endblock %}
28 changes: 28 additions & 0 deletions erpnext/templates/generators/partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.utils import filter_strip_join

doctype = "Sales Partner"
condition_field = "show_in_website"

def get_context(context):
partner_context = context.doc.as_dict()

address = frappe.db.get_value("Address",
{"sales_partner": context.doc.name, "is_primary_address": 1},
"*", as_dict=True)
if address:
city_state = ", ".join(filter(None, [address.city, address.state]))
address_rows = [address.address_line1, address.address_line2,
city_state, address.pincode, address.country]

partner_context.update({
"email": address.email_id,
"partner_address": filter_strip_join(address_rows, "\n<br>"),
"phone": filter_strip_join(cstr(address.phone).split(","), "\n<br>")
})

return partner_context
Empty file.
10 changes: 10 additions & 0 deletions erpnext/templates/includes/product_breadcrumbs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% if parent_groups and (parent_groups|length) > 1 %}
<div class="container">
<ul class="breadcrumb">
{% for ig in parent_groups[:-1] %}
<li><a href="{{ ig.page_name }}.html">{{ ig.name }}</a></li>
{% endfor %}
<li class="active">{{ parent_groups[-1].name }}</li>
</ul>
</div>
{% endif %}
Loading

0 comments on commit 5d591eb

Please sign in to comment.