internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: frappe/frappe

Issue:
PDF Filename is broken when saving if it contains UTF-8 characters
## Description of the issue
When trying to save a PDF using python 3 the filename is broken if it contains UTF-8 characters. 
## Context information (for bug reports)

**Output of `bench version`**
```
erpnext 12.x.x-develop
frappe 12.x.x-develop
fatal: Not a git repository (or any of the parent directories): .git
```

## Steps to reproduce the issue
For example 
1. create an invoice naming series called "ΤΠΥ" 
2. then create an invoice in this series
3. try to save the PDF

### Observed result
filename is broken
![image](https://user-images.githubusercontent.com/20220341/67803772-62bb6f00-fa96-11e9-8a15-827a690e3fbd.png)

### Expected result
Filename renders correctly


## Additional information

OS version / distribution, `ERPNext` install method, etc.
Ubuntu 16.04
easy install script 
Python 3


Discussion:
Let me share some additional relevant headers that I saw. 
```

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 29 Oct 2019 20:57:57 GMT
Content-Type: application/pdf
Content-Length: 43070
Connection: keep-alive
Content-Disposition: filename="ÃÂÃÂ-ÃÂÃÂÃÂ£-00003.pdf"
```

It seems that the Content-Disposition header is not correctly set for UTF8 Characters. Modern browsers will support the filename* option which can allow for a utf8 filename as well as a fallback filename. 

Could anyone point me to where this header is set in the app so I can further test? 

Never mind I have a solution in local. It's related to frappe so I'll open a pull request there. 

Resolving patch:
diff --git a/frappe/utils/response.py b/frappe/utils/response.py
--- a/frappe/utils/response.py
+++ b/frappe/utils/response.py
@@ -90,7 +90,8 @@ def as_json():
 def as_pdf():
 	response = Response()
 	response.mimetype = "application/pdf"
-	response.headers["Content-Disposition"] = ("filename=\"%s\"" % frappe.response['filename'].replace(' ', '_')).encode("utf-8")
+	encoded_filename = quote(frappe.response['filename'].replace(' ', '_'), encoding='utf-8')
+	response.headers["Content-Disposition"] = ("filename=\"%s\"" % frappe.response['filename'].replace(' ', '_') + ";filename*=utf-8''%s" % encoded_filename).encode("utf-8")
 	response.data = frappe.response['filecontent']
 	return response
 


Test patch:
Repository: frappe/frappe

Issue:
Incomplete Dependencies not updating in Link Cards
## Description of the issue

Some Link Card links on customized workspace Desk Pages are inactive when they should be active. 

## Context information (for bug reports)

**Output of `bench version`**
```
erpnext 13.0.0-dev
frappe 13.0.0-dev
```

## Steps to reproduce the issue

1. In a new installation, customize the workspace for a desk page, like Buying, before the dependencies for one of the links (like Purchase Invoice in the Buying card) have been fulfilled. (For example, add a shortcut.) 
2. Then fulfill the dependencies (for example, create at least one Supplier and at least one Item). 
3. Clear the cache, reload the Buying desk page, even log out and log back in and/or restart the server.

### Observed result

The Purchase Invoice link on the Buying card on the Buying desk page still reports "You need to create these first: Item, Supplier" and the link is inactive.

### Expected result

Once the dependencies have been created, the link should become active.

### Stacktrace / full error message

n/a - erroneous information message mentioned above.

## Additional information

Debian 4.19, fresh install of bench/frappe/erpnext using standard guides, latest develop version of all components.

It appears the "incomplete_dependencies" attribute of the links in the Link Card are being saved at the time of customization and nothing is ever clearing them. I will post a 2-line PR to fix this issue momentarily.



Discussion:

Resolving patch:
diff --git a/frappe/desk/desktop.py b/frappe/desk/desktop.py
--- a/frappe/desk/desktop.py
+++ b/frappe/desk/desktop.py
@@ -221,6 +221,8 @@ def _prepare_item(item):
 				incomplete_dependencies = [d for d in item.dependencies if not _doctype_contains_a_record(d)]
 				if len(incomplete_dependencies):
 					item.incomplete_dependencies = incomplete_dependencies
+				else:
+					item.incomplete_dependencies = ""
 
 			if item.onboard:
 				# Mark Spotlights for initial


Test patch:
Repository: frappe/frappe

Issue:
SMTP: Exception Handling Resolution Order
In `frappe/frappe/email/smtp.py` In `SMTPServer.sess`:
`		try:`
`				....`
`		except `_socket.error` as e:     `
`			....      `
`		except smtplib.SMTPAuthenticationError as e:     `
`			....   `
`		except smtplib.SMTPException:    `
`			....   `
`

Where:
`_socket.error` is `OSError` Which is defined: `class OSError(Exception):`
`class SMTPException(OSError):`
`class SMTPResponseException(SMTPException):`
`class SMTPAuthenticationError(SMTPResponseException):`

From the python documentation:

> A class in an except clause is compatible with an exception if it is the same class or a base class thereof (but not the other way around — an except clause listing a derived class is not compatible with a base class).

So the way the except clauses are ordered now will always be handled by the `except` clause with `_socket.error` no matter what the error is.


Discussion:

Resolving patch:
diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py
--- a/frappe/email/smtp.py
+++ b/frappe/email/smtp.py
@@ -225,12 +225,6 @@ def sess(self):
 
 			return self._sess
 
-		except _socket.error as e:
-			# Invalid mail server -- due to refusing connection
-			frappe.msgprint(_('Invalid Outgoing Mail Server or Port'))
-			traceback = sys.exc_info()[2]
-			raise_(frappe.ValidationError, e, traceback)
-
 		except smtplib.SMTPAuthenticationError as e:
 			frappe.msgprint(_("Invalid login or password"))
 			traceback = sys.exc_info()[2]
@@ -239,3 +233,9 @@ def sess(self):
 		except smtplib.SMTPException:
 			frappe.msgprint(_('Unable to send emails at this time'))
 			raise
+
+		except _socket.error as e:
+			# Invalid mail server -- due to refusing connection
+			frappe.msgprint(_('Invalid Outgoing Mail Server or Port'))
+			traceback = sys.exc_info()[2]
+			raise_(frappe.ValidationError, e, traceback)


Test patch:
Repository: frappe/frappe

Issue:
Workflow transition condition allows datetime functions
<!--
Welcome to the Frappe Framework issue tracker! Before creating an issue, please heed the following:

1. This tracker should only be used to report bugs and request features / enhancements to Frappe
    - For questions and general support, refer to https://stackoverflow.com/questions/tagged/frappe
    - For documentation issues, use https://frappeframework.com/docs/user/en or the developer cheetsheet https://frappeframework.com/docs/user/en/bench/resources/bench-commands-cheatsheet
2. Use the search function before creating a new issue. Duplicates will be closed and directed to
   the original discussion.
3. When making a feature request, make sure to be as verbose as possible. The better you convey your message, the     greater the drive to make it happen.
-->

**Is your feature request related to a problem? Please describe.**
Some workflows transitions may be date/time sensitive.  With current conditions, we don't have access to the current date or date add functions.

**Describe the solution you'd like**
I would like to use frappe.utils.now_datetime and frappe.utils.add_to_date with my workflow transition conditions.  for example:
doc.created <= frappe.utils.add_to_Date(frappe.utils.now_datetime, days=7)  
This would only allow the transition up to 7 days after the doc is created.

**Describe alternatives you've considered**
Perhaps the session last updated could be used, but this would not be accurate.

**Additional context**
The model.workflow module would be updated to 
def get_workflow_safe_globals():
	# access to frappe.db.get_value and frappe.db.get_list
	return dict(
		frappe=frappe._dict(
			db=frappe._dict(
				get_value=frappe.db.get_value,
				get_list=frappe.db.get_list
			),
			session=frappe.session,
			utils=frappe._dict(
				now_datetime=frappe.utils.now_datetime,
				add_to_date=frappe.utils.add_to_date
				)		
		)
	)



Discussion:

Resolving patch:
diff --git a/frappe/model/workflow.py b/frappe/model/workflow.py
--- a/frappe/model/workflow.py
+++ b/frappe/model/workflow.py
@@ -53,14 +53,17 @@ def get_transitions(doc, workflow = None, raise_exception=False):
 	return transitions
 
 def get_workflow_safe_globals():
-	# access to frappe.db.get_value and frappe.db.get_list
+	# access to frappe.db.get_value, frappe.db.get_list, and date time utils.
 	return dict(
 		frappe=frappe._dict(
-			db=frappe._dict(
-				get_value=frappe.db.get_value,
-				get_list=frappe.db.get_list
+			db=frappe._dict(get_value=frappe.db.get_value, get_list=frappe.db.get_list),
+			session=frappe.session,
+			utils=frappe._dict(
+				now_datetime=frappe.utils.now_datetime,
+				add_to_date=frappe.utils.add_to_date,
+				get_datetime=frappe.utils.get_datetime,
+				now=frappe.utils.now,
 			),
-			session=frappe.session
 		)
 	)
 


Test patch:
Repository: frappe/frappe

Issue:
[Bug] Sitemap has /all-products/index.js but no root / entry
## Description of the issue
The sitemap has an erroneous index.js entry and has no entry for the root homepage. The latter causes Google to list it differently in Google Search Console.

## Context information (for bug reports)

**Output of `bench version`**
```
ERPNext: v12.9.4
Frappe Framework: v12.6.2
```

## Steps to reproduce the issue

1. Go to /sitemap.xml

### Observed result
```
<url>
<loc>https://www.example.in/all-products/index.js</loc>
<lastmod>2020-06-07</lastmod>
</url>
```

### Expected result
```
<url>
<loc>https://www.example.in/</loc>
<lastmod>2020-06-07</lastmod>
</url>
```


Discussion:

Resolving patch:
diff --git a/frappe/website/router.py b/frappe/website/router.py
--- a/frappe/website/router.py
+++ b/frappe/website/router.py
@@ -210,7 +210,7 @@ def get_pages_from_path(start, app, app_path):
 				if not '.' in fname:
 					continue
 				page_name, extn = fname.rsplit(".", 1)
-				if extn in ('js', 'css') and os.path.exists(os.path.join(basepath, fname + '.html')):
+				if extn in ('js', 'css') and os.path.exists(os.path.join(basepath, page_name + '.html')):
 					# js, css is linked to html, skip
 					continue
 
@@ -441,4 +441,4 @@ def _get():
 	return frappe.cache().get_value('doctypes_with_web_view', _get)
 
 def get_start_folders():
-	return frappe.local.flags.web_pages_folders or ('www', 'templates/pages')
\ No newline at end of file
+	return frappe.local.flags.web_pages_folders or ('www', 'templates/pages')


Test patch:
Repository: frappe/frappe

Issue:
It is not possible to return 404 from portal pages
<!--
Welcome to the Frappe Framework issue tracker! Before creating an issue, please heed the following:

1. This tracker should only be used to report bugs and request features / enhancements to Frappe
    - For questions and general support, use https://stackoverflow.com/questions/tagged/frappe
    - For documentation issues, refer to https://frappeframework.com/docs/user/en or the developer cheetsheet https://github.com/frappe/frappe/wiki/Developer-Cheatsheet
2. Use the search function before creating a new issue. Duplicates will be closed and directed to
   the original discussion.
3. When making a bug report, make sure you provide all required information. The easier it is for
   maintainers to reproduce, the faster it'll be fixed.
4. If you think you know what the reason for the bug is, share it with us. Maybe put in a PR 😉
-->

## Description of the issue

There is no way to return 404 from the portal pages. 

When enquired about it in the _Frappe Framework Public_ Telegram group, @netchampfaris suggested to `raise frappe.PageDoesNotExistError`.  But that didn't work either. 

It looks like the [render.py][1] is handling the `PageDoesNotExistError`, but the [context.py][2] is handling only DoesNotExistError. Because of this, raising none of those exceptions work.

[1]: https://github.com/frappe/frappe/blob/develop/frappe/website/render.py#L50 
[2]: https://github.com/frappe/frappe/blob/develop/frappe/website/context.py#L61

## Context information (for bug reports)

**Output of `bench version`**
```
(paste here)
```

## Steps to reproduce the issue

1. Create a portal page with `raise frappe.PageDoesNotExist()` in the `get_context` function
2. Open that portal page in the browser
3. You'll see a portal page  (or 500 if a variable that is defined after the raise statement is used) instead of 404

### Observed result

a portal page  with 200 OK (or 500 if a variable that is defined after the raise statement is used)

### Expected result

404 Not Found

### Stacktrace / full error message

Not applicable.

## Additional information

OS version / distribution: Mac OS X 11.2.3

```
$ bench version
community 0.0.1
frappe 13.x.x-develop
```


Discussion:

Resolving patch:
diff --git a/frappe/website/context.py b/frappe/website/context.py
--- a/frappe/website/context.py
+++ b/frappe/website/context.py
@@ -58,7 +58,7 @@ def update_controller_context(context, controller):
 					ret = module.get_context()
 				if ret:
 					context.update(ret)
-			except (frappe.PermissionError, frappe.DoesNotExistError, frappe.Redirect):
+			except (frappe.PermissionError, frappe.PageDoesNotExistError, frappe.Redirect):
 				raise
 			except:
 				if not frappe.flags.in_migrate:


Test patch:
Repository: frappe/frappe

Issue:
Query report doesn't load translations from app
### Reproduce

1. Create a query report for your custom app
2. Add a column. Give it a weird name so there isn't a translation in frappe already. For example, "Not translated in frontend"
3. Add a translation to `your_app/translations/de.csv` (german, in this case). It contains the following line:

    ```csv
    Not translated in frontend,Im Frontend nicht übersetzt
    ```

4. Clear all the caches, restart etc
5. Load the query report

### Problem

Column labels don't get translated with the custom translations; the column label is not in `frappe._messages`. (Translations from frappe are still loaded; If your column label happens to be translated in frappe already you're lucky.)

On the desk/workspace translations are showing fine, but translation from the browser console does not work (I suppose it's rendered and translated in the backend). Translation from the python console works fine.

Browser console:

```js
__("Not translated in frontend") // -> Not translated in frontend
```

Python console:

```py
_("Not translated in frontend") # -> Im Frontend nicht übersetzt
```

Related Issue: #12368


Discussion:
@barredterra Tried doing `frappe.cache().flushall()`? translations are stored in shared cache.
I'll try, thanks!

Additional Note: The same translations are working as expected in Workspace and DocTypes, but not in Query Report. So I think it might be a problem with Query Report.
@surajshetty3416 `frappe.cache().flushall()` doesn't help. I updated the description, it seems to be a general problem with js translations.

Resolving patch:
diff --git a/frappe/translate.py b/frappe/translate.py
--- a/frappe/translate.py
+++ b/frappe/translate.py
@@ -443,8 +443,15 @@ def get_messages_from_report(name):
 	messages = _get_messages_from_page_or_report("Report", name,
 		frappe.db.get_value("DocType", report.ref_doctype, "module"))
 
+	if report.columns:
+		messages.extend([(None, report_column.label) for report_column in report.columns])
+
+	if report.filters:
+		messages.extend([(None, report_filter.label) for report_filter in report.filters])
+
 	if report.query:
 		messages.extend([(None, message) for message in re.findall('"([^:,^"]*):', report.query) if is_translatable(message)])
+
 	messages.append((None,report.report_name))
 	return messages
 


Test patch:
Repository: frappe/frappe

Issue:
The base template is generating invalid HTML

## Description of the issue

The `body` tag generated by the base template is as follows:

```
<body frappe-session-status="logged-out" data-path="courses/the-joy-of-programming/getting-started"class="">
```

Notice that there is no space before the class. 

While it is not harmful, it is still incorrect html.

## Context information (for bug reports)

**Output of `bench version`**
```
$ bench version
community 0.0.1
frappe 13.0.0-dev
```

## Steps to reproduce the issue

1. Open any frappe app and view source
2. Notice that there is no space before `class` in the body tag

### Observed result

```
<body frappe-session-status="logged-out" data-path="courses/the-joy-of-programming/getting-started"class="">
```

### Expected result

```
<body frappe-session-status="logged-out" data-path="courses/the-joy-of-programming/getting-started" class="">
```



Discussion:

Resolving patch:
diff --git a/frappe/__init__.py b/frappe/__init__.py
--- a/frappe/__init__.py
+++ b/frappe/__init__.py
@@ -33,7 +33,7 @@
 	reload(sys)
 	sys.setdefaultencoding("utf-8")
 
-__version__ = '13.1.0'
+__version__ = '13.1.1'
 
 __title__ = "Frappe Framework"
 
diff --git a/frappe/translate.py b/frappe/translate.py
--- a/frappe/translate.py
+++ b/frappe/translate.py
@@ -115,7 +115,7 @@ def get_dict(fortype, name=None):
 				messages.extend(get_server_messages(app))
 			messages = deduplicate_messages(messages)
 
-			messages += frappe.db.sql("""select "navbar", item_label from `tabNavbar Item` where item_label is not null""")
+			messages += frappe.db.sql("""select 'navbar', item_label from `tabNavbar Item` where item_label is not null""")
 			messages = get_messages_from_include_files()
 			messages += frappe.db.sql("select 'Print Format:', name from `tabPrint Format`")
 			messages += frappe.db.sql("select 'DocType:', name from tabDocType")


Test patch:
Repository: frappe/frappe

Issue:
doc.get_url returns wrong url
<!--
Welcome to the Frappe Framework issue tracker! Before creating an issue, please heed the following:

1. This tracker should only be used to report bugs and request features / enhancements to Frappe
    - For questions and general support, use https://stackoverflow.com/questions/tagged/frappe
    - For documentation issues, refer to https://frappeframework.com/docs/user/en or the developer cheetsheet https://github.com/frappe/frappe/wiki/Developer-Cheatsheet
2. Use the search function before creating a new issue. Duplicates will be closed and directed to
   the original discussion.
3. When making a bug report, make sure you provide all required information. The easier it is for
   maintainers to reproduce, the faster it'll be fixed.
4. If you think you know what the reason for the bug is, share it with us. Maybe put in a PR 😉
-->

## Description of the issue

## Context information (for bug reports)

**Output of `bench version`**
```
erpnext 13.0.0-dev
frappe 13.0.2
```

## Steps to reproduce the issue

```py
item = frappe.get_doc("Item", "302")
item.get_url() # -> '/app/Form/Item/302'
frappe.utils.data.get_url_to_form("Item", "302") # -> 'http://my.erp.com/app/item/302'
```

### Observed result

item.get_url() returns '/app/Form/Item/302'

### Expected result

item.get_url() returns '/app/item/302'





Discussion:
@abhishekbalam, Can I work on this issue?
@krishnakanthati 

Sure

Resolving patch:
diff --git a/frappe/model/document.py b/frappe/model/document.py
--- a/frappe/model/document.py
+++ b/frappe/model/document.py
@@ -17,6 +17,7 @@
 from frappe.utils.global_search import update_global_search
 from frappe.integrations.doctype.webhook import run_webhooks
 from frappe.desk.form.document_follow import follow_document
+from frappe.desk.utils import slug
 from frappe.core.doctype.server_script.server_script_utils import run_server_script_for_doc_event
 
 # once_only validation
@@ -1202,8 +1203,8 @@ def round_floats_in(self, doc, fieldnames=None):
 			doc.set(fieldname, flt(doc.get(fieldname), self.precision(fieldname, doc.parentfield)))
 
 	def get_url(self):
-		"""Returns Desk URL for this document. `/app/Form/{doctype}/{name}`"""
-		return "/app/Form/{doctype}/{name}".format(doctype=self.doctype, name=self.name)
+		"""Returns Desk URL for this document. `/app/{doctype}/{name}`"""
+		return f"/app/{slug(self.doctype)}/{self.name}"
 
 	def add_comment(self, comment_type='Comment', text=None, comment_email=None, link_doctype=None, link_name=None, comment_by=None):
 		"""Add a comment to this document.


Test patch:
Repository: frappe/frappe

Issue:
Review: Connected App: Difficult to see how Token Cache get_expires_in could be any more wrong
https://github.com/frappe/frappe/blob/86e512452d77f3e61405fd33ecd1bf881790ae18/frappe/integrations/doctype/token_cache/token_cache.py#L53

PR to follow


Discussion:

Resolving patch:
diff --git a/frappe/integrations/doctype/token_cache/token_cache.py b/frappe/integrations/doctype/token_cache/token_cache.py
--- a/frappe/integrations/doctype/token_cache/token_cache.py
+++ b/frappe/integrations/doctype/token_cache/token_cache.py
@@ -3,7 +3,7 @@
 # For license information, please see license.txt
 
 from __future__ import unicode_literals
-from datetime import datetime, timedelta
+from datetime import timedelta
 
 import frappe
 from frappe import _
@@ -52,8 +52,8 @@ def update_data(self, data):
 		return self
 
 	def get_expires_in(self):
-		expiry_time = frappe.utils.get_datetime(self.modified) + timedelta(self.expires_in)
-		return (datetime.now() - expiry_time).total_seconds()
+		expiry_time = frappe.utils.get_datetime(self.modified) + timedelta(seconds=self.expires_in)
+		return (expiry_time - frappe.utils.now_datetime()).total_seconds()
 
 	def is_expired(self):
 		return self.get_expires_in() < 0


Test patch:
Repository: frappe/frappe

Issue:
Problems while using translations via Globe Symbol 
## Description / Context information (for bug reports)

There are 2 issues - 

**Issue - 1** 
While updating translations using Globe Symbol, system generates duplicate rows of translations for records added in Description 


https://user-images.githubusercontent.com/16986940/132095963-9b747145-cdb8-4972-8cd9-e2dc907b45a3.mp4


<hr/>

**Issue - 2**
When clicking on the globe button of Item-2, values from the prev edited item (Item-1) are displayed.In order to refresh the correct values of Item-2, page reload is required.

https://user-images.githubusercontent.com/16986940/132095967-caefab6a-ff84-45dc-b1ad-50a0ba60e84f.mp4


**Output of `bench version`**

We Checked on version-12 and version-13
Problem is consistent for versions

```
Bench 5.5.0
ERPNext: v13.10.0 (version-13)
Frappe Framework: v13.10.0 (version-13)
```

```
Bench 5.5.0
ERPNext: v12.23.0 (version-12)
Frappe Framework: v12.20.0 (version-12)
```


## Steps to reproduce the issue

Check Translate On Description field for Item master.

<hr/>

- vamagithub@gmail.com 
- seanthankur20@gmail.com



Discussion:

Resolving patch:
diff --git a/frappe/translate.py b/frappe/translate.py
--- a/frappe/translate.py
+++ b/frappe/translate.py
@@ -846,6 +846,9 @@ def update_translations_for_source(source=None, translation_dict=None):
 
 	translation_dict = json.loads(translation_dict)
 
+	if is_html(source):
+		source = strip_html_tags(source)
+
 	# for existing records
 	translation_records = frappe.db.get_values('Translation', {
 		'source_text': source


Test patch: